Skip to content

Instantly share code, notes, and snippets.

@listrophy
Last active August 29, 2015 14:02
Show Gist options
  • Save listrophy/c74e841b01cc820c0a27 to your computer and use it in GitHub Desktop.
Save listrophy/c74e841b01cc820c0a27 to your computer and use it in GitHub Desktop.
composeAndApply in swift
func composeAndApply<T>(funcs: (T -> T)[], val: T) -> T {
return funcs.reduce(val, combine: { (memo, aFunc) in aFunc(memo) })
}
func square(val: Int) -> Int {
return val * val
}
func double(val: Int) -> Int {
return val * 2
}
func addSome(a: Int) -> (Int -> Int) {
return {(val: Int) -> Int in val + a }
}
composeAndApply([square, double, addSome(3)], 5) // => 53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment