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 plusThree(addend: Int) -> Int { | |
| return addend + 3 | |
| } | |
| Optional.Some(2).map(plusThree) | |
| // => .Some(5) |
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(2).map { $0 + 3 } | |
| // => .Some(5) |
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 map<U>(f: T -> U) -> U? { | |
| switch self { | |
| 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
| Optional.None.map { $0 + 3 } | |
| // => .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
| let post = Post.findByID(1) | |
| if post != nil { | |
| return post.title | |
| } else { | |
| return nil | |
| } |
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
| findPost(1).map(getPostTitle) |
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>(f: T -> U, a: T?) -> U? { | |
| return a.map(f) | |
| } | |
| getPostTitle <^> findPost(1) |
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
| map({ $0 + 2 }, { $0 + 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
| typealias IntFunction = Int -> Int | |
| func map(f: IntFunction, _ g: IntFunction) -> IntFunction { | |
| return { x in f(g(x)) } | |
| } | |
| let foo = map({ $0 + 2 }, { $0 + 3 }) | |
| foo(10) | |
| // => 15 |
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
| extension Optional { | |
| func apply<U>(f: (T -> U)?) -> U? { | |
| switch f { | |
| case .Some(let someF): return self.map(someF) | |
| case .None: return .None | |
| } | |
| } | |
| } | |
| extension Array { |