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

Go Pure with ClojureScript

Go Pure with ClojureScript

Marcelo Piva

July 13, 2019
Tweet

More Decks by Marcelo Piva

Other Decks in Programming

Transcript

  1. ?

  2. Pure functions let eat = (food) => { let foodList

    = [, , , ] if(foodList.includes(food)) { return } else { return } }
  3. Impure functions let eat = (food) => { let foodList

    = [, , , ] if(foodList.includes(food)) { console.log() } else { console.log() } } Not testable, depends on the world
  4. Class Object Instance Inheritance Polymorphism Abstract Factory Builder Factory Method

    Object Pool Prototype Singleton Adapter Bridge Composite Decorator Facade Flyweight Private Class Data Proxy Chain of responsibility Command Interpreter Iterator Mediator Memento Null Object Observer State
  5. First class functions ✅ let temperatures = [10, 20, 30]

    let celsius2fahrenheit = (t) => t * 1.8 + 32 temperatures.map(celsius2fahrenheit) // => [ 50, 68, 86 ]
  6. First class functions ✅ let temperatures = [10, 20, 30]

    let celsius2fahrenheit = (t) => t * 1.8 + 32 temperatures.map(celsius2fahrenheit) // => [ 50, 68, 86 ]
  7. First class functions ✅ let temperatures = [10, 20, 30]

    let celsius2fahrenheit = (t) => t * 1.8 + 32 temperatures.map(celsius2fahrenheit) // => [ 50, 68, 86 ]
  8. First class functions ✅ let temperatures = [10, 20, 30]

    let celsius2fahrenheit = (t) => t * 1.8 + 32 temperatures.map(celsius2fahrenheit) // => [ 50, 68, 86 ]
  9. Immutability ❌ let account = {id: "acc-1", status: "active"} let

    account2blocked = (acc) => { acc.status = "blocked" return acc } account2blocked(account) // => {id: "acc-1", status: "blocked"} account // => {id: "acc-1", status: "blocked"}
  10. Immutability ❌ let account = {id: "acc-1", status: "active"} let

    account2blocked = (acc) => { acc.status = "blocked" return acc } account2blocked(account) // => {id: "acc-1", status: "blocked"} account // => {id: "acc-1", status: "blocked"}
  11. Immutability ❌ let account = {id: "acc-1", status: "active"} let

    account2blocked = (acc) => { acc.status = "blocked" return acc } account2blocked(account) // => {id: "acc-1", status: "blocked"} account // => {id: "acc-1", status: "blocked"}
  12. Immutability ❌ let account = {id: "acc-1", status: "active"} let

    account2blocked = (acc) => { acc.status = "blocked" return acc } account2blocked(account) // => {id: "acc-1", status: "blocked"} account // => {id: "acc-1", status: "blocked"}
  13. Immutability ❌ let account = {id: "acc-1", status: "active"} let

    account2blocked = (acc) => { acc.status = "blocked" return acc } account2blocked(account) // => {id: "acc-1", status: "blocked"} account // => {id: "acc-1", status: "blocked"}
  14. Immutability ❌ let account = {id: "acc-1", status: "active"} let

    account2blocked = (acc) => { acc.status = "blocked" return acc } account2blocked(account) // => {id: "acc-1", status: "blocked"} account // => {id: "acc-1", status: "blocked"}
  15. const? const foo = {bar: 1} foo = {bar: 2}

    // => SyntaxError: Identifier 'foo' has already been declared foo.bar = 2 foo // => {bar: 2}
  16. const? const foo = {bar: 1} foo = {bar: 2}

    // => SyntaxError: Identifier 'foo' has already been declared foo.bar = 2 foo // => {bar: 2}
  17. const? const foo = {bar: 1} foo = {bar: 2}

    // => SyntaxError: Identifier 'foo' has already been declared foo.bar = 2 foo // => {bar: 2}
  18. Immutability ❌ let account = {id: "acc-1", status: "active"} let

    account2blocked = (acc) => { return {...acc, status: "blocked"} } account2blocked(account) // => {id: "acc-1", status: "blocked"} account // => {id: "acc-1", status: "active"}
  19. Immutability ❌ let account = {id: "acc-1", status: "active"} let

    account2blocked = (acc) => { return {...acc, status: "blocked"} } account2blocked(account) // => {id: "acc-1", status: "blocked"} account // => {id: "acc-1", status: "active"}
  20. Immutability ❌ let account = {id: "acc-1", status: "active"} let

    account2blocked = (acc) => { return {...acc, status: "blocked"} } account2blocked(account) // => {id: "acc-1", status: "blocked"} account // => {id: "acc-1", status: "active"}
  21. Immutability ❌ let account = {id: "acc-1", status: "active"} let

    account2blocked = (acc) => { return {...acc, status: "blocked"} } account2blocked(account) // => {id: "acc-1", status: "blocked"} account // => {id: "acc-1", status: "active"}
  22. Immutability ❌ let account = {id: "acc-1", status: "active"} let

    account2blocked = (acc) => { return {...acc, status: "blocked"} } account2blocked(account) // => {id: "acc-1", status: "blocked"} account // => {id: "acc-1", status: "active"}
  23. First class functions ✅ (def temperatures [10 20 30]) (defn

    celsius->fahrenheit [t] (+ 32 (* t 1.8))) (map celsius->fahrenheit temperatures) ; => [50.0 68.0 86.0]
  24. First class functions ✅ (def temperatures [10 20 30]) (defn

    celsius->fahrenheit [t] (+ 32 (* t 1.8))) (map celsius->fahrenheit temperatures) ; => [50.0 68.0 86.0]
  25. First class functions ✅ (def temperatures [10 20 30]) (defn

    celsius->fahrenheit [t] (+ 32 (* t 1.8))) (map celsius->fahrenheit temperatures) ; => [50.0 68.0 86.0]
  26. First class functions ✅ (def temperatures [10 20 30]) (defn

    celsius->fahrenheit [t] (+ 32 (* t 1.8))) (map celsius->fahrenheit temperatures) ; => [50.0 68.0 86.0]
  27. Immutability ✅ (def account {:id "acc-1" :status :active}) (defn account->blocked

    [acc] (assoc acc :status :blocked) (account->blocked account) ; => {:id "acc-1" :status :blocked} account ; => {:id "acc-1" :status :active}
  28. Immutability ✅ (def account {:id "acc-1" :status :active}) (defn account->blocked

    [acc] (assoc acc :status :blocked) (account->blocked account) ; => {:id "acc-1" :status :blocked} account ; => {:id "acc-1" :status :active}
  29. Immutability ✅ (def account {:id "acc-1" :status :active}) (defn account->blocked

    [acc] (assoc acc :status :blocked) (account->blocked account) ; => {:id "acc-1" :status :blocked} account ; => {:id "acc-1" :status :active}
  30. Immutability ✅ (def account {:id "acc-1" :status :active}) (defn account->blocked

    [acc] (assoc acc :status :blocked) (account->blocked account) ; => {:id "acc-1" :status :blocked} account ; => {:id "acc-1" :status :active}
  31. Immutability ✅ (def account {:id "acc-1" :status :active}) (defn account->blocked

    [acc] (assoc acc :status :blocked) (account->blocked account) ; => {:id "acc-1" :status :blocked} account ; => {:id "acc-1" :status :active}
  32. ?