Created
June 25, 2015 11:34
-
-
Save sonsongithub/f1714cbcfcaa7d4aab74 to your computer and use it in GitHub Desktop.
Result<A> meets flatMap
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
| //: Playground - noun: a place where people can play | |
| import UIKit | |
| public enum Result<A> { | |
| case Success(A) | |
| case Failure(NSError) | |
| public init(value:A) { | |
| self = .Success(value) | |
| } | |
| public init(error: NSError?) { | |
| if let error = error { | |
| self = .Failure(error) | |
| } | |
| else { | |
| self = .Failure(NSError(domain: "a", code: 0, userInfo: [:])) | |
| } | |
| } | |
| func open<T>(@noescape ifSuccess ifSuccess: A -> T, @noescape ifFailure: NSError -> T) -> T { | |
| switch self { | |
| case .Success(let value): | |
| return ifSuccess(value) | |
| case .Failure(let value): | |
| return ifFailure(value) | |
| } | |
| } | |
| // func open2<Result>(@noescape ifSuccess ifSuccess: A -> Result, @noescape ifFailure: NSError -> Result) -> Result { | |
| // switch self { | |
| // case .Success(let value): | |
| // return ifSuccess(value) | |
| // case .Failure(let value): | |
| // return ifFailure(value) | |
| // } | |
| // } | |
| func map<B>(@noescape transform: A -> B) -> Result<B> { | |
| return flatMap { .Success(transform($0)) } | |
| } | |
| public func flatMap<B>(@noescape transform: A -> Result<B>) -> Result<B> { | |
| return open( | |
| ifSuccess: transform, | |
| ifFailure: Result<B>.Failure) | |
| } | |
| } | |
| func hoge(input:Any) -> Result<String> { | |
| if let string = input as? String { | |
| return Result(value: string) | |
| } | |
| else { | |
| return Result(error: NSError(domain: "a", code: 0, userInfo: [:])) | |
| } | |
| } | |
| let a:Result<Any> = Result(value:10) | |
| let b:Result<Any> = Result(value:"10") | |
| /// error | |
| a.flatMap(hoge).flatMap({Result(value:"Answer is \($0)")}) | |
| /// OK | |
| b.flatMap(hoge).flatMap({Result(value:"Answer is \($0)")}) | |
| // | |
| //func convertIt<A, T>(input:Result<A>) -> Result<T> { | |
| // switch input { | |
| // case .Success(let x): | |
| // if let value = x as? T { | |
| // return Result(value:value) | |
| // } else { | |
| // return Result(error: NSError(domain: "", code: 0, userInfo: [:])) | |
| // } | |
| // case let .Failure(error): | |
| // return .Failure(error) | |
| // } | |
| //} | |
| // | |
| //func safeSqrt(input:Double) -> Double? { | |
| // return (input > 0) ? sqrt(input) : nil | |
| //} | |
| // | |
| //let k:Int = 10 | |
| // | |
| // | |
| // | |
| //let b:Result<String> = convertIt(a) | |
| //let c:Result<Int> = convertIt(a) | |
| //let array:[Double] = [10, 20, -1] | |
| // | |
| //for value in array.flatMap({safeSqrt($0)}) { | |
| // print(value) | |
| //} | |
| // | |
| //for value in array.map({safeSqrt($0)}) { | |
| // print(value) | |
| //} | |
| // | |
| //let d:Double? = 11 | |
| //let e:Double? = d.flatMap({safeSqrt($0).flatMap{safeSqrt($0)}}) | |
| //print(e) | |
| // | |
| //let f:Double????? = d.map({safeSqrt($0)}) | |
| //print(f) | |
| // | |
| ////func convert<A,B>(t:Result<A>) -> Result<B> { | |
| //// | |
| //// t.map{ | |
| //// switch $0 { | |
| //// case .Success: | |
| //// return Result(error: NSError(domain: "a", code: 0, userInfo: [:])) | |
| //// case .Failure: | |
| //// return Result(error: NSError(domain: "a", code: 0, userInfo: [:])) | |
| //// } | |
| //// } | |
| //// | |
| //// return Result(error: NSError(domain: "a", code: 0, userInfo: [:])) | |
| ////} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment