Last active
June 25, 2018 19:07
-
-
Save borkdude/af4978cd9849357aed25144369fe007c to your computer and use it in GitHub Desktop.
as-some-> macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro as-some-> | |
"Hybrid of as-> and some->." | |
[expr name & forms] | |
(let [steps (map (fn [step] `(if (nil? ~name) nil (-> ~name ~step))) | |
forms)] | |
`(let [~name ~expr | |
~@(interleave (repeat name) (butlast steps))] | |
~(if (empty? forms) | |
name | |
(last steps))))) | |
(as-some-> 1 x inc inc inc) ;; 4 | |
(as-some-> 1 x inc inc (when-not (pos? x) 1) inc) ;; nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment