Comonad

public protocol Comonad : Functor

A Comonad is the dual of a Monad. It provides capabilities to compose functions that extract values from their context.

Implementations of this instance must obey the following laws:

extract(duplicate(fa)) == fa
map(fa, f) == coflatMap(fa, { a in f(extract(a)) }
coflatMap(fa, extract) == fa
extract(coflatMap(fa, f)) == f(fa)
  • Applies a value in the context implementing this instance to a function that takes a value in a context, and returns a normal value.

    This function is the dual of Monad.flatMap.

    Declaration

    Swift

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

    Parameters

    fa

    Value in the context implementing this instance.

    f

    Extracting function.

    Return Value

    The result of extracting and transforming the value, in the context implementing this instance.

  • Extracts the value contained in the context implementing this instance.

    This function is the dual of Monad.pure (via Applicative).

    Declaration

    Swift

    static func extract<A>(_ fa: Kind<Self, A>) -> A

    Parameters

    fa

    A value in the context implementing this instance.

    Return Value

    A normal value.

  • pair() Extension method

    Provides a Pairing for this Comonad and its dual Monad.

    Declaration

    Swift

    static func pair() -> Pairing<Self, CoPartial<Self>>

    Return Value

    A Pairing for this Comonad and its dual Monad.

  • duplicate(_:) Extension method

    Wraps a value in another layer of the context implementing this instance.

    Declaration

    Swift

    static func duplicate<A>(_ fa: Kind<Self, A>) -> Kind<Self, Kind<Self, A>>

    Parameters

    fa

    A value in the context implementing this instance.

    Return Value

    Value wrapped in another context layer.