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

"An insight from a functional world: how Elm changes iOS reality" from Riccardo Ferretti

"An insight from a functional world: how Elm changes iOS reality" from Riccardo Ferretti

In this presentation Riccardo shares his experience how Elm-inspired architecture is used for his application written in Swift. As a listener you'll be guided through introduction to Elm, porting it to Swift and what has to be done to achieve this. Riccardo talks about hidden gems on this way, will describe pros and cons of this architecture.

uaMobiTech

July 28, 2016
Tweet

More Decks by uaMobiTech

Other Decks in Programming

Transcript

  1. About me Previously Backend and Web Developing for OSX Principal

    Engineer @ LinkedIn Technical Architect @ AKQA CTO @ Yourvine CTO @ Momentumworks @riccardoferrett
  2. Elm Architecture Update struct Model { ... } enum Action

    {
 case DoThing
 case DoOtherThing
 } func update(Action, Model) -> Model @riccardoferrett
  3. Elm Architecture View struct Model { ... } enum Action

    {
 case DoThing
 case DoOtherThing
 } func update(Action, Model) -> Model func view(Model) -> Html<Action> @riccardoferrett
  4. Counter Fractal Model Update View Pair of Counters Model Update

    View Counter Model Update View @riccardoferrett
  5. Counter Fractal Model Update View Pair of Counters Model Update

    View Counter Model Update View Model Update View App Other Thing Model Update View @riccardoferrett
  6. Elm

  7. Elm p1 = {x = 5, y = 3, z

    = 1} p2 = { p1 |
 x = p1.x + 1,
 y = 7
 } Immutable Data @riccardoferrett
  8. Swift structs Immutable struct Point {
 let x: Int
 let

    y: Int
 let z: Int
 } let p1 = Point(x:5, y:3, z:1) p1.x = p1.x + 1 p1.y = 7 let p2 = Point(
 x:p1.x+1, y:7, z:p1.z
 ) @riccardoferrett
  9. Swift structs Immutable struct Point: Immutable {
 let x: Int


    let y: Int
 let z: Int
 } // codegen let p2 = p1
 .set(x: p1.x+1)
 .set(y: 7) @riccardoferrett
  10. Swift structs Equatable struct Person {
 let first: String
 let

    last: String
 } let p1 = Person(...) let p2 = Person(...) let isEqual = p1 == p2 @riccardoferrett
  11. Swift structs Equatable struct Person: Equatable {
 let first: String


    let last: String
 } func ==(lhs: Person, rhs: Person) … let p1 = Person(...) let p2 = Person(...) let isEqual = p1 == p2 @riccardoferrett
  12. Swift structs Equatable struct Person: AutoEquatable {
 let first: String


    let last: String
 } // codegen let p1 = Person(...) let p2 = Person(...) let isEqual = p1 == p2 @riccardoferrett
  13. Swift structs Diffable struct Person: AutoDiffable {
 let first: String


    let last: String
 } // codegen let p1 = Person(...) let p2 = Person(...) let diff = p1.diff(p2) @riccardoferrett
  14. UIKit @riccardoferrett typealias Instance = (UIView, Support) func create() ->

    Instance func render(
 instance: Instance, 
 model: Model, 
 dispatch: Action ->()) -> Void Declarative UI Our “Solution”
  15. Component Template @riccardoferrett protocol Component {
 associatedtype Model
 associatedtype Action

    associatedtype Support associatedtype View associatedtype Instance = (View, Support) static func create() -> Instance static func render(Instance, Model, Action ->()) -> Void static func update(Action, Model) -> Model }
  16. Component Template @riccardoferrett protocol Component {
 associatedtype Model
 associatedtype Action

    associatedtype Support associatedtype View associatedtype Instance = (View, Support) static func create() -> Instance static func render(Instance, Model, Action ->()) -> Void static func update(Action, Model) -> Model }
  17. Elm → Swift > Component based > Immutable data >

    Single state tree > Unidirectional data flow > Pure functions @riccardoferrett
  18. Credits and Notes Rheese @rheeseyb Mark @itchingpixels Sean @seanparsons Krzysztof

    @merowing_ ReSwift https://github.com/ReSwift/ReSwift SourceKitten https://github.com/jpsim/SourceKitten Stencil https://github.com/kylef/Stencil GenKit https://github.com/momentumworks/GenKit