Skip to content

Instantly share code, notes, and snippets.

@tcrayford
Last active March 31, 2017 15:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcrayford/df4afe5a01801464ef76 to your computer and use it in GitHub Desktop.
Save tcrayford/df4afe5a01801464ef76 to your computer and use it in GitHub Desktop.
(ns yeller.infrastructure.err)
(deftype Failure [f])
(defn unwrap-fail [^Failure fail]
(.f fail))
(defn fail? [potential-fail]
(isa? (class potential-fail) Failure))
(defmacro err->>
"Threads like ->>, but if
an expr returns a Failure,
run the function embedded in
the failure instead of continuing"
([x form]
(if (seq? form)
(with-meta `(~(first form) ~@(next form) ~x) (meta form))
(list form x)))
([x form & more]
`(let [result# (->> ~x ~form)]
(if (fail? result#)
(unwrap-fail result#)
(err->> result# ~@more)))))
(defmacro fail [& body]
`(do
~@(butlast body)
(Failure.
~(last body))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment