Created
June 7, 2014 19:00
-
-
Save mbrandonw/c5bc1a84f4c749a87b0f to your computer and use it in GitHub Desktop.
Generic function composition in Swift
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
@infix func * <A,B,C> (g: B -> C, f: A -> B) -> (A) -> C { | |
return {(x: A) -> C in | |
return g(f(x)) | |
} | |
} | |
var f = {(x: Int) -> Float in | |
return Float(x * x) / 2.0 | |
} | |
var g = {(x: Float) -> Float in | |
return 1.0 / x | |
} | |
(atanf * g * f)(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment