Created
June 4, 2014 01:30
-
-
Save ayanonagon/47916cff9b1e86f1c19c to your computer and use it in GitHub Desktop.
Function application using Swift
This file contains 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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice partial application. Can it be extrapolated to work with unlimited arguments, though? For instance, typical implementations of partial application would permit:
(It's pretty clear that the original version of
fp()
only applies to dyadic functions.)