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

Swift

static func flatMap<A, B>(_ fa: Kind<Self, A>, _ f: @escaping (A) -> Kind<Self, B>) -> Kind<Self, B>

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

Swift

static func tailRecM<A, B>(_ a: A, _ f : @escaping (A) -> Kind<Self, Either<A, B>>) -> Kind<Self, B>

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

Declaration

Swift

static func ap<A, B>(_ ff: Kind<Self, (A) -> B>, _ fa: Kind<Self, A>) -> Kind<Self, B>
• select(_:_:) Extension method

Declaration

Swift

static func select<A, B>(_ fab: Kind<Self, Either<A, B>>, _ f: Kind<Self, (A) -> B>) -> Kind<Self, B>
• flatten(_:) Extension method

Flattens a nested structure of the context implementing this instance into a single layer.

Declaration

Swift

static func flatten<A>(_ ffa: Kind<Self, Kind<Self, A>>) -> Kind<Self, A>

Parameters

 ffa Value with a nested structure.

Return Value

Value with a single context structure.

• followedBy(_:_:) Extension method

Sequentially compose two computations, discarding the value produced by the first.

Declaration

Swift

static func followedBy<A, B>(_ fa: Kind<Self, A>, _ fb: Kind<Self, B>) -> Kind<Self, B>

Parameters

 fa 1st computation. fb 2nd computation.

Return Value

Result of running the second computation after the first one.

• followedByEval(_:_:) Extension method

Sequentially compose a computation with a potentially lazy one, discarding the value produced by the first.

Declaration

Swift

static func followedByEval<A, B>(_ fa: Kind<Self, A>, _ fb: Eval<Kind<Self, B>>) -> Kind<Self, B>

Parameters

 fa Regular computation. fb Potentially lazy computation.

Return Value

Result of running the second computation after the first one.

• forEffect(_:_:) Extension method

Sequentially compose two computations, discarding the value produced by the second.

Declaration

Swift

static func forEffect<A, B>(_ fa: Kind<Self, A>, _ fb: Kind<Self, B>) -> Kind<Self, A>

Parameters

 fa 1st computation. fb 2nd computation.

Return Value

Result produced from the first computation after both are computed.

• forEffectEval(_:_:) Extension method

Sequentially compose a computation with a potentially lazy one, discarding the value produced by the second.

Declaration

Swift

static func forEffectEval<A, B>(_ fa: Kind<Self, A>, _ fb: Eval<Kind<Self, B>>) -> Kind<Self, A>

Parameters

 fa Regular computation. fb Potentially lazy computation.

Return Value

Result produced from the first computation after both are computed.

• mproduct(_:_:) Extension method

Pair the result of a computation with the result of applying a function to such result.

Declaration

Swift

static func mproduct<A, B>(_ fa: Kind<Self, A>, _ f: @escaping (A) -> Kind<Self, B>) -> Kind<Self, (A, B)>

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 method

Conditionally apply a closure based on the boolean result of a computation.

Declaration

Swift

static func ifM<B>(_ fa: Kind<Self, Bool>, _ ifTrue: @escaping () -> Kind<Self, B>, _ ifFalse: @escaping () -> Kind<Self, B>) -> Kind<Self, B>

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.

• binding(_:_:) Extension method

Monad comprehensions for two closures producing computations.

Each closure is expected to provide a computation whose result may be used in subsequent computations. For this reason, the ith closure receives all values generated by the instructions evaluated before it.

Declaration

Swift

static func binding<B, C>(_ f: () -> Kind<Self, B>,
_ fb: @escaping (B) -> Kind<Self, C>) -> Kind<Self, C>

Parameters

 f 1st closure providing a computation. fb 2nd closure providing a computation.

Return Value

Result of evaluating the last computation.

• binding(_:_:_:) Extension method

Monad comprehensions for three closures producing computations.

Each closure is expected to provide a computation whose result may be used in subsequent computations. For this reason, the ith closure receives all values generated by the instructions evaluated before it.

Declaration

Swift

static func binding<B, C, D>(_ f: () -> Kind<Self, B>,
_ fb: @escaping (B) -> Kind<Self, C>,
_ fc: @escaping (B, C) -> Kind<Self, D>) -> Kind<Self, D>

Parameters

 f 1st closure providing a computation. fb 2nd closure providing a computation. fc 3rd closure providing a computation.

Return Value

Result of evaluating the last computation.

• binding(_:_:_:_:) Extension method

Monad comprehensions for four closures producing computations.

Each closure is expected to provide a computation whose result may be used in subsequent computations. For this reason, the ith closure receives all values generated by the instructions evaluated before it.

Declaration

Swift

static func binding<B, C, D, E>(_ f: () -> Kind<Self, B>,
_ fb: @escaping (B) -> Kind<Self, C>,
_ fc: @escaping (B, C) -> Kind<Self, D>,
_ fd: @escaping (B, C, D) -> Kind<Self, E>) -> Kind<Self, E>

Parameters

 f 1st closure providing a computation. fb 2nd closure providing a computation. fc 3rd closure providing a computation. fd 4th closure providing a computation.

Return Value

Result of evaluating the last computation.

• binding(_:_:_:_:_:) Extension method

Monad comprehensions for five closures producing computations.

Each closure is expected to provide a computation whose result may be used in subsequent computations. For this reason, the ith closure receives all values generated by the instructions evaluated before it.

Declaration

Swift

static func binding<B, C, D, E, G>(_ f: () -> Kind<Self, B>,
_ fb: @escaping (B) -> Kind<Self, C>,
_ fc: @escaping (B, C) -> Kind<Self, D>,
_ fd: @escaping (B, C, D) -> Kind<Self, E>,
_ fe: @escaping (B, C, D, E) -> Kind<Self, G>) -> Kind<Self, G>

Parameters

 f 1st closure providing a computation. fb 2nd closure providing a computation. fc 3rd closure providing a computation. fd 4th closure providing a computation. fe 5th closure providing a computation.

Return Value

Result of evaluating the last computation.

• binding(_:_:_:_:_:_:) Extension method

Monad comprehensions for six closures producing computations.

Each closure is expected to provide a computation whose result may be used in subsequent computations. For this reason, the ith closure receives all values generated by the instructions evaluated before it.

Declaration

Swift

static func binding<B, C, D, E, G, H>(_ f: () -> Kind<Self, B>,
_ fb: @escaping (B) -> Kind<Self, C>,
_ fc: @escaping (B, C) -> Kind<Self, D>,
_ fd: @escaping (B, C, D) -> Kind<Self, E>,
_ fe: @escaping (B, C, D, E) -> Kind<Self, G>,
_ fg: @escaping (B, C, D, E, G) -> Kind<Self, H>) -> Kind<Self, H>

Parameters

 f 1st closure providing a computation. fb 2nd closure providing a computation. fc 3rd closure providing a computation. fd 4th closure providing a computation. fe 5th closure providing a computation. fg 6th closure providing a computation.

Return Value

Result of evaluating the last computation.

• binding(_:_:_:_:_:_:_:) Extension method

Monad comprehensions for seven closures producing computations.

Each closure is expected to provide a computation whose result may be used in subsequent computations. For this reason, the ith closure receives all values generated by the instructions evaluated before it.

Declaration

Swift

static func binding<B, C, D, E, G, H, I>(_ f: () -> Kind<Self, B>,
_ fb: @escaping (B) -> Kind<Self, C>,
_ fc: @escaping (B, C) -> Kind<Self, D>,
_ fd: @escaping (B, C, D) -> Kind<Self, E>,
_ fe: @escaping (B, C, D, E) -> Kind<Self, G>,
_ fg: @escaping (B, C, D, E, G) -> Kind<Self, H>,
_ fh: @escaping (B, C, D, E, G, H) -> Kind<Self, I>) -> Kind<Self, I>

Parameters

 f 1st closure providing a computation. fb 2nd closure providing a computation. fc 3rd closure providing a computation. fd 4th closure providing a computation. fe 5th closure providing a computation. fg 6th closure providing a computation. fh 7th closure providing a computation.

Return Value

Result of evaluating the last computation.

• Monad comprehensions for eight closures producing computations.

Each closure is expected to provide a computation whose result may be used in subsequent computations. For this reason, the ith closure receives all values generated by the instructions evaluated before it.

Declaration

Swift

static func binding<B, C, D, E, G, H, I, J>(_ f : () -> Kind<Self, B>,
_ fb: @escaping (B) -> Kind<Self, C>,
_ fc: @escaping (B, C) -> Kind<Self, D>,
_ fd: @escaping (B, C, D) -> Kind<Self, E>,
_ fe: @escaping (B, C, D, E) -> Kind<Self, G>,
_ fg: @escaping (B, C, D, E, G) -> Kind<Self, H>,
_ fh: @escaping (B, C, D, E, G, H) -> Kind<Self, I>,
_ fi: @escaping (B, C, D, E, G, H, I) -> Kind<Self, J>) -> Kind<Self, J>

Parameters

 f 1st closure providing a computation. fb 2nd closure providing a computation. fc 3rd closure providing a computation. fd 4th closure providing a computation. fe 5th closure providing a computation. fg 6th closure providing a computation. fh 7th closure providing a computation. fi 8th closure providing a computation.

Return Value

Result of evaluating the last computation.

• Monad comprehensions for nine closures producing computations.

Each closure is expected to provide a computation whose result may be used in subsequent computations. For this reason, the ith closure receives all values generated by the instructions evaluated before it.

Declaration

Swift

static func binding<B, C, D, E, G, H, I, J, K>(_ f: () -> Kind<Self, B>,
_ fb: @escaping (B) -> Kind<Self, C>,
_ fc: @escaping (B, C) -> Kind<Self, D>,
_ fd: @escaping (B, C, D) -> Kind<Self, E>,
_ fe: @escaping (B, C, D, E) -> Kind<Self, G>,
_ fg: @escaping (B, C, D, E, G) -> Kind<Self, H>,
_ fh: @escaping (B, C, D, E, G, H) -> Kind<Self, I>,
_ fi: @escaping (B, C, D, E, G, H, I) -> Kind<Self, J>,
_ fj: @escaping (B, C, D, E, G, H, I, J) -> Kind<Self, K>) -> Kind<Self, K>

Parameters

 f 1st closure providing a computation. fb 2nd closure providing a computation. fc 3rd closure providing a computation. fd 4th closure providing a computation. fe 5th closure providing a computation. fg 6th closure providing a computation. fh 7th closure providing a computation. fi 8th closure providing a computation. fj 9th closure providing a computation.

Return Value

Result of evaluating the last computation.