A result 'type' à la Result in Rust, the Error monad, etc.
A result 'type' à la Result in Rust, the Error monad, etc.
(pipeline [f & more-fs] init)Pass init data value into a pipeline of functions.
Each function should return a "result".
As soon as an :error is encountered, the pipeline will return an :error result.
Otherwise the :ok value from each result will be passed as the input into the next
function until all functions have been called, and the final result will be returned.
When there are no functions, returns (result init)
Pass `init` data value into a pipeline of functions. Each function should return a "result". As soon as an :error is encountered, the pipeline will return an :error result. Otherwise the :ok value from each result will be passed as the input into the next function until all functions have been called, and the final result will be returned. When there are no functions, returns `(result init)`
(result x)A result is in an 'error' state if its error entry has an instance of ExceptionInfo as its value. Otherwise it's assumed to be ok.
A result is in an 'error' state if its `error` entry has an instance of `ExceptionInfo` as its value. Otherwise it's assumed to be `ok`.
(unwrap result)Blindly ask for the result, throwing the (:error result) exception if the result has an error instead.
Blindly ask for the result, throwing the `(:error result)` exception if the result has an error instead.
(with-result result condition-binding-a branch-a condition-binding-b branch-b)A 'pattern match' for the :ok and :error cases of the Result type.
This macro provides a single point of enforcement that both 'good' and 'bad' result scenarios are handled explicitly. This can be used to thread result values through a call stack rather than using dynamic scope or exceptions for control flow.
This could certainly be built upon to do something more dynamic by dispatching on the :ok and/or :error payloads.
Examples:
(with-result (result 42) [:ok data] (/ data 2) [:error e] (ex-data e)) ;=> 21
(with-result (result (ex-info "Bad news" {:type :bears})) [:ok data] data [:error e] (:type (ex-data e))) ;=> :bears
;; Destructuring is supported, just like in let
(with-result (result {:answer 42})
[:ok {:keys [answer]}] (/ answer 2)
[:error e] (ex-data e)) ;=> 21
;; Order of the :ok/:error clauses doesn't matter (with-result (result (ex-info "Bad news" {:type :bears})) [:error e] (:type (ex-data e)) [:ok data] data) ;=> :bears
A 'pattern match' for the :ok and :error cases of the Result type.
This macro provides a single point of enforcement that both 'good' and 'bad'
result scenarios are handled explicitly. This can be used to thread
result values through a call stack rather than using dynamic scope or
exceptions for control flow.
This could certainly be built upon to do something more dynamic by dispatching
on the :ok and/or :error payloads.
Examples:
(with-result (result 42)
[:ok data] (/ data 2)
[:error e] (ex-data e)) ;=> 21
(with-result (result (ex-info "Bad news" {:type :bears}))
[:ok data] data
[:error e] (:type (ex-data e))) ;=> :bears
;; Destructuring is supported, just like in `let`
(with-result (result {:answer 42})
[:ok {:keys [answer]}] (/ answer 2)
[:error e] (ex-data e)) ;=> 21
;; Order of the :ok/:error clauses doesn't matter
(with-result (result (ex-info "Bad news" {:type :bears}))
[:error e] (:type (ex-data e))
[:ok data] data) ;=> :bearscljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |