Created
April 3, 2014 00:18
-
-
Save tcrayford/9946004 to your computer and use it in GitHub Desktop.
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
(deftype Failure [f]) | |
(defn unwrap-fail [^Failure fail] | |
((.f fail))) | |
(defn fail? [potential-fail] | |
(isa? (class potential-fail) Failure)) | |
;;TODO: extract into a new namespace, maybe an OS project? | |
(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] | |
`(Failure. | |
(fn [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment