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
| Optional(20) >>- half >>- half >>- half | |
| // => .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
| // For Optional | |
| func >>-<T, U>(a: T?, f: T -> U?) -> U? | |
| // For Array | |
| func >>-<T, U>(a: [T], f: T -> [U]) -> [U] |
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
| Optional(3) >>- half | |
| // .None | |
| Optional(4) >>- half | |
| // 2 | |
| Optional.None >>- half | |
| // .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 half(a: Int) -> Int? { | |
| return a % 2 == 0 ? a / 2 : .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
| infix operator >>- { associativity left } | |
| func >>-<T, U>(a: T?, f: T -> U?) -> U? { | |
| return a.flatMap(f) | |
| } |
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 curriedTimes(a: Int)(b: Int) -> Int { | |
| return a * b | |
| } | |
| curriedTimes <^> Optional(5) <*> Optional(3) |
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
| curriedAddition <^> Optional(2) <*> Optional(3) |
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 curriedAddition(a: Int)(b: Int) -> Int { | |
| return a + b | |
| } | |
| curriedAddition <^> Optional(2) <^> Optional(3) | |
| // => COMPILER ERROR: Value of optional type '(Int -> Int)? not unwrapped; did you mean to use '!' or '??' |
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
| [ { $0 + 3 }, { $0 * 2 } ] <*> [1, 2, 3] | |
| // => [ 4, 5, 6, 2, 4, 6 ] |
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
| Optional.Some({ $0 + 3 }) <*> Optional.Some(2) | |
| // => 5 |