style not only a paradigm • Programming with first class functions • Modularization of the software • Avoid side effects (where possible) • Immutability (where possible) 2
also cursed and the curse of the monad is that once you get the epiphany, once you understand - "oh that's what it is" - you lose the ability to explain it to anybody. — Doug Crockford 4
nil var response: NSURLResponse? let contentRequest = NSURLRequest(URL: NSURL(string: "https://lobste.rs/hottest.json")!) let data = NSURLConnection.sendSynchronousRequest(contentRequest, returningResponse: &response, error: error) if let d = data { self.list = NSJSONSerialization.JSONObjectWithData(d, options: nil, error: error) as! Array<Dictionary<String, AnyObject>>? if let l = self.list { self.title = "Lobsters (Hottest \(l.count))" self.tableView.reloadData() } } } 6
case Value(Box<V>) // A Box is a workaround to avoid // compilation failure public init(_ e: NSError?, _ v: V) { if let ex = e { self = Result.Error(ex) } else { self = Result.Value(Box(v)) } } // [...] } 19
public func >>= <VA, VB>(a: Result<VA>, f: VA -> Result<VB>) -> Result<VB> { switch a { case let .Error(l): return .Error(l) case let .Value(r): return f(r.value) } } 20
a value which may become available at some point and: • If the runnable block has not yet completed, we say that the Future is not completed. • If the runnable block has completed with a value or an exception, we say that the Future is completed. 26
Future, with the following differences: • A Promise "can" return a Future (not the opposite) • A Promise can or cannot be fulfilled • A Promise is read/write 27
this is a convenience global function to create a Future future { return self.retrieveData(url) }.then { data in return self.deserializeData(data) }.onSuccess { items in self.updateUI(items) }.onFailure { e in NSLog(e.description) } } 33