MonadFlow: A library for idiomatic error handling using the Either monad.
Functions should return a vector [val error], where val
represents a successful result
(the Right value), and error
represents a failure (the Left value). If no error occurs,
val
contains the result, and error
is nil. Functions not following this convention
can be 'lifted' using the lift
function.
MonadFlow: A library for idiomatic error handling using the Either monad. Functions should return a vector [val error], where `val` represents a successful result (the Right value), and `error` represents a failure (the Left value). If no error occurs, `val` contains the result, and `error` is nil. Functions not following this convention can be 'lifted' using the `lift` function.
The Either monad interface, representing success (Right) or error (Left).
The Either monad interface, representing success (Right) or error (Left).
(success? _)
Returns true if this is a Right value, indicating success.
Returns true if this is a Right value, indicating success.
(inspect-chain-> val body)
Inserts side-effecting operations into a try-chain->
flow for debugging or logging.
The value :flow-value
is replaced with the current computation value.
Example: (f/try-chain-> 10 (f/inspect-chain-> (println "initial value: " :flow-value)) inc (f/inspect-chain-> (println "intermediate value: " :flow-value)) inc (f/inspect-chain-> (println "not important")) inc (f/inspect-chain-> (println "final value: " :flow-value))) initial value: 10 intermediate value: 11 not important final value: 13 => Right{:value 13}
Inserts side-effecting operations into a `try-chain->` flow for debugging or logging. The value `:flow-value` is replaced with the current computation value. Example: (f/try-chain-> 10 (f/inspect-chain-> (println "initial value: " :flow-value)) inc (f/inspect-chain-> (println "intermediate value: " :flow-value)) inc (f/inspect-chain-> (println "not important")) inc (f/inspect-chain-> (println "final value: " :flow-value))) initial value: 10 intermediate value: 11 not important final value: 13 => Right{:value 13}
(inspect-chain->> body val)
Inserts side-effecting operations into a try-chain->>
flow for debugging or logging.
The value :flow-value
is replaced with the current computation value.
Example: (f/try-chain->> 10 (f/inspect-chain->> (println "initial value: " :flow-value)) inc (f/inspect-chain->> (println "intermediate value: " :flow-value)) inc (f/inspect-chain->> (println "not important")) inc (f/inspect-chain->> (println "final value: " :flow-value))) initial value: 10 intermediate value: 11 not important final value: 13 => .Right{:value 13}
Inserts side-effecting operations into a `try-chain->>` flow for debugging or logging. The value `:flow-value` is replaced with the current computation value. Example: (f/try-chain->> 10 (f/inspect-chain->> (println "initial value: " :flow-value)) inc (f/inspect-chain->> (println "intermediate value: " :flow-value)) inc (f/inspect-chain->> (println "not important")) inc (f/inspect-chain->> (println "final value: " :flow-value))) initial value: 10 intermediate value: 11 not important final value: 13 => .Right{:value 13}
(lift f)
(lift f error-fn)
Wraps a function f
with error handling, returning a monadic value
(Right on success, Left on error). Optionally, customize the error transformation with error-fn
.
Wraps a function `f` with error handling, returning a monadic value (Right on success, Left on error). Optionally, customize the error transformation with `error-fn`.
(lift-> body)
(lift-> val body)
Helper for more readable error handling inside try-chain->
.
Automatically wraps expressions in a try block and returns a monadic value.
Example: (try-chain-> {:a "a value"} (lift-> :a)) => Right{:value "a value"}
Helper for more readable error handling inside `try-chain->`. Automatically wraps expressions in a try block and returns a monadic value. Example: (try-chain-> {:a "a value"} (lift-> :a)) => Right{:value "a value"}
(lift->> body)
(lift->> body val)
Helper for more readable error handling inside try-chain->>
.
Automatically wraps expressions in a try block and returns a monadic value.
Example: (try-chain->> {:a "a value"} (lift->> :a)) => Right{:value "a value"}
Helper for more readable error handling inside `try-chain->>`. Automatically wraps expressions in a try block and returns a monadic value. Example: (try-chain->> {:a "a value"} (lift->> :a)) => Right{:value "a value"}
(lift-value x)
Wraps the plain value x
into a monadic (Right, Left) value.
If x
is nil or a [nil error] pair, it returns a Left with an error message.
Wraps the plain value `x` into a monadic (Right, Left) value. If `x` is nil or a [nil error] pair, it returns a Left with an error message.
(try-chain-> expr & forms)
Attempts to thread the expression expr
through forms, like ->
, but handling errors monadically.
Each function must return an Either value or a pair [val error] pair. If an error occurs (Left), it short-circuits the flow.
Example: (try-chain-> 1 (wrap inc)) => Right{:value 2}
(try-chain-> 1 (wrap inc) (wrap #(throw+ (str "error: " %)))) => Left{:error "error: 2"}
Attempts to thread the expression `expr` through forms, like `->`, but handling errors monadically. Each function must return an Either value or a pair [val error] pair. If an error occurs (Left), it short-circuits the flow. Example: (try-chain-> 1 (wrap inc)) => Right{:value 2} (try-chain-> 1 (wrap inc) (wrap #(throw+ (str "error: " %)))) => Left{:error "error: 2"}
(try-chain->> expr & forms)
Similar to try-chain->
, but uses the ->>
threading model. The result of each function is
passed as the last argument to the next form. Returns either a Right (success) or Left (error).
Example: (try-chain->> 1 (wrap inc) (wrap #(throw+ (str "error: " %)))) => Left{:error "error: 2"}
Similar to `try-chain->`, but uses the `->>` threading model. The result of each function is passed as the last argument to the next form. Returns either a Right (success) or Left (error). Example: (try-chain->> 1 (wrap inc) (wrap #(throw+ (str "error: " %)))) => Left{:error "error: 2"}
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close