Skip to content

Instantly share code, notes, and snippets.

@ayanonagon
Created June 4, 2014 01:30
Show Gist options
  • Save ayanonagon/47916cff9b1e86f1c19c to your computer and use it in GitHub Desktop.
Save ayanonagon/47916cff9b1e86f1c19c to your computer and use it in GitHub Desktop.
Function application using Swift
func fp<A, B, C>(fn:((A, B) -> C), arg:A) -> (B -> C) {
func apply(b:B) -> C {
return fn(arg, b)
}
return apply
}
func add(x: Int, y: Int) -> Int {
return x + y
}
let addThree = fp(add, 3)
addThree(0)
addThree(1)
addThree(2)
addThree(3)
@00dani
Copy link

00dani commented Jun 4, 2014

Nice partial application. Can it be extrapolated to work with unlimited arguments, though? For instance, typical implementations of partial application would permit:

func add(x: Int, y: Int, z: Int) -> Int { return x + y + z }
fp(add, 1)(2, 3) == (1 + 2 + 3)
// and
fp(fp(add, 1), 2)(3) == (1 + 2 + 3)
// or even
fp(add, 1, 2)(3) == (1 + 2 + 3)

(It's pretty clear that the original version of fp() only applies to dyadic functions.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment