Created
November 20, 2022 15:44
-
-
Save boraseoksoon/2c00fa3e5151d1f39aaf60cac874a885 to your computer and use it in GitHub Desktop.
Swift operator overloading example (pipe)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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