Skip to content

Instantly share code, notes, and snippets.

@mbrandonw
Created June 7, 2014 19:00
Show Gist options
  • Save mbrandonw/c5bc1a84f4c749a87b0f to your computer and use it in GitHub Desktop.
Save mbrandonw/c5bc1a84f4c749a87b0f to your computer and use it in GitHub Desktop.
Generic function composition in Swift
@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