Functor

public protocol Functor : Invariant

A Functor provides a type with the ability to transform its value type into another type, while preserving its structure.

Using the encoding for HKTs in Bow, in the type Kind<F, A>, A is the value type and F represents the structure of the type. An instance of Functor for F allows to transform A into another type, while maintaining F unchanged.

  • Creates a new value transforming the type using the provided function, preserving the structure of the original type.

    The implementation of this function must obey two laws:

    1. Preserve identity:

      map(fa, id) == fa
      
    2. Preserve composition:

      map(map(fa, f), g) == map(fa, compose(g, f))
      

    Declaration

    Swift

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

    Parameters

    fa

    A value in the context of the type implementing this instance of Functor.

    f

    A transforming function.

    Return Value

    The result of transforming the value type using the provided function, maintaining the structure of the original value.

  • hylo(_:_:_:) Extension method

    Undocumented

    Declaration

    Swift

    static func hylo<A, B>(_ algebra : @escaping Algebra<Self, Eval<B>>,
                           _ coalgebra : @escaping Coalgebra<Self, A>,
                           _ a : A) -> B
  • imap(_:_:_:) Extension method

    Declaration

    Swift

    static func imap<A, B>(
        _ fa: Kind<Self, A>,
        _ f: @escaping (A) -> B,
        _ g: @escaping (B) -> A) -> Kind<Self, B>
  • lift(_:) Extension method

    Given a function, provides a new function lifted to the context type implementing this instance of Functor.

    Declaration

    Swift

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

    Parameters

    f

    Function to be lifted.

    Return Value

    Function in the context implementing this instance of Functor.

  • void(_:) Extension method

    Replaces the value type by the Void type.

    Declaration

    Swift

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

    Parameters

    fa

    Value to be transformed.

    Return Value

    New value in the context implementing this instance of Functor, with Void as value type.

  • fproduct(_:_:) Extension method

    Transforms the value type and pairs it with its original value.

    Declaration

    Swift

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

    Parameters

    fa

    Value to be transformed.

    f

    Transforming function.

    Return Value

    A pair with the original value and its transformation, in the context of the original value.

  • as(_:_:) Extension method

    Transforms the value type with a constant value.

    Declaration

    Swift

    static func `as`<A, B>(
        _ fa: Kind<Self, A>,
        _ b: B) -> Kind<Self, B>

    Parameters

    fa

    Value to be transformed.

    b

    Constant value to replace the value type.

    Return Value

    A new value with the structure of the original value, with its value type transformed.

  • tupleLeft(_:_:) Extension method

    Transforms the value type by making a tuple with a new constant value to the left of the original value type.

    Declaration

    Swift

    static func tupleLeft<A, B>(
        _ fa: Kind<Self, A>,
        _ b: B) -> Kind<Self, (B, A)>

    Parameters

    fa

    Value to be transformed.

    b

    Constant value for the tuple.

    Return Value

    A new value with the structure of the original value, with a tuple in its value type.

  • tupleRight(_:_:) Extension method

    Transforms the value type by making a tuple with a new constant value to the right of the original value type.

    Declaration

    Swift

    static func tupleRight<A, B>(
        _ fa: Kind<Self, A>,
        _ b: B) -> Kind<Self, (A, B)>

    Parameters

    fa

    Value to be transformed.

    b

    Constant value for the tuple.

    Return Value

    A new value with the structure of the original value, with a tuple in its value type.