Created
June 11, 2014 12:56
-
-
Save tLewisII/809cdc25c05d54e2550b to your computer and use it in GitHub Desktop.
Function composition in 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
operator infix .. { | |
associativity right | |
} | |
func ..<A, B, C>(f: B -> C, g: A -> B) -> A -> C { | |
return { (a: A) -> C in | |
return f(g(a)) | |
} | |
} | |
let array = [1,2,3,4] | |
let mapped = array.map({$0 + 1} .. {$0 + 1} .. {$0 + 1}) | |
mapped // [4,5,6,7] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment