--=-=-= Content-Type: text/plain Tags: patch > Could we instead > provide something that allows partial application of arbitrary > arguments, e.g. one of the arguments in the middle? I have tried implementing, but it's really hard to provide a practical example for documenting. Also, most of the time after specifying a position, we would insert to left of that. If 0 is for regular `apply-partially`, then -1 is certainly not possible for `apply-rpartially`. To pass ARGS at the last, should the following condition be OK?: "If POSITION is not an integer or is >= the length of the function application's arguments in the future." (funcall (apply-mid-partially #'append 0 '(0 1 2 3)) '(4) '(5)) ; Equivalent to `apply-partially'` => (0 1 2 3 4 5) (funcall (apply-mid-partially #'append 1 '(1 2 3)) '(0) '(4)) => (0 1 2 3 4) (funcall (apply-mid-partially #'append -1 '(1 2 3)) '(-2 -1) '(4 5) '(6 7)) => (-2 -1 4 5 1 2 3 6 7) ; apply-rpartially (funcall (apply-mid-partially #'append most-positive-fixnum '(1 2 3)) '(-2 -1) '(4 5) '(6 7)) => (-2 -1 4 5 6 7 1 2 3) (funcall (apply-mid-partially #'append 'foo '(1 2 3)) '(-2 -1) '(4 5) '(6 7)) => (-2 -1 4 5 6 7 1 2 3) --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0005-b-Define-apply-mid-partially.patch 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 --=-=-=-- On 7/2/21 5:34 AM, Michael Heerdegen wrote: > daanturo writes: > > | +(defun apply-rpartially (fun &rest args) > | + "Return a function that is a partial application of FUN to ARGS to the > | right. > | +ARGS is a list of the last N arguments to pass to FUN. > > I wonder: If we leave syntax aside for a moment - this suggestion seems > to provide a solution for a quite special case: is this useful more > often than partial application of arbitrary arguments? Could we instead > provide something that allows partial application of arbitrary > arguments, e.g. one of the arguments in the middle? > > > Michael. -- Daanturo.