Last active
June 6, 2018 05:37
-
-
Save JadenGeller/0ef1372f7a20c885fd65 to your computer and use it in GitHub Desktop.
Swift Chain Function
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
/* | |
* chain takes in any number of 1-argument functions as arguments | |
* and returns a function that executes each function in order | |
*/ | |
func chain<T>(all: T -> () ...) -> T -> () { | |
return { x in | |
for f in all { f(x) } | |
} | |
} | |
// Example | |
var stack: [Int] | |
chain(stack.append, println)(10) | |
// is equivalent to | |
stack.append(10) | |
println(10) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment