Skip to content

Instantly share code, notes, and snippets.

@tLewisII
Created June 11, 2014 12:56
Show Gist options
  • Save tLewisII/809cdc25c05d54e2550b to your computer and use it in GitHub Desktop.
Save tLewisII/809cdc25c05d54e2550b to your computer and use it in GitHub Desktop.
Function composition in Swift
operator infix .. {
associativity right
}
func ..<A, B, C>(f: B -> C, g: A -> B) -> A -> C {
return { (a: A) -> C in
return f(g(a))
}
}
let array = [1,2,3,4]
let mapped = array.map({$0 + 1} .. {$0 + 1} .. {$0 + 1})
mapped // [4,5,6,7]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment