What is Functional Programming? (In a nutshell) • It's a style not only a paradigm • Programming with first class functions • Modularization of the software • Avoid side effects (where possible) • Immutability (where possible) 2
Crockford's law In addition to it begin useful, it is 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
Example (Initial) func refreshData() { var error : NSErrorPointer = 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>? if let l = self.list { self.title = "Lobsters (Hottest \(l.count))" self.tableView.reloadData() } } } 6
Crash Free - Version 2 func refreshData() { let url = "https://lobste.rs/hottest.json" if let result = retrieveData(url) >>= self.deserializeData { updateUI(result) } } 13
With a Result type! public enum Result { case Error(NSError) case Value(Box) // 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
FlatMap for Result // Our FlatMap operator for Result type! public func >>= (a: Result, f: VA -> Result) -> Result { switch a { case let .Error(l): return .Error(l) case let .Value(r): return f(r.value) } } 20
What is the result? (Version 3) func refreshData() { let url = "https://lobste.rs/hottest.json" (retrieveData(url) >>= deserializeData) >>- updateUI } 21
Version 3 (1/2) func retrieveData(stringURL: String) -> Result { var error : NSError?; var response: NSURLResponse? let contentRequest = NSURLRequest(URL: NSURL(string: stringURL)!) if let data = NSURLConnection.sendSynchronousRequest(contentRequest, returningResponse: &response, error: &error) { return Result(data) } return Result(error!) } 22
What is a Future? A Future is an object holding 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
What is a Promise? A Promise is an "extension" of 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
Promise public class Promise : Future { // Fulfill the promise successfully public func success (v: T) { //... } // Process with failure public func failure (e: NSError) { //... } } 32
Final Version func refreshData() { let url = "https://lobste.rs/hottest.json" /// 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
What can be built with Monads? (Examples) • A Reactive Framework, for combining streams • A DSL for SQLite, creating type safe queries • A toolset to deal with collections (e.g. LINQ) 38
Next? • Brian Beckman: Don't fear the Monad (video) • Functional Programming in Swift (eBook) • Functional Programming (Coursera) • Principles of Reactive Programming (Coursera) 39