Articles, podcasts and news about Swift development, 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.

Support Swift by Sundell by checking out this sponsor:

Raycast

Raycast: Take the macOS Spotlight experience to the next level: Create Jira issues, manage GitHub pull requests and control other tools with a few keystrokes. Easily automate every-day tasks and boost your developer productivity by downloading Raycast for free.