Monad
public protocol Monad : Selective
A Monad provides functionality to sequence operations that are dependent from one another.
Instances of Monad
must obey the following rules:
flatMap(pure(a), f) == f(a)
flatMap(fa, pure) == fa
flatMap(fa) { a in flatMap(f(a), g) } == flatMap(flatMap(fa, f), g)
Also, instances of Monad
derive a default implementation for Applicative.ap
as:
ap(ff, fa) == flapMap(ff, { f in map(fa, f) }
-
Sequentially compose two computations, passing any value produced by the first as an argument to the second.
Declaration
Parameters
fa
First computation.
f
A function describing the second computation, which depends on the value of the first.
Return Value
Result of composing the two computations.
-
Monadic tail recursion.
tailRecM
can be used for computations that can potentially make the stack overflow.Initially introduced in Stack Safety for Free
Declaration
Parameters
a
Initial value for the recursion.
f
A function describing a recursive step.
Return Value
Result of evaluating recursively the provided function with the initial value.
-
ap(_:_:)
Extension method -
select(_:_:)
Extension method -
flatten(_:)
Extension method -
followedBy(_:_:)
Extension methodSequentially compose two computations, discarding the value produced by the first.
Declaration
Parameters
fa
1st computation.
fb
2nd computation.
Return Value
Result of running the second computation after the first one.
-
followedByEval(_:_:)
Extension methodSequentially compose a computation with a potentially lazy one, discarding the value produced by the first.
Declaration
Parameters
fa
Regular computation.
fb
Potentially lazy computation.
Return Value
Result of running the second computation after the first one.
-
forEffect(_:_:)
Extension methodSequentially compose two computations, discarding the value produced by the second.
Declaration
Parameters
fa
1st computation.
fb
2nd computation.
Return Value
Result produced from the first computation after both are computed.
-
forEffectEval(_:_:)
Extension methodSequentially compose a computation with a potentially lazy one, discarding the value produced by the second.
Declaration
Parameters
fa
Regular computation.
fb
Potentially lazy computation.
Return Value
Result produced from the first computation after both are computed.
-
mproduct(_:_:)
Extension methodPair the result of a computation with the result of applying a function to such result.
Declaration
Parameters
fa
A computation in the context implementing this instance.
f
A function to be applied to the result of the computation.
Return Value
A tuple of the result of the computation paired with the result of the function, in the context implementing this instance.
-
ifM(_:_:_:)
Extension methodConditionally apply a closure based on the boolean result of a computation.
Declaration
Parameters
fa
A boolean computation.
ifTrue
Closure to be applied if the computation evaluates to
true
.ifFalse
Closure to be applied if the computation evaluates to
false
.Return Value
Result of applying the corresponding closure based on the result of the computation.
-
flatTap(_:_:)
Extension methodApplies a monadic function and discard the result while keeping the effect.
Declaration
Parameters
fa
A computation.
f
A monadic function which result will be discarded.
Return Value
A computation with the result of the initial computation and the effect caused by the function application.
-
stackSafeFlatMap(_:_:)
Extension methodA stack safe version of
flatMap
, based ontailRecM
.Declaration
Parameters
fa
First computation.
f
A function describing the second computation, which depends on the value of the first.
Return Value
Result of composing the two computations.