Add threading macro !!-> with `it` as placeholder.

master
Magnar Sveen 14 years ago
parent 9410f7e5a6
commit ec6a85fcb5
  1. 16
      README.md
  2. 11
      bang.el
  3. 10
      examples.el

@ -27,6 +27,7 @@ Or you can just dump `bang.el` in your load path somewhere.
* [!rpartial](#rpartial-fn-rest-args) `(fn &rest args)`
* [!->](#x-optional-form-rest-more) `(x &optional form &rest more)`
* [!->>](#x-form-rest-more) `(x form &rest more)`
* [!!->](#x-form-rest-more) `(x form &rest more)`
* [!difference](#difference-list-list2) `(list list2)`
* [!intersection](#intersection-list-list2) `(list list2)`
* [!distinct](#distinct-list) `(list)`
@ -179,8 +180,8 @@ through the `rep` function.
```cl
(!replace-where 'even? 'square '(1 2 3 4)) ;; => '(1 4 3 16)
(!replace-where (lambda (n) (= n 3)) (lambda (n) 0) '(1 2 3 4)) ;; => '(1 2 0 4)
(!!replace-where (> it 2) (* it it) '(1 2 3 4)) ;; => '(1 2 9 16)
(!!replace-where (= it 2) 17 '(1 2 3 4)) ;; => '(1 17 3 4)
```
### !first `(fn list)`
@ -247,6 +248,19 @@ last item in second form, etc.
(!->> 5 (- 8)) ;; => 3
```
### !!-> `(x form &rest more)`
Threads the expr through the forms. Inserts `x` at the position
signified by the token `it` in the first form. If there are more
forms, inserts the first form at the position signified by `it`
in in second form, etc.
```cl
(!!-> "def" (concat "abc" it "ghi")) ;; => "abcdefghi"
(!!-> "def" (concat "abc" it "ghi") (upcase it)) ;; => "ABCDEFGHI"
(!!-> "def" (concat "abc" it "ghi") upcase) ;; => "ABCDEFGHI"
```
### !difference `(list list2)`
Return a new list with only the members of `list` that are not in `list2`.

@ -215,6 +215,17 @@ last item in second form, etc."
(list form x))
`(!->> (!->> ,x ,form) ,@more)))
(defmacro !!-> (x form &rest more)
"Threads the expr through the forms. Inserts X at the position
signified by the token `it' in the first form. If there are more
forms, inserts the first form at the position signified by `it'
in in second form, etc."
(if (null more)
(if (listp form)
(!!replace-where (eq it 'it) x form)
(list form x))
`(!!-> (!!-> ,x ,form) ,@more)))
(defun !distinct (list)
"Return a new list with all duplicates removed.
The test for equality is done with `equal',

@ -65,8 +65,9 @@
(defexamples !replace-where
(!replace-where 'even? 'square '(1 2 3 4)) => '(1 4 3 16)
(!replace-where (lambda (n) (= n 3)) (lambda (n) 0) '(1 2 3 4)) => '(1 2 0 4)
(!!replace-where (> it 2) (* it it) '(1 2 3 4)) => '(1 2 9 16))
(!!replace-where (> it 2) (* it it) '(1 2 3 4)) => '(1 2 9 16)
(!!replace-where (= it 2) 17 '(1 2 3 4)) => '(1 17 3 4)
(!replace-where (lambda (n) (= n 3)) (lambda (n) 0) '(1 2 3 4)) => '(1 2 0 4))
(defexamples !first
(!first 'even? '(1 2 3)) => 2
@ -95,6 +96,11 @@
(!->> 5 (- 8)) => 3
(!->> 5 (- 3) square) => 4)
(defexamples !!->
(!!-> "def" (concat "abc" it "ghi")) => "abcdefghi"
(!!-> "def" (concat "abc" it "ghi") (upcase it)) => "ABCDEFGHI"
(!!-> "def" (concat "abc" it "ghi") upcase) => "ABCDEFGHI")
(defexamples !difference
(!difference '() '()) => '()
(!difference '(1 2 3) '(4 5 6)) => '(1 2 3)

Loading…
Cancel
Save