Skip to content

Instantly share code, notes, and snippets.

@sharplet
Created January 13, 2015 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharplet/04ee7fbcc8717f79092b to your computer and use it in GitHub Desktop.
Save sharplet/04ee7fbcc8717f79092b to your computer and use it in GitHub Desktop.
Mapping with methods
/// Convert a free method into a regular funcion accepting an instance as the first parameter.
func method<A, B>(m: A -> () -> B) -> A -> B {
return { m($0)() }
}
let strings = ["1", "2", "3", "foo"]
strings.map(method(String.toInt))
// => [{Some 1}, {Some 2}, {Some 3}, nil]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment