Liking cljdoc? Tell your friends :D

rp.condition.result

A result 'type' à la Result in Rust, the Error monad, etc.

A result 'type' à la Result in Rust, the Error monad, etc.
raw docstring

handle-resultclj

(handle-result ok-fn error-fn {:keys [ok error]})
source

pipelineclj

(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)`
sourceraw docstring

resultclj

(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`.
sourceraw docstring

unwrapclj

(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.
sourceraw docstring

validate-match-argsclj

(validate-match-args conditions condition-1 condition-2)
source

with-resultcljmacro

(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) ;=> :bears
sourceraw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close