Functions

The following functions are available globally.

  • Return the result of evaluating a function f with argument(s) a.

    Declaration

    Swift

    public func pipe<A, B>(_ a: A, _ f: (A) -> B) -> B

    Parameters

    a

    Value to be passed to f.

    f

    Function to evaluate.

    Return Value

    f(a)

  • Infix pipe operator.

    See more

    Declaration

    Swift

    public func |><A, B>(a: A, f: (A) -> B) -> B

    Parameters

    a

    Value to be passed to f.

    f

    Function to evaluate.

    Return Value

    f(a)

  • Returns a new function which returns the result of evaluating a function g with the result of evaluating another function f.

    Declaration

    Swift

    public func compose<A, B, C>(_ f: @autoclosure @escaping () -> (A) -> B,
                                 _ g: @autoclosure @escaping () -> (B) -> C) -> (A) -> C

    Parameters

    f

    First function to be called.

    g

    Second function to be called.

    Return Value

    A function which returns the result of calling g with the output of calling f with the function’s input: g(f($0)).

  • Infix compose operator.

    See more

    Declaration

    Swift

    public func >>><A, B, C>(_ f: @escaping (A) -> B,
                             _ g: @escaping (B) -> C) -> (A) -> C

    Parameters

    f

    First function to be called.

    g

    Second function to be called.

    Return Value

    A function which returns the result of calling g with the output of calling f with the function’s input: g(f($0)).

  • A function which returns its argument.

    Declaration

    Swift

    public func identity<A>(_ a: A) -> A

    Parameters

    a

    value to return.

    Return Value

    a.

  • A function which ignores its argument.

    Declaration

    Swift

    public func ignore<A>(_: A) -> Void

    Parameters

    a

    value to ignore.

  • Create a curried version of a two-argument function.

    Declaration

    Swift

    public func curry<A, B, C>(_ f: @autoclosure @escaping () -> (A, B) -> C) -> (A) -> (B) -> C

    Parameters

    f

    function to curry.

    Return Value

    a chain of functions each taking a single argument.

  • Create a curried version of a three-argument function.

    Declaration

    Swift

    public func curry<A, B, C, D>(_ f: @autoclosure @escaping () -> (A, B, C) -> D) -> (A) -> (B) -> (C) -> D

    Parameters

    f

    function to curry.

    Return Value

    a chain of functions each taking a single argument.

  • Create a curried version of a four-argument function.

    Declaration

    Swift

    public func curry<A, B, C, D, E>(_ f: @autoclosure @escaping () -> (A, B, C, D) -> E) -> (A) -> (B) -> (C) -> (D) -> E

    Parameters

    f

    function to curry.

    Return Value

    a chain of functions each taking a single argument.

  • Create a function accepting an ignored argument.

    Declaration

    Swift

    public func uncurry<A, B>(_ f: @autoclosure @escaping () -> () -> B) -> (A) -> B

    Parameters

    f

    function to uncurry.

    Return Value

    A function which ignores its argument, returning f().

  • Convert a curried function into one that takes two arguments.

    Declaration

    Swift

    public func uncurry<A, B, C>(_ f: @autoclosure @escaping () -> (A) -> (B) -> C) -> (A, B) -> C

    Parameters

    f

    function to uncurry.

    Return Value

    A function taking two arguments.

  • Convert a curried function into one that takes three arguments.

    Declaration

    Swift

    public func uncurry<A, B, C, D>(_ f: @autoclosure @escaping () -> (A) -> (B) -> (C) -> D) -> (A, B, C) -> D

    Parameters

    f

    function to uncurry.

    Return Value

    A function taking three arguments.

  • Convert a curried function into one that takes four arguments.

    Declaration

    Swift

    public func uncurry<A, B, C, D, E>(_ f: @autoclosure @escaping () -> (A) -> (B) -> (C) -> (D) -> E) -> (A, B, C, D) -> E

    Parameters

    f

    function to uncurry.

    Return Value

    A function taking four arguments.

  • Reverse the order of a two-argument function’s arguments.

    Declaration

    Swift

    public func reverse<A, B, C>(_ f: @autoclosure @escaping () -> (A, B) -> C) -> (B, A) -> C

    Parameters

    f

    a function.

    Return Value

    a function which accepts arguments in the reverse order.

  • Reverse the order of a three-argument function’s arguments.

    Declaration

    Swift

    public func reverse<A, B, C, D>(_ f: @autoclosure @escaping () -> (A, B, C) -> D) -> (C, B, A) -> D

    Parameters

    f

    a function.

    Return Value

    a function which accepts arguments in the reverse order.

  • Reverse the order of a four-argument function’s arguments.

    Declaration

    Swift

    public func reverse<A, B, C, D, E>(_ f: @autoclosure @escaping () -> (A, B, C, D) -> E) -> (D, C, B, A) -> E

    Parameters

    f

    a function.

    Return Value

    a function which accepts arguments in the reverse order.

  • Reverse the order of a two-curry function.

    Declaration

    Swift

    public func reverse<A, B, C>(_ f: @autoclosure @escaping () -> (A) -> (B) -> C) -> (B) -> (A) -> C

    Parameters

    f

    a function.

    Return Value

    a function which accepts curried arguments in the reverse order.

  • Reverse the order of a three-curry function.

    Declaration

    Swift

    public func reverse<A, B, C, D>(_ f: @autoclosure @escaping () -> (A) -> (B) -> (C) -> D) -> (C) -> (B) -> (A) -> D

    Parameters

    f

    a function.

    Return Value

    a function which accepts curried arguments in the reverse order.

  • Reverse the order of a four-curry function.

    Declaration

    Swift

    public func reverse<A, B, C, D, E>(_ f: @autoclosure @escaping () -> (A) -> (B) -> (C) -> (D) -> E) -> (D) -> (C) -> (B) -> (A) -> E

    Parameters

    f

    a function.

    Return Value

    a function which accepts curried arguments in the reverse order.

  • bind creates a function that passes a Result‘s success value to another function, while errors are passed through unchanged.

    Declaration

    Swift

    public func bind<A, B, C>(_ f: @autoclosure @escaping () -> (A) -> Result<B, C>) -> (Result<A, C>) -> Result<B, C>

    Parameters

    f

    a function which maps a value to a Result.

    Return Value

    a function which maps one Result to another.

  • Infix version of f composed with bind. Returns a function that maps a value through both f and g.

    See more

    Declaration

    Swift

    public func >=><A, B, C, D>(_ f: @autoclosure @escaping () -> (A) -> Result<B, D>,
                                _ g: @autoclosure @escaping () -> (B) -> Result<C, D>) -> (A) -> Result<C, D>

    Parameters

    f

    function mapping a value to a Result.

    g

    function mapping a value to a Result.

    Return Value

    the result of g if f and g are both successful, a failure otherwise.

  • Creates a switched function that always returns success.

    Declaration

    Swift

    public func turnout<A, B, C>(_ f: @autoclosure @escaping () -> (A) -> B) -> (A) -> Result<B, C>

    Parameters

    f

    function being evaluated.

    Return Value

    a function mapping a value to a result via f.

  • Recover form a failure by mapping a failure to success.

    Declaration

    Swift

    public func turnin<A, B>(_ f: @autoclosure @escaping () -> (B) -> A) -> (Result<A, B>) -> A

    Parameters

    f

    function mapping the Result type to something else.

    Return Value

    a function mapping a Result to a value via f.

  • Recover form a failure by re-mapping to success.

    Declaration

    Swift

    public func turnin<A, B, C>(_ f: @autoclosure @escaping () -> (B) -> Result<A, C>) -> (Result<A, B>) -> Result<A, C>

    Parameters

    f

    function mapping the Result type to something else.

    Return Value

    a function mapping a Result to a value via f.

  • Pipes a value to another function, returning the original value instead.

    Declaration

    Swift

    public func tee<A, B>(_ f: @autoclosure @escaping () -> (A) -> B) -> (A) -> A

    Parameters

    f

    function to evaluate.

    Return Value

    a function which maps a value to itself, passig it to f first.

  • Pipes a value to another function, returning the original value instead.

    This version may throw.

    Declaration

    Swift

    public func tee<A, B>(_ f: @autoclosure @escaping () -> (A) throws -> B) -> (A) throws -> A

    Parameters

    f

    function to evaluate.

    Return Value

    a function which maps a value to itself, passig it to f first.

  • Map both the success and failure values of a Result to different values.

    Declaration

    Swift

    public func bimap<A, B, C, D>(_ success: @autoclosure @escaping () -> (A) -> B,
                                  _ failure: @autoclosure @escaping () -> (C) -> D) -> (Result<A, C>) -> Result<B, D>

    Parameters

    success

    function evaluated if the Result succeeded.

    failure

    function evaluated if the Result failed.

    Return Value

    a function mapping one Result to another.

  • Map both the successful value of a Result to a different value.

    Declaration

    Swift

    public func map<A, B, C>(_ f: @autoclosure @escaping () -> (A) -> B) -> (Result<A, C>) -> Result<B, C>

    Parameters

    success

    function evaluated if the Result succeeded.

    Return Value

    a function mapping one Result to another.

  • Create a function that interprets a thrown error as a failed result.

    Declaration

    Swift

    public func tryCatch<A, B>(_ f: @autoclosure @escaping () -> (A) throws -> B) -> (A) -> Result<B, Error>

    Parameters

    f

    function evaluated with the passed-in value.

    Return Value

    a function mapping a value to a Result.

  • Evaluate a function returning an optional value, returning a failure if nil was returned, converting an optional to a two-way track.

    Declaration

    Swift

    public func unwrap<A, B>(_ f: @autoclosure @escaping () -> (A) -> B?) -> (A) -> Result<B, Nil>

    Parameters

    f

    function to evaluate.

    Return Value

    a function mapping a value to a Result.

  • Unwrap an optional value, returning a Result, converting an optional to a two-way track.

    Declaration

    Swift

    public func unwrap<A>(_ a: A?) -> Result<A, Nil>

    Parameters

    a

    value to attempt to unwrap.

    Return Value

    a Result indicating whether or not the unwrapping was successful.