Merge pull request #532 from dubek/vhdl-fix-defmacro
[jackhill/mal.git] / impls / lib / threading.mal
CommitLineData
dcdb6c02
NB
1;; Composition of partially applied functions.
2
13e679cd
NB
3(load-file "../lib/load-file-once.mal")
4(load-file-once "../lib/reducers.mal") ; reduce
dcdb6c02
NB
5
6;; Rewrite x (a a1 a2) .. (b b1 b2) as
7;; (b (.. (a x a1 a2) ..) b1 b2)
8;; If anything else than a list is found were `(a a1 a2)` is expected,
9;; replace it with a list with one element, so that `-> x a` is
10;; equivalent to `-> x (list a)`.
11(defmacro! ->
12 (fn* (x & xs)
bf6647fb
NB
13 (reduce _iter-> x xs)))
14
15(def! _iter->
16 (fn* [acc form]
17 (if (list? form)
18 `(~(first form) ~acc ~@(rest form))
19 (list form acc))))
dcdb6c02
NB
20
21;; Like `->`, but the arguments describe functions that are partially
22;; applied with *left* arguments. The previous result is inserted at
23;; the *end* of the new argument list.
24;; Rewrite x ((a a1 a2) .. (b b1 b2)) as
25;; (b b1 b2 (.. (a a1 a2 x) ..)).
26(defmacro! ->>
27 (fn* (x & xs)
bf6647fb
NB
28 (reduce _iter->> x xs)))
29
30(def! _iter->>
31 (fn* [acc form]
32 (if (list? form)
33 `(~(first form) ~@(rest form) ~acc)
34 (list form acc))))