Threading macros
Macros that conditionally combine sequential forms for brevity or readability.
Macro: -> (x &optional form &rest more)
Thread the expr through the forms. Insert 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, insert the first form as the second item in second form, etc.
(-> '(2 3 5))
⇒ (2 3 5)(-> '(2 3 5) (append '(8 13)))
⇒ (2 3 5 8 13)(-> '(2 3 5) (append '(8 13)) (-slice 1 -1))
⇒ (3 5 8)Macro: ->> (x &optional form &rest more)
Thread the expr through the forms. Insert 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, insert the first form as the last item in second form, etc.
(->> '(1 2 3) (-map 'square))
⇒ (1 4 9)(->> '(1 2 3) (-map 'square) (-remove 'even?))
⇒ (1 9)(->> '(1 2 3) (-map 'square) (-reduce '+))
⇒ 14Macro: --> (x &rest forms)
Starting with the value of x, thread each expression through forms.
Insert x at the position signified by the symbol it in the first form. If there are more forms, insert the first form at the position signified by it in the second form, etc.
(--> "def" (concat "abc" it "ghi"))
⇒ "abcdefghi"(--> "def" (concat "abc" it "ghi") (upcase it))
⇒ "ABCDEFGHI"(--> "def" (concat "abc" it "ghi") upcase)
⇒ "ABCDEFGHI"Macro: -as-> (value variable &rest forms)
Starting with value, thread variable through forms.
In the first form, bind variable to value. In the second form, bind variable to the result of the first form, and so forth.
(-as-> 3 my-var (1+ my-var) (list my-var) (mapcar (lambda (ele) (* 2 ele)) my-var))
⇒ (8)(-as-> 3 my-var 1+)
⇒ 4(-as-> 3 my-var)
⇒ 3Macro: -some-> (x &optional form &rest more)
When expr is non-nil, thread it through the first form (via -> (see ->)), and when that result is non-nil, through the next form, etc.
(-some-> '(2 3 5))
⇒ (2 3 5)(-some-> 5 square)
⇒ 25(-some-> 5 even? square)
⇒ nilMacro: -some->> (x &optional form &rest more)
When expr is non-nil, thread it through the first form (via ->> (see ->>)), and when that result is non-nil, through the next form, etc.
(-some->> '(1 2 3) (-map 'square))
⇒ (1 4 9)(-some->> '(1 3 5) (-last 'even?) (+ 100))
⇒ nil(-some->> '(2 4 6) (-last 'even?) (+ 100))
⇒ 106Macro: -some--> (expr &rest forms)
Thread expr through forms via --> (see -->), while the result is non-nil. When expr evaluates to non-nil, thread the result through the first of forms, and when that result is non-nil, thread it through the next form, etc.
(-some--> "def" (concat "abc" it "ghi"))
⇒ "abcdefghi"(-some--> nil (concat "abc" it "ghi"))
⇒ nil(-some--> '(0 1) (-remove #'natnump it) (append it it) (-map #'1+ it))
⇒ ()Macro: -doto (init &rest forms)
Evaluate init and pass it as argument to forms with -> (see ->). The result of evaluating init is threaded through each of forms individually using -> (see ->), which see. The return value is result, which forms may have modified by side effect.
(-doto (list 1 2 3) pop pop)
⇒ (3)(-doto (cons 1 2) (setcar 3) (setcdr 4))
⇒ (3 . 4)(gethash 'k (--doto (make-hash-table) (puthash 'k 'v it)))
⇒ v