2019-05-15 15:06:26 +02:00
|
|
|
;; Composition of partially applied functions.
|
|
|
|
|
|
2019-06-02 15:35:54 +02:00
|
|
|
(load-file "../lib/load-file-once.mal")
|
|
|
|
|
(load-file-once "../lib/reducers.mal") ; reduce
|
2019-05-15 15:06:26 +02:00
|
|
|
|
|
|
|
|
;; Rewrite x (a a1 a2) .. (b b1 b2) as
|
|
|
|
|
;; (b (.. (a x a1 a2) ..) b1 b2)
|
|
|
|
|
;; If anything else than a list is found were `(a a1 a2)` is expected,
|
|
|
|
|
;; replace it with a list with one element, so that `-> x a` is
|
|
|
|
|
;; equivalent to `-> x (list a)`.
|
|
|
|
|
(defmacro! ->
|
|
|
|
|
(fn* (x & xs)
|
2019-05-15 15:49:35 +02:00
|
|
|
(reduce _iter-> x xs)))
|
|
|
|
|
|
|
|
|
|
(def! _iter->
|
|
|
|
|
(fn* [acc form]
|
|
|
|
|
(if (list? form)
|
|
|
|
|
`(~(first form) ~acc ~@(rest form))
|
|
|
|
|
(list form acc))))
|
2019-05-15 15:06:26 +02:00
|
|
|
|
|
|
|
|
;; Like `->`, but the arguments describe functions that are partially
|
|
|
|
|
;; applied with *left* arguments. The previous result is inserted at
|
|
|
|
|
;; the *end* of the new argument list.
|
|
|
|
|
;; Rewrite x ((a a1 a2) .. (b b1 b2)) as
|
|
|
|
|
;; (b b1 b2 (.. (a a1 a2 x) ..)).
|
|
|
|
|
(defmacro! ->>
|
|
|
|
|
(fn* (x & xs)
|
2019-05-15 15:49:35 +02:00
|
|
|
(reduce _iter->> x xs)))
|
|
|
|
|
|
|
|
|
|
(def! _iter->>
|
|
|
|
|
(fn* [acc form]
|
|
|
|
|
(if (list? form)
|
|
|
|
|
`(~(first form) ~@(rest form) ~acc)
|
|
|
|
|
(list form acc))))
|