Skip to content

Instantly share code, notes, and snippets.

@christianromney
Created November 29, 2016 18:56
Show Gist options
  • Save christianromney/285b1c3254cd4022d8226a8857345e0a to your computer and use it in GitHub Desktop.
Save christianromney/285b1c3254cd4022d8226a8857345e0a to your computer and use it in GitHub Desktop.
(defmacro <!?
"Exception-aware parking take. If a Throwable is returned
from the channel, it is re-thrown."
[c]
`(let [v# (<! ~c)]
(if (instance? Throwable v#)
(throw v#)
v#)))
(defmacro <!!?
"Exception-aware blocking take. If a Throwable is returned
from the channel, it is rethrown."
[c]
`(let [v# (<!! ~c)]
(if (instance? Throwable v#)
(throw v#)
v#)))
(defmacro go-try
"Tries to execute the body in a go block. If an exception is thrown,
it is placed on the channel."
[& body]
`(go
(try
~@body
(catch Throwable t#
t#))))
(defmacro thread-try
"Tries to execute the body in a thread block. If an exception is thrown,
it is placed on the channel."
[& body]
`(thread
(try
~@body
(catch Throwable t#
t#))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment