apply-partially lets you create a new function by fixing the first n arguments of an existing function. Is there any way to achieve unrestricted partial function application, i.e., the ability to fix any n arguments of a function without the constraint that they be the first n? An example from another language is Python's functools.partial.

  • 1
    I'm less sure about this, but suspect SRFI 26 does this. It's implemented in dash.el as -cut
    – wasamasa
    Dec 31 '14 at 18:54

The idea of transforming functions into different functions with specialized arguments has been elaborated upon in SRFI 26. There's an Emacs Lisp implementation of it in the very useful dash.el library where it's available as -cut.


Here's something that I just wrote down:

(defun foobar (a b c)
  (list a "foo" b "bar" c))

(defun lame-partial (fun &rest plist)
  (let ((args (copy-seq
               (help-function-arglist fun t))))
    (dolist (k (cl-remove-if-not #'keywordp plist))
      (setq args (delq (intern (substring (symbol-name k) 1)) args)))
    `(lambda (,@args)
           (lambda (x)
             (or (plist-get plist (intern (format ":%S" x)))
                 x)) (help-function-arglist fun t))))))

(fset 'foobar-partial-1 (lame-partial #'foobar :b 7 :c (+ 2 2)))
(fset 'foobar-partial-2 (lame-partial #'foobar :c "derp" :b 0))

(mapcar #'foobar-partial-1 '(0 1 2))
;; => ((0 "foo" 7 "bar" 4) (1 "foo" 7 "bar" 4) (2 "foo" 7 "bar" 4))

(mapcar #'foobar-partial-2 '(0 1 2))
;; =>
;; ((0 "foo" 0 "bar" "derp")
;;  (1 "foo" 0 "bar" "derp")
;;  (2 "foo" 0 "bar" "derp"))

It's probably lame, but the approach is different from -cut.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.