Weekly Swift articles, podcasts and tips by John Sundell.

Combining a sequence of functions

Published on 20 May 2017

Here's how a sequence of operations can be chained together, by combining their functions into a single closure:

internal func +<A, B, C>(lhs: @escaping (A) throws -> B,
                         rhs: @escaping (B) throws -> C) -> (A) throws -> C {
    return { try rhs(lhs($0)) }
}

public func run() throws {
    try (determineTarget + build + analyze + output)()
}

In functional languages, the above is often implemented as the pipe forward (|>) operator.