Created
November 29, 2014 11:17
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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