Last active
August 29, 2015 14:02
-
-
Save listrophy/c74e841b01cc820c0a27 to your computer and use it in GitHub Desktop.
composeAndApply 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
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