Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Last active June 6, 2018 05:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JadenGeller/0ef1372f7a20c885fd65 to your computer and use it in GitHub Desktop.
Save JadenGeller/0ef1372f7a20c885fd65 to your computer and use it in GitHub Desktop.
Swift Chain Function
/*
* 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