Skip to content

Instantly share code, notes, and snippets.

@cocoaNib
Last active April 1, 2019 13:58
Show Gist options
  • Save cocoaNib/adde68e73f5788d984737a8420893c8f to your computer and use it in GitHub Desktop.
Save cocoaNib/adde68e73f5788d984737a8420893c8f to your computer and use it in GitHub Desktop.
Operator ForwardPipe
import UIKit
// Definition
infix operator |> : ForwardPipePrecedence
precedencegroup ForwardPipePrecedence {
associativity: left
higherThan: AssignmentPrecedence
}
public func |> <T, U> (x: T, f: (T) -> U) -> U {
return f(x)
}
// Usage
func multiply(x: Int) -> Int {
return x * 2
}
func addTen(x: Int) -> Int {
return x + 10
}
let number = 3
var result = number |> addTen |> multiply
result = number |> multiply |> addTen
// Further Reading
// https://nshipster.com/swift-operators/
// https://github.com/apple/swift/ ... /stdlib/public/core/Policy.swift
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment