Skip to content

Commit

Permalink
Add `-npartial' for arbitrary currying
Browse files Browse the repository at this point in the history
Currently, we have `-partial' and `-rpartial' for currying with
arguments at either the left or the right, but we don't have anything
for truly arbitrary application of curried arguments.

Therefore I've included `-npartial' to plug that gap. It takes an offset
N to determine where to put the curried arguments, a function FN, and
the curried ARGS.

E.g.:
    (funcall (-npartial 1 (lambda (a b c) (+ a (* b c))) 5) 2 3) ;; => 17
    (funcall (-npartial 2 (lambda (a b c d) (+ a (* b c) d)) 3) 2 4 5) ;; => 19

(Note that I have a slightly different version of texinfo -- 6 instead
of 4 -- so the way that the paragraphs have been filled is slightly
different, but I'm afraid there's nothing much I can do, because Arch
can't downgrade all the way to 4.)
  • Loading branch information
daantjie committed Nov 12, 2016
1 parent 07c61f5 commit e6013f9
Show file tree
Hide file tree
Showing 5 changed files with 596 additions and 508 deletions.
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -292,6 +292,7 @@ These combinators require Emacs 24 for its lexical scope. So they are offered in

* [-partial](#-partial-fn-rest-args) `(fn &rest args)`
* [-rpartial](#-rpartial-fn-rest-args) `(fn &rest args)`
* [-npartial](#-npartial-n-fn-rest-args) `(n fn &rest args)`
* [-juxt](#-juxt-rest-fns) `(&rest fns)`
* [-compose](#-compose-rest-fns) `(&rest fns)`
* [-applify](#-applify-fn) `(fn)`
Expand Down Expand Up @@ -1669,6 +1670,7 @@ Return the first item of `list`, or nil on an empty list.
```el
(-first-item '(1 2 3)) ;; => 1
(-first-item nil) ;; => nil
(let ((list (list 1 2 3))) (setf (-first-item list) 5) list) ;; => '(5 2 3)
```

#### -last-item `(list)`
Expand All @@ -1678,6 +1680,7 @@ Return the last item of `list`, or nil on an empty list.
```el
(-last-item '(1 2 3)) ;; => 3
(-last-item nil) ;; => nil
(let ((list (list 1 2 3))) (setf (-last-item list) 5) list) ;; => '(1 2 5)
```

#### -butlast `(list)`
Expand Down Expand Up @@ -2280,6 +2283,19 @@ args first and then `args`.
(funcall (-rpartial '- 5 2) 10) ;; => 3
```

#### -npartial `(n fn &rest args)`

Takes an offset `n`, a function `fn` and fewer than the normal
arguments to `fn`, and returns a function which takes a variable
number of additional `args`. When called, the returned function
calls `fn` with the first `n` additional args first, then `args`, then
the remaining additional args.

```el
(funcall (-npartial 1 (lambda (a b c) (+ a (* b c))) 5) 2 3) ;; => 17
(funcall (-npartial 2 (lambda (a b c d) (+ a (* b c) d)) 3) 2 4 5) ;; => 19
```

#### -juxt `(&rest fns)`

Takes a list of functions and returns a fn that is the
Expand Down
10 changes: 10 additions & 0 deletions dash-functional.el
Expand Up @@ -45,6 +45,16 @@ When called, the returned function calls FN with the additional
args first and then ARGS."
(lambda (&rest args-before) (apply fn (append args-before args))))

(defun -npartial (n fn &rest args)
"Takes an offset N, a function FN and fewer than the normal
arguments to FN, and returns a function which takes a variable
number of additional ARGS. When called, the returned function
calls FN with the first N additional args first, then ARGS, then
the remaining additional args."
(lambda (&rest args-around) (apply fn (append (-take n args-around)
args
(-drop n args-around)))))

(defun -juxt (&rest fns)
"Takes a list of functions and returns a fn that is the
juxtaposition of those fns. The returned fn takes a variable
Expand Down

0 comments on commit e6013f9

Please sign in to comment.