Skip to content

Instantly share code, notes, and snippets.

@borkdude
Last active June 25, 2018 19:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borkdude/af4978cd9849357aed25144369fe007c to your computer and use it in GitHub Desktop.
Save borkdude/af4978cd9849357aed25144369fe007c to your computer and use it in GitHub Desktop.
as-some-> macro
(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