From 88522f33b497a6463ee73c4ba9479e853291035a Mon Sep 17 00:00:00 2001 From: Daanturo Date: Fri, 2 Jul 2021 11:22:11 +0700 Subject: [PATCH] Define apply-mid-partially * lisp/subr.el (apply-mid-partially): Currying functions with arbitrary arguments position. --- lisp/subr.el | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lisp/subr.el b/lisp/subr.el index 5965655d48..2c25343a76 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -474,6 +474,33 @@ function was called." (lambda (&rest args1) (apply fun (append args1 args)))) +(defun apply-mid-partially (fun position &rest args) + "Return a function that is a partial application of FUN to ARGS at POSITION. + +ARGS is a list of N arguments to pass to FUN, starting at +POSITION (integer). + +The result is a new function which does the same as FUN, except +that N arguments starting from POSITION (inclusive) are fixed at the +values with which this function was called. + +If POSITION is not an integer or is >= the length of the function +application's arguments in the future, ARGS will be at the last. + +Else if POSITION is non-negative integer, count from the left. + +Else (POSITION is a negative integer), count from the right." + (lambda (&rest other-args) + (let* ((right-partially (or (not (integerp position)) + (<= (length other-args) position))) + (first-args (seq-subseq other-args + 0 + (if right-partially nil position))) + (last-args (if right-partially + nil + (seq-subseq other-args position)))) + (apply fun (append first-args args last-args))))) + (defun zerop (number) "Return t if NUMBER is zero." ;; Used to be in C, but it's pointless since (= 0 n) is faster anyway because -- 2.32.0