-
-
Save mccraigmccraig/07a6ef639eff0d5c6957c5fe60b12b92 to your computer and use it in GitHub Desktop.
This file contains 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 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