Add !rpartial

master
Magnar Sveen 14 years ago
parent 05dec7aea4
commit 70488c2d6d
  1. 19
      README.md
  2. 15
      bang.el
  3. 6
      examples.el

@ -22,6 +22,7 @@ Or you can just dump `bang.el` in your load path somewhere.
* [!mapcat](#mapcat-fn-list) `(fn list)`
* [!first](#first-fn-list) `(fn list)`
* [!partial](#partial-fn-rest-args) `(fn &rest args)`
* [!rpartial](#rpartial-fn-rest-args) `(fn &rest args)`
* [!difference](#difference-list-list2) `(list list2)`
* [!intersection](#intersection-list-list2) `(list list2)`
* [!distinct](#distinct-list) `(list)`
@ -172,14 +173,26 @@ To get the first item in the list no questions asked, use `car`.
Takes a function `fn` and fewer than the normal arguments to `fn`,
and returns a fn that takes a variable number of additional `args`.
When called, the returned function calls `fn` with `args` +
additional args.
When called, the returned function calls `fn` with `args` first and
then additional args.
```cl
(funcall (!partial '+ 5) 3) ;; => 8
(funcall (!partial '- 5) 3) ;; => 2
(funcall (!partial '+ 5 2) 3) ;; => 10
```
### !rpartial `(fn &rest args)`
Takes a function `fn` and fewer than the normal arguments to `fn`,
and returns a fn that takes a variable number of additional `args`.
When called, the returned function calls `fn` with the additional
args first and then `args`.
```cl
(funcall (!rpartial '- 5) 8) ;; => 3
(funcall (!rpartial '- 5 2) 10) ;; => 3
```
### !difference `(list list2)`
Return a new list with only the members of `list` that are not in `list2`.

@ -141,9 +141,18 @@ Thus function FN should return a collection."
(defun !partial (fn &rest args)
"Takes a function FN and fewer than the normal arguments to FN,
and returns a fn that takes a variable number of additional ARGS.
When called, the returned function calls FN with ARGS +
additional args."
(apply 'apply-partially fn args))
When called, the returned function calls FN with ARGS first and
then additional args."
`(closure (t) (&rest args)
(apply ',fn ,@(mapcar (lambda (arg) `',arg) args) args)))
(defun !rpartial (fn &rest args)
"Takes a function FN and fewer than the normal arguments to FN,
and returns a fn that takes a variable number of additional ARGS.
When called, the returned function calls FN with the additional
args first and then ARGS."
`(closure (t) (&rest args)
(apply ',fn (append args ',args))))
(defun !distinct (list)
"Return a new list with all duplicates removed.

@ -64,9 +64,13 @@
(!!first (> it 2) '(1 2 3)) => 3)
(defexamples !partial
(funcall (!partial '+ 5) 3) => 8
(funcall (!partial '- 5) 3) => 2
(funcall (!partial '+ 5 2) 3) => 10)
(defexamples !rpartial
(funcall (!rpartial '- 5) 8) => 3
(funcall (!rpartial '- 5 2) 10) => 3)
(defexamples !difference
(!difference '() '()) => '()
(!difference '(1 2 3) '(4 5 6)) => '(1 2 3)

Loading…
Cancel
Save