Skip to content

Instantly share code, notes, and snippets.

@boraseoksoon
Created November 20, 2022 15:44
Show Gist options
  • Save boraseoksoon/2c00fa3e5151d1f39aaf60cac874a885 to your computer and use it in GitHub Desktop.
Save boraseoksoon/2c00fa3e5151d1f39aaf60cac874a885 to your computer and use it in GitHub Desktop.
Swift operator overloading example (pipe)
import Foundation
precedencegroup ForwardPipe {
associativity: left
}
infix operator |> : ForwardPipe
func |> <V, F>(v: V, f: ((V) -> F)) -> F {
f(v)
}
func add(v: Int) -> Int { v + 3 }
func multiply(v: Int) -> Int { v * 3 }
let res1 = multiply(v: (add(v: multiply(v: add(v: 2)))))
let res2 = 2 |> add |> multiply |> add |> multiply
res1 // 54
res2 // 54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment