Add clojure threading macros, !-> and !->>

master
Magnar Sveen 14 years ago
parent 70488c2d6d
commit 4205e58d7e
  1. 23
      bang.el
  2. 13
      examples.el

@ -154,6 +154,29 @@ args first and then ARGS."
`(closure (t) (&rest args)
(apply ',fn (append args ',args))))
(defmacro !-> (x &optional form &rest more)
"Threads the expr through the forms. Inserts X as the second
item in the first form, making a list of it if it is not a list
already. If there are more forms, inserts the first form as the
second item in second form, etc."
(cond
((null form) x)
((null more) (if (listp form)
`(,(car form) ,x ,@(cdr form))
(list form x)))
(:else `(!-> (!-> ,x ,form) ,@more))))
(defmacro !->> (x form &rest more)
"Threads the expr through the forms. Inserts X as the last item
in the first form, making a list of it if it is not a list
already. If there are more forms, inserts the first form as the
last item in second form, etc."
(if (null more)
(if (listp form)
`(,(car form) ,@(cdr form) ,x)
(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',

@ -71,6 +71,19 @@
(funcall (!rpartial '- 5) 8) => 3
(funcall (!rpartial '- 5 2) 10) => 3)
(defexamples !->
(!-> "Abc") => "Abc"
(!-> "Abc" (concat "def")) => "Abcdef"
(!-> "Abc" (concat "def") (concat "ghi")) => "Abcdefghi"
(!-> 5 square) => 25
(!-> 5 (+ 3) square) => 64)
(defexamples !->>
(!->> "Abc" (concat "def")) => "defAbc"
(!->> "Abc" (concat "def") (concat "ghi")) => "ghidefAbc"
(!->> 5 (- 8)) => 3
(!->> 5 (- 3) square) => 4)
(defexamples !difference
(!difference '() '()) => '()
(!difference '(1 2 3) '(4 5 6)) => '(1 2 3)

Loading…
Cancel
Save