Skip to content

Instantly share code, notes, and snippets.

@kostiakoval
Created November 29, 2014 11:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kostiakoval/3a58332a1ccf0beff520 to your computer and use it in GitHub Desktop.
Save kostiakoval/3a58332a1ccf0beff520 to your computer and use it in GitHub Desktop.
Haskel Fmap in Swift. Get optiona value and call function with that values as parameter
infix operator <^> { associativity left }
func <^><A, B>(f: A -> B, a: A?) -> B? {
switch a {
case .Some(let x): return f(x)
case .None: return .None
}
}
func increment(someNumber: Int) -> Int {
return someNumber + 1
}
increment <^> 5 // Some 6
increment <^> nil
let s = { $0 + 1 } <^> 10
let s0 = { a in a + 1 } <^> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment