Created
October 6, 2020 11:04
-
-
Save wwiill/43e59409e79b605e93ab16064468a018 to your computer and use it in GitHub Desktop.
Unless error macro
This file contains hidden or 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 unless-error-> | |
"Unless expr is a map like {:error xxx}, threads it into the first form (via ->), | |
and unless that result is a map like {:error xxx}, through the next etc" | |
[expr & forms] | |
(let [g (gensym) | |
steps (map (fn [step] `(if (:error ~g) ~g (~step ~g))) | |
forms)] | |
`(let [~g ~expr | |
~@(interleave (repeat g) (butlast steps))] | |
~(if (empty? steps) | |
g | |
(last steps))))) | |
(defn boo [x] | |
(println "boo:" x) | |
{:error "boo"}) | |
(defn foo [x] | |
(println "foo:" x) | |
(:name x)) | |
(defn moo [x] | |
(println "moo:" x) | |
(str/upper-case x)) | |
(unless-error-> {:name "trump"} foo boo moo) | |
(unless-error-> {:name "trump"} | |
foo | |
(fn [x] x) | |
moo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment