This library contains the most commonly used monads as well as macros for defining and using monads and useful monadic functions.
This library contains the most commonly used monads as well as macros for defining and using monads and useful monadic functions.
(asks f)
Returns a function of the current environment.
Returns a function of the current environment.
(call-cc f)
A computation in the cont monad that calls function f with a single argument representing the current continuation. The function f should return a continuation (which becomes the return value of call-cc), or call the passed-in current continuation to terminate.
A computation in the cont monad that calls function f with a single argument representing the current continuation. The function f should return a continuation (which becomes the return value of call-cc), or call the passed-in current continuation to terminate.
(cond-statement expr mexpr continuation)
Process a :cond steps when adding a new monadic step to the mexpr.
Process a :cond steps when adding a new monadic step to the mexpr.
Monad describing computations in continuation-passing style. The monadic values are functions that are called with a single argument representing the continuation of the computation, to which they pass their result.
Monad describing computations in continuation-passing style. The monadic values are functions that are called with a single argument representing the continuation of the computation, to which they pass their result.
(defmonad name operations)
(defmonad name doc-string operations)
Define a named monad by defining the monad operations. The definitions are written like bindings to the monad operations m-bind and m-result (required) and m-zero and m-plus (optional).
Define a named monad by defining the monad operations. The definitions are written like bindings to the monad operations m-bind and m-result (required) and m-zero and m-plus (optional).
(defmonadfn name docstring? attr-map? args expr)
(defmonadfn name docstring? attr-map? (args expr) ...)
Like defn, but for functions that use monad operations and are used inside a with-monad block.
Like defn, but for functions that use monad operations and are used inside a with-monad block.
(domonad steps expr)
(domonad name steps expr)
Monad comprehension. Takes the name of a monad, a vector of steps given as binding-form/monadic-expression pairs, and a result value specified by expr. The monadic-expression terms can use the binding variables of the previous steps. If the monad contains a definition of m-zero, the step list can also contain conditions of the form :when p, where the predicate p can contain the binding variables from all previous steps. A clause of the form :let [binding-form expr ...], where the bindings are given as a vector as for the use in let, establishes additional bindings that can be used in the following steps.
Monad comprehension. Takes the name of a monad, a vector of steps given as binding-form/monadic-expression pairs, and a result value specified by expr. The monadic-expression terms can use the binding variables of the previous steps. If the monad contains a definition of m-zero, the step list can also contain conditions of the form :when p, where the predicate p can contain the binding variables from all previous steps. A clause of the form :let [binding-form expr ...], where the bindings are given as a vector as for the use in let, establishes additional bindings that can be used in the following steps.
(fetch-state)
Return a state-monad function that returns the current state and does not modify it.
Return a state-monad function that returns the current state and does not modify it.
(fetch-val key)
Return a state-monad function that assumes the state to be a map and returns the value corresponding to the given key. The state is not modified.
Return a state-monad function that assumes the state to be a map and returns the value corresponding to the given key. The state is not modified.
Monad describing plain computations. This monad does in fact nothing at all. It is useful for testing, for combination with monad transformers, and for code that is parameterized with a monad.
Monad describing plain computations. This monad does in fact nothing at all. It is useful for testing, for combination with monad transformers, and for code that is parameterized with a monad.
(local f g)
Runs reader g in the context of an environment modified by f
Runs reader g in the context of an environment modified by f
(m+m-reduce+m m-bind m-result m-zero m-plus f mvs)
(m+m-reduce+m m-bind m-result m-zero m-plus f val mvs)
Chains together monadic computation steps that are each functions of one parameter. Each step is called with the result of the previous step as its argument. (m-chain (step1 step2)) is equivalent to (fn [x] (domonad [r1 (step1 x) r2 (step2 r1)] r2)).
Chains together monadic computation steps that are each functions of one parameter. Each step is called with the result of the previous step as its argument. (m-chain (step1 step2)) is equivalent to (fn [x] (domonad [r1 (step1 x) r2 (step2 r1)] r2)).
Bind the monadic value m to the function returning (f x) for argument x
Bind the monadic value m to the function returning (f x) for argument x
Converts a monadic value containing a monadic value into a 'simple' monadic value.
Converts a monadic value containing a monadic value into a 'simple' monadic value.
(m-lift n f)
Converts a function f of n arguments into a function of n monadic arguments returning a monadic value.
Converts a function f of n arguments into a function of n monadic arguments returning a monadic value.
'Executes' the sequence of monadic values resulting from mapping f onto the values xs. f must return a monadic value.
'Executes' the sequence of monadic values resulting from mapping f onto the values xs. f must return a monadic value.
Return the reduction of (m-lift 2 f) over the list of monadic values mvs with initial value (m-result val).
Return the reduction of (m-lift 2 f) over the list of monadic values mvs with initial value (m-result val).
'Executes' the monadic values in ms and returns a sequence of the basic values contained in them.
'Executes' the monadic values in ms and returns a sequence of the basic values contained in them.
While (p x) is false, replace x by the value returned by the monadic computation (f x). Return (m-result x) for the first x for which (p x) is true.
While (p x) is false, replace x by the value returned by the monadic computation (f x). Return (m-result x) for the first x for which (p x) is true.
(m-when test m-expr)
If test is logical true, return monadic value m-expr, else return (m-result nil).
If test is logical true, return monadic value m-expr, else return (m-result nil).
(m-when-not test m-expr)
If test if logical false, return monadic value m-expr, else return (m-result nil).
If test if logical false, return monadic value m-expr, else return (m-result nil).
Monad describing computations with possible failures. Failure is represented by nil, any other value is considered valid. As soon as a step returns nil, the whole computation will yield nil as well.
Monad describing computations with possible failures. Failure is represented by nil, any other value is considered valid. As soon as a step returns nil, the whole computation will yield nil as well.
(maybe-t m)
(maybe-t m nothing)
(maybe-t m nothing which-m-plus)
Monad transformer that transforms a monad m into a monad in which the base values can be invalid (represented by nothing, which defaults to nil). The third argument chooses if m-zero and m-plus are inherited from the base monad (use :m-plus-from-base) or adopt maybe-like behaviour (use :m-plus-from-transformer). The default is :m-plus-from-base if the base monad m has a definition for m-plus, and :m-plus-from-transformer otherwise.
Monad transformer that transforms a monad m into a monad in which the base values can be invalid (represented by nothing, which defaults to nil). The third argument chooses if m-zero and m-plus are inherited from the base monad (use :m-plus-from-base) or adopt maybe-like behaviour (use :m-plus-from-transformer). The default is :m-plus-from-base if the base monad m has a definition for m-plus, and :m-plus-from-transformer otherwise.
(monad operations)
Define a monad by defining the monad operations. The definitions are written like bindings to the monad operations m-bind and m-result (required) and m-zero and m-plus (optional).
Define a monad by defining the monad operations. The definitions are written like bindings to the monad operations m-bind and m-result (required) and m-zero and m-plus (optional).
(monad-transformer base which-m-plus operations)
Define a monad transformer in terms of the monad operations and the base monad. The argument which-m-plus chooses if m-zero and m-plus are taken from the base monad or from the transformer.
Define a monad transformer in terms of the monad operations and the base monad. The argument which-m-plus chooses if m-zero and m-plus are taken from the base monad or from the transformer.
Monad describing computations which read values from a shared environment. Also known as the environment monad.
Monad describing computations which read values from a shared environment. Also known as the environment monad.
(run-cont c)
Execute the computation c in the cont monad and return its result.
Execute the computation c in the cont monad and return its result.
Monad describing multi-valued computations, i.e. computations that can yield multiple values. Any object implementing the seq protocol can be used as a monadic value.
Monad describing multi-valued computations, i.e. computations that can yield multiple values. Any object implementing the seq protocol can be used as a monadic value.
(sequence-t m)
(sequence-t m which-m-plus)
Monad transformer that transforms a monad m into a monad in which the base values are sequences. The argument which-m-plus chooses if m-zero and m-plus are inherited from the base monad (use :m-plus-from-base) or adopt sequence-like behaviour (use :m-plus-from-transformer). The default is :m-plus-from-base if the base monad m has a definition for m-plus, and :m-plus-from-transformer otherwise.
Monad transformer that transforms a monad m into a monad in which the base values are sequences. The argument which-m-plus chooses if m-zero and m-plus are inherited from the base monad (use :m-plus-from-base) or adopt sequence-like behaviour (use :m-plus-from-transformer). The default is :m-plus-from-base if the base monad m has a definition for m-plus, and :m-plus-from-transformer otherwise.
Monad describing multi-valued computations, like sequence-m, but returning sets of results instead of sequences of results.
Monad describing multi-valued computations, like sequence-m, but returning sets of results instead of sequences of results.
(set-state s)
Return a state-monad function that replaces the current state by s and returns the previous state.
Return a state-monad function that replaces the current state by s and returns the previous state.
(set-val key val)
Return a state-monad function that assumes the state to be a map and replaces the value associated with key by val. The old value is returned.
Return a state-monad function that assumes the state to be a map and replaces the value associated with key by val. The old value is returned.
Monad describing stateful computations. The monadic values have the structure (fn [old-state] [result new-state]).
Monad describing stateful computations. The monadic values have the structure (fn [old-state] [result new-state]).
(state-m-until p f x)
An optimized implementation of m-until for the state monad that replaces recursion by a loop.
An optimized implementation of m-until for the state monad that replaces recursion by a loop.
(state-t m)
Monad transformer that transforms a monad m into a monad of stateful computations that have the base monad type as their result.
Monad transformer that transforms a monad m into a monad of stateful computations that have the base monad type as their result.
(update-state f)
Return a state-monad function that replaces the current state by the result of f applied to the current state and that returns the old state.
Return a state-monad function that replaces the current state by the result of f applied to the current state and that returns the old state.
(update-val key f)
Return a state-monad function that assumes the state to be a map and replaces the value associated with the given key by the return value of f applied to the old value. The old value is returned.
Return a state-monad function that assumes the state to be a map and replaces the value associated with the given key by the return value of f applied to the old value. The old value is returned.
(with-monad monad & exprs)
Evaluates an expression after replacing the keywords defining the monad operations by the functions associated with these keywords in the monad definition given by name.
Evaluates an expression after replacing the keywords defining the monad operations by the functions associated with these keywords in the monad definition given by name.
(with-state-field key statement)
Returns a state-monad function that expects a map as its state and runs statement (another state-monad function) on the state defined by the map entry corresponding to key. The map entry is updated with the new state returned by statement.
Returns a state-monad function that expects a map as its state and runs statement (another state-monad function) on the state defined by the map entry corresponding to key. The map entry is updated with the new state returned by statement.
(writer-m empty-accumulator)
Monad describing computations that accumulate data on the side, e.g. for logging. The monadic values have the structure [value log]. Any of the accumulators from clojure.contrib.accumulators can be used for storing the log data. Its empty value is passed as a parameter.
Monad describing computations that accumulate data on the side, e.g. for logging. The monadic values have the structure [value log]. Any of the accumulators from clojure.contrib.accumulators can be used for storing the log data. Its empty value is passed as a parameter.
Accumulation of values into containers
Accumulation of values into containers
(writer-m-add container value)
add value to container, return new container
add value to container, return new container
(writer-m-combine container1 container2)
combine two containers, return new container
combine two containers, return new container
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close