Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -npartial for arbitrary partial application #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions dash-functional.el
Expand Up @@ -45,6 +45,28 @@ 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, similar to `-partial' and
`-rpartial'.

ARGS will be spliced in after the Nth element in the additional
args. If N is negative, -1 is taken to be the final item, -2, the
penultimate item, etc.

This function satisfies the following laws:

(-npartial 0 ...) ≡ (-partial ...)
(-npartial -1 ...) ≡ (-rpartial ...)"
(lambda (&rest args-around) (apply fn (if (< n 0)
(append (-drop-last (1+ n) args-around)
args
(-take-last (1+ n) args-around))
(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
3 changes: 3 additions & 0 deletions dash-template.texi
Expand Up @@ -439,6 +439,9 @@ script to create an info manual.
@item
@uref{https://github.com/occidens,William West} made @code{-fixfn}
more robust at handling floats.
@item
@uref{https://github.com/daantjie,Daniel Oosthuizen} added @code{-npartial}
for arbitrary partial application.
@end itemize

Thanks!
Expand Down
6 changes: 6 additions & 0 deletions dev/examples.el
Expand Up @@ -1055,6 +1055,12 @@ new list."
(funcall (-rpartial '- 5) 8) => 3
(funcall (-rpartial '- 5 2) 10) => 3)

(defexamples -npartial
(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
(funcall (-npartial -1 #'concat "last " "words.") "These " "are " "the ") => "These are the last words."
(funcall (-npartial 3 (lambda (a b c d e f) (concat a b c d e f ".")) "penultimate " "words ") "I'll " "put " "the " "here") => "I'll put the penultimate words here.")

(defexamples -juxt
(funcall (-juxt '+ '-) 3 5) => '(8 -2)
(-map (-juxt 'identity 'square) '(1 2 3)) => '((1 1) (2 4) (3 9)))
Expand Down
2 changes: 1 addition & 1 deletion readme-template.md
Expand Up @@ -245,7 +245,7 @@ Change `readme-template.md` or `examples-to-docs.el` instead.
- [Vasilij Schneidermann](https://github.com/wasamasa) contributed `-some`.
- [William West](https://github.com/occidens) made `-fixfn` more robust at handling floats.
- [Cam Saül](https://github.com/camsaul) contributed `-some->`, `-some->>`, and `-some-->`.

- [Daniel Oosthuizen](https://github.com/daantjie)} added `-npartial` for arbitrary partial application.
Thanks!

## License
Expand Down