Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Composability with Swift

Junior B.
October 01, 2015

Composability with Swift

Composing things has radically changed from Objective-C, these slides are showing how we can compose things in safer way.

Junior B.

October 01, 2015

More Decks by Junior B.

Other Decks in Programming


  1. – Terry Pratchett “It is important that we know where

    we come from, because if you do not know where you come from, you don't know where you're going.”
  2. ;

  3. Objective-C • We compose statements • We tell the machine

    what to do step by step (Imperative) • We manage states • We perform side effects all the time
  4. Objective-C Composability MyVar *foo = [MyVar new]; MyVar *bar =

    [MyVar new]; [foo doThis]; [bar doThat]; [blabla doBlaBla]; //...
  5. Async Objective-C dispatch_async(queue, ^{ // do something if (!error) {

    dispatch_async(queue, ^{ if (!error) { dispatch_async(dispatch_get_main_queue(), ^{ // UPDATE UI }); } //... }); } else { // display error } });
  6. What is a Function? • In Swift functions are First

    Class Citizens • A function is an ordinary element • First class function means that they are like Int, Double, Array, etc…
  7. What can I do composing data? • Create complex algorithms

    combining small reusable functions • Asynchronous programming • Error handling • …there’s not enough space to list all the things!
  8. In a Nutshell • Composing data means that I can

    compose types (structs or classes) with functions • The composition of different entities is achieve taking advantage of Generics • Composing things will make the compiler the first checker for errors
  9. Example 1: Result • A type Result can hold either

    a value or an error • Results can be composed to create a chain • Errors are propagated to the chain • The type checker will prevent type mismatch!
  10. Example 2: Future • A type Future is a placeholder

    for a value that is still not available, but that will be (Latency) • Futures can be composed to create an asynchronous chain • Errors are propagated to the chain • The origin of the term is a proposal for the call-by- future evaluation method
  11. Future / Promise • A Future is a read-only placeholder

    • A Promise is a write-able placeholder • A Promise is a subclass of Future
  12. Conclusions • Composing data is a very good WAY to

    control complexity • Big features can be create composing small chunks of code (bricks) • Code is more readable and more maintainable
  13. Where to go from here • Composition with Future and

    Promise is very powerful, but has limits (ex: resource management) • Future and Promise are single value, what about multiple values? (sequences)
  14. The Reactive Way • Reactive Programming is the NATURAL evolution

    of data composition • Asynchronous Programming is easier and more fun using the Reactive style!
  15. Good Resources After This RxSwift SideEffects.xyz (yeah, that’s a domain…)

    Brian Beckman: Don't fear the Monad Erik Meijer: What does it mean to be Reactive? Ash Furrow: Functional Reactive Awesomeness With Swift!