Skip to content

Instantly share code, notes, and snippets.

@Ben-G
Created April 27, 2016 14:27
Show Gist options
  • Save Ben-G/e3db922bd13d3dbae6562e3ece41f0eb to your computer and use it in GitHub Desktop.
Save Ben-G/e3db922bd13d3dbae6562e3ece41f0eb to your computer and use it in GitHub Desktop.
Function as Functor in Swift
func map<A,B,C>(f1: (B) -> C, f2: (A) -> B) -> (A) -> C {
return { f1(f2($0)) }
}
func addOne(i: Int) -> Int {
return i + 1
}
func convertToString(i: Int) -> String {
return String(i)
}
let f = map(convertToString, f2: addOne)
f(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment