Skip to content

Instantly share code, notes, and snippets.

@bitops
Created September 4, 2018 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitops/3b614629fa1c4c5894bbe83cdad0e7a8 to your computer and use it in GitHub Desktop.
Save bitops/3b614629fa1c4c5894bbe83cdad0e7a8 to your computer and use it in GitHub Desktop.
// pipe-forward operator
precedencegroup ForwardApplication {
associativity: left
}
infix operator |>: ForwardApplication
func |> <A, B>(a: A, f: (A) -> B) -> B {
return f(a)
}
// non-curried add
func add(a: Int, b: Int) -> Int {
return a + b
}
// curried version of add
func addCurry(a: Int) -> (Int) -> Int {
return { b in
return a + b
}
}
// partial application
let addOne = addCurry(a: 1)
// both of these return 2
1 |> addOne
1 |> addCurry(a: 1)
// returns 3
1 |> addCurry(a: 1) |> addOne
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment