Skip to content

Instantly share code, notes, and snippets.

@dtchepak
Last active August 29, 2015 14:02
Show Gist options
  • Save dtchepak/57eefc01ac08c5fa2116 to your computer and use it in GitHub Desktop.
Save dtchepak/57eefc01ac08c5fa2116 to your computer and use it in GitHub Desktop.
Function compsition in swift (based on https://twitter.com/jckarter/status/473642680782573569)
operator infix • { associativity right }
@infix func •<A,B,C> (f : B->C, g : A->B) -> (A->C) {
return { (x : A) in f(g(x)) }
}
func inc(a : Int) -> Int { return a+1 }
func show(a : Int) -> String { return String(a) }
let f = show • inc • inc
@dtchepak
Copy link
Author

dtchepak commented Feb 7, 2015

6.1.1

infix operator • { associativity right }
func •<A,B,C> (f : B->C, g : A->B) -> (A->C) {
    return  { (x : A) in f(g(x)) }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment