Skip to content

Instantly share code, notes, and snippets.

@dudarenko-io
Created June 30, 2017 15:02
Show Gist options
  • Save dudarenko-io/79da3c7e301c2c233e2e9c827c3604b0 to your computer and use it in GitHub Desktop.
Save dudarenko-io/79da3c7e301c2c233e2e9c827c3604b0 to your computer and use it in GitHub Desktop.
Functions are functors too. Map function implemented to concatenate functions
func map<T,S,U>(_ f: @escaping (T)->S, _ g: @escaping (S)->U) -> (T) -> U {
return {x in g(f(x)) }
}
let countNumberOfDigits = map( { (n: Int) in "\(n)" }, { (str: String) in str.characters.count } )
countNumberOfDigits(12345)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment