Skip to content

Instantly share code, notes, and snippets.

@mccraigmccraig
Created February 18, 2021 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mccraigmccraig/07a6ef639eff0d5c6957c5fe60b12b92 to your computer and use it in GitHub Desktop.
Save mccraigmccraig/07a6ef639eff0d5c6957c5fe60b12b92 to your computer and use it in GitHub Desktop.
(defn stuff
[base]
(m/mlet rwx/rwexception-ctx
[_ (writer/tell [:base base])
{offs :config/offset} (reader/ask)
_ (writer/tell [:config/offset offs])
tot (m/return (+ base offs))
_ (writer/tell [:total tot])]
;; (throw (ex-info "boo" {}))
(m/return tot)))
(defn handle-stuff
[& args]
(m/with-context rwx/rwexception-ctx
(-> (apply stuff args)
(e/handle
(fn [left right]
(if (some? left)
(m/mlet
[_ (writer/tell [:ex :handled])]
(m/return (ex-message left)))
(m/mlet
[_ (writer/tell [:ex :none])]
(m/return right))))))))
;; writer log is preserved through errors,
;; and you can log in handle, catch and finally
(r/run (handle-stuff 100)
{:monad.reader/env {:config/offset 100}})
=>
{:monad.writer/output {:base [100],
:config/offset [100],
:total [200],
:ex [:none]},
:monad/val 200}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment