Selective

public protocol Selective : Applicative

The Selective typeclass represents Selective Applicative Functors, described in this paper. Selective Applicative Functors enrich Applicative Functors by providing an operation that composes two effectful computations where the second depends on the first.

  • Conditionally applies the second computation based on the result of the first.

    Declaration

    Swift

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

    Parameters

    fab

    A computation that results in an Either value.

    f

    A computation that is executed in case the first computation evaluates to a left value.

    Return Value

    Composition of the two computations.

  • whenS(_:then:) Extension method

    Evaluates the second computation when the first evaluates to true.

    Declaration

    Swift

    static func whenS(
        _ cond: Kind<Self, Bool>,
        then f: Kind<Self, ()>) -> Kind<Self, ()>

    Parameters

    cond

    A computation evaluating to a boolean value.

    then

    A computation that will be evaluated if the first computation evaluates to true.

    Return Value

    Composition of the two computations.

  • Evaluates one out of two computations based on the result of another computation.

    Declaration

    Swift

    static func branch<A, B, C>(
        _ fab: Kind<Self, Either<A, B>>,
        ifLeft fa: Kind<Self, (A) -> C>,
        ifRight fb: Kind<Self, (B) -> C>) -> Kind<Self, C>

    Parameters

    fab

    Computation producing an Either value, to decide which computation is executed afterwards.

    ifLeft

    Computation that will be executed if fab evaluates to an Either.left value.

    ifRight

    Computation that will be executed if fab evaluates to an Either.right value.

    Return Value

    Composition of the computations.

  • ifS(_:then:else:) Extension method

    Evaluates one out of two computations based on the result of another computation.

    Declaration

    Swift

    static func ifS<A>(
        _ x: Kind<Self, Bool>,
        then t: Kind<Self, A>,
        else e: Kind<Self, A>) -> Kind<Self, A>

    Parameters

    x

    Computation producing a boolean value to decide which computation is exectured afterwards.

    then

    Computation that will be executed if the first evaluates to true.

    else

    Computation that will be executed if the first evaluates to false.

    Return Value

    Composition of the computations.

  • orS(_:_:) Extension method

    A lifted version of lazy boolean or.

    Declaration

    Swift

    static func orS(
        _ x: Kind<Self, Bool>,
        _ y: Kind<Self, Bool>) -> Kind<Self, Bool>

    Parameters

    x

    Computation to be or'ed.

    y

    Computation to be or'ed.

    Return Value

    Result of the or operation on the two computations.

  • andS(_:_:) Extension method

    A lifted version of lazy boolean and.

    Declaration

    Swift

    static func andS(
        _ x: Kind<Self, Bool>,
        _ y: Kind<Self, Bool>) -> Kind<Self, Bool>

    Parameters

    x

    Computation to be and'ed.

    y

    Computation to be and'ed.

    Return Value

    Result of the and operation on the two computations.

  • fromOptionS(_:_:) Extension method

    Evaluates an optional computation, providing a default value for the empty case.

    Declaration

    Swift

    static func fromOptionS<A>(
        _ x: Kind<Self, A>,
        _ mx: Kind<Self, Option<A>>) -> Kind<Self, A>

    Parameters

    x

    Default value for the empty case.

    mx

    A computation resulting in an optional value.

    Return Value

    Composition of the two computations.

  • anyS(_:_:) Extension method

    A lifted version of any. Retains the short-circuiting behavior.

    Declaration

    Swift

    static func anyS<A>(
        _ p: @escaping (A) -> Kind<Self, Bool>,
        _ array: ArrayK<A>) -> Kind<Self, Bool>

    Parameters

    p

    A lifted predicate to find any element of the array that matches it.

    array

    An array to look for an element that matches the predicate in.

    Return Value

    A boolean computation describing if any element of the array matches the predicate.

  • allS(_:_:) Extension method

    A lifted version of all. Retains the short-circuiting behavior.

    Declaration

    Swift

    static func allS<A>(
        _ p: @escaping (A) -> Kind<Self, Bool>,
        _ array: ArrayK<A>) -> Kind<Self, Bool>

    Parameters

    p

    A lifted predicate to check all elements of the array match it.

    array

    An array to check if all elements match the predicate.

    Return Value

    A boolean computation describing if all elements of the array match the predicate.

  • whileS(_:) Extension method

    Evaluates a computation as long as it evaluates to true.

    Declaration

    Swift

    static func whileS(_ x: Kind<Self, Bool>) -> Eval<Kind<Self, ()>>

    Parameters

    x

    A computation.

    Return Value

    A potentially lazy computation.