Last active
August 29, 2015 14:01
-
-
Save tmoerman/d7035032a1a1f4b15a6c to your computer and use it in GitHub Desktop.
Clojure core.async exception handling
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
(defn nil->keyword [x] (if (nil? x) :nil x)) | |
(defn nil-safe [f] (comp nil->keyword f)) | |
(defn lift | |
[f] | |
(fn [x] | |
(cond | |
(instance? Throwable x) x | |
(nil? x) :nil | |
(= :nil x) :nil | |
:default (try ((nil-safe f) x) | |
(catch Throwable t t))))) | |
(defn log-throwable [t] (if (instance? Throwable t) (prn "got: " t)) t) | |
(def input (chan)) | |
(->> input | |
(async/map< (lift #(/ 5 %))) | |
(async/map< (lift prn)) | |
(async/map< log-throwable) | |
(suck)) | |
(async/put! input 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment