Created
November 6, 2014 08:39
-
-
Save jseanj/b0c8321eb3bde21963f5 to your computer and use it in GitHub Desktop.
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
| import UIKit | |
| typealias JSON = AnyObject | |
| typealias JSONArray = [JSON] | |
| struct Page { | |
| let title: String | |
| } | |
| enum PageListResult { | |
| case Success([Page]) | |
| case Failure(NSError) | |
| } | |
| enum Result<A> { | |
| case Success(Box<A>) | |
| case Failure(NSError) | |
| } | |
| class Box<T> { | |
| let value: T | |
| init(_ value: T) { | |
| self.value = value | |
| } | |
| } | |
| func asJSON(data: NSData) -> Result<JSON> { | |
| var error: NSError? | |
| let json: JSON? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: &error) | |
| switch (json, error) { | |
| case (_, .Some(let error)): return .Failure(error) | |
| case (.Some(let json), _): return .Success(Box(json)) | |
| default: | |
| return .Failure(NSError()) | |
| } | |
| } | |
| func asJSONArray(json: JSON) -> Result<JSONArray> { | |
| if let array = json as? JSONArray { | |
| return .Success(Box(array)) | |
| } else { | |
| return .Failure(NSError()) | |
| } | |
| } | |
| func secondElement(array: JSONArray) -> Result<JSON> { | |
| if array.count < 2 { | |
| return .Failure(NSError()) | |
| } else { | |
| return .Success(Box(array[1])) | |
| } | |
| } | |
| func asStringList(element: JSON) -> Result<[String]> { | |
| if let array = element as? [String] { | |
| return .Success(Box(array)) | |
| } else { | |
| return .Failure(NSError()) | |
| } | |
| } | |
| func asPages(stringList: [String]) -> Result<[Page]> { | |
| return .Success(Box(stringList.map { | |
| Page(title: $0) | |
| })) | |
| } | |
| func pagesFromData2(data: NSData) -> Result<[Page]> { | |
| return test(asJSON(data)) { | |
| test(asJSONArray($0)) { | |
| test(secondElement($0)) { | |
| test(asStringList($0)) { | |
| asPages($0) | |
| } | |
| } | |
| } | |
| } | |
| } | |
| func test<T, U>(input: Result<T>, f: T->Result<U>) -> Result<U> { | |
| switch input { | |
| case .Success(let box): | |
| return f(box.value) | |
| case .Failure(let error): | |
| return .Failure(error) | |
| } | |
| } | |
| let box = Box(12) | |
| var result: Result = .Success(box) | |
| //test(result) { | |
| // x in | |
| // | |
| //} | |
| func pagesFromData(data: NSData) -> PageListResult { | |
| // parse NSData to JSON | |
| var error: NSError? | |
| let json: JSON? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: &error) | |
| if let json: JSON = json { | |
| if let array = json as? JSONArray { | |
| if array.count < 2 { | |
| return .Failure(NSError()) | |
| } | |
| let element: JSON = array[1] | |
| if let titles = element as? [String] { | |
| return .Success(titles.map { Page(title: $0) }) | |
| } else { | |
| return .Failure(NSError()) | |
| } | |
| } | |
| else { | |
| return .Failure(NSError()) | |
| } | |
| } | |
| else if let error = error { | |
| return .Failure(error) | |
| } | |
| else { | |
| fatalError("Received neither JSON nor an error") | |
| return .Failure(NSError()) | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment