Liking cljdoc? Tell your friends :D

clojure.algo.monads

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.
raw docstring

askclj

(ask)

Returns the environment.

Returns the environment.
sourceraw docstring

asksclj

(asks f)

Returns a function of the current environment.

Returns a function of the current environment.
sourceraw docstring

call-ccclj

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

censorclj

(censor f mv)
source

cond-statementclj

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

cont-mclj

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

defmonadcljmacro

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

defmonadfncljmacro

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

domonadcljmacro

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

fetch-stateclj

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

fetch-valclj

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

identity-mclj

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

listenclj

(listen mv)
source

localclj

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

m+m-chain+mclj

(m+m-chain+m m-bind m-result m-zero m-plus steps)
source

m+m-fmap+mclj

(m+m-fmap+m m-bind m-result m-zero m-plus f m)
source

m+m-join+mclj

(m+m-join+m m-bind m-result m-zero m-plus m)
source

m+m-map+mclj

(m+m-map+m m-bind m-result m-zero m-plus f xs)
source

m+m-reduce+mclj

(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)
source

m+m-seq+mclj

(m+m-seq+m m-bind m-result m-zero m-plus ms)
source

m+m-until+mclj

(m+m-until+m m-bind m-result m-zero m-plus p f x)
source

m+write+mclj

(m+write+m m-bind m-result m-zero m-plus v)
source

m-bindclj

source

m-chainclj

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

m-fmapclj

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

m-joinclj

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

m-liftcljmacro

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

m-mapclj

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

m-plusclj

source

m-reduceclj

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

m-resultclj

source

m-seqclj

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

m-untilclj

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

m-whencljmacro

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

m-when-notcljmacro

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

m-zeroclj

source

maybe-mclj

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

maybe-tclj

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

monadcljmacro

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

monad-transformercljmacro

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

reader-mclj

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

run-contclj

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

sequence-mclj

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

sequence-tclj

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

set-mclj

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

set-stateclj

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

set-valclj

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

state-mclj

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]).
sourceraw docstring

state-m-untilclj

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

state-tclj

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

update-stateclj

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

update-valclj

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

with-monadcljmacro

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

with-state-fieldclj

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

writeclj

source

writer-mclj

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

writer-monad-protocolcljprotocol

Accumulation of values into containers

Accumulation of values into containers

writer-m-addclj

(writer-m-add container value)

add value to container, return new container

add value to container, return new container

writer-m-combineclj

(writer-m-combine container1 container2)

combine two containers, return new container

combine two containers, return new container
sourceraw docstring

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

× close