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

7MASTERS Programação Funcional - Javascript Funcional Com Ramda

7MASTERS Programação Funcional - Javascript Funcional Com Ramda

Ana Luiza Portello

July 25, 2018
Tweet

More Decks by Ana Luiza Portello

Other Decks in Programming

Transcript

  1. ANA LUIZA BASTOS
    github.com/anabastos
    @naluhh
    @anapbastos
    Software Developer na Quanto e
    cientista da computação na
    PUC-SP
    anabastos.me

    View Slide

  2. JAVASCRIPT FUNCIONAL COM
    RAMDA

    View Slide

  3. JS pode ser usado
    como uma
    linguagem
    funcional?

    View Slide

  4. Javascript tem funções de
    primeira ordem (HOF)

    View Slide

  5. Quando falamos de funcional
    nessas linguagens falamos de
    “Mantenabilidade”
    “Menos bugs”
    “Declaratividade”

    View Slide

  6. JS vem aos poucos
    adotando elementos de
    linguagens funcionais

    View Slide

  7. PROPOSALS

    View Slide

  8. Flatmap
    Partial Application
    Pipeline operator
    Pattern Matching

    View Slide

  9. RAMDA

    View Slide

  10. Biblioteca que foi pensada
    para tornar mais fácil o
    javascript funcional

    View Slide

  11. ● 100% imutável
    ● ganho em performance
    ● legibilidade
    ● point-free / tacit programming

    View Slide

  12. ● Lists(map, filter, reduce, contains, replace,
    passAll, crop, flatten, find)
    ● Maths(inc, add, mean, sum)
    ● String(split, replace)
    ● Logics(equals, cond, not)
    ● Relation(intersection, clamp, gt, lt)
    ● Functions(curry, pipe, compose, ifElse, etc)

    View Slide

  13. TODAS AS FUNÇÕES
    SÃO CURRIED

    View Slide

  14. const add = (x, y) => x + y
    add(1, 2); // 3
    add(1) // Error

    View Slide

  15. const add = x => y => x + y
    add(1, 2); // 3
    add(1) // Function

    View Slide

  16. R.add(1, 2); // 3
    R.add(1) // Function

    View Slide

  17. CURRY
    (PROPOSAL PARTIAL
    APPLICATION)

    View Slide

  18. const curryAdd = R.curry((x, y) => x + y))
    curryAdd(1, 2); // 3
    curryAdd(1)(2); // 3

    View Slide

  19. PIPE / COMPOSE
    (PROPOSAL PIPELINE OPERATOR)

    View Slide

  20. PIPE / COMPOSE:
    compõe funções de forma
    sequencial
    function1 function2
    INPUT OUTPUT

    View Slide

  21. EXEMPLINHO

    View Slide

  22. Happy Hour:
    ● Soma o consumido
    ● Adiciona os 10%
    ● Divide entre os colegas

    View Slide

  23. const conta = [9, 9, 9, 9, 15]

    View Slide

  24. const divideConta = R.pipe(
    R.sum, // 51
    R.mul(1.1), // 56,1
    R.divide(6) // 9,35
    )
    divideConta(conta)

    View Slide

  25. PIPE
    COMPOSE

    View Slide

  26. const ListOfItems = R.compose(
    Box,
    List,
    ListItems
    )([{...}, {...}, {...}])

    View Slide

  27. PIPEP / COMPOSEP

    View Slide

  28. then().then().then().then()

    View Slide

  29. const asyncStuff = R.pipeP(
    getId,
    getUserById,
    asyncStuff
    )
    // Promise

    View Slide

  30. IFELSE
    (maybe monad)

    View Slide

  31. const findName = R.ifElse(
    R.has('name'),
    R.prop('name'),
    R.always('no name'),
    )({ id: 123, name: “ana”})
    // “ana”

    View Slide

  32. FLATTER
    (PROPOSAL FLATMAP)

    View Slide

  33. R.flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, 12]]]]);
    // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

    View Slide

  34. DATA HANDLING

    View Slide

  35. EVOLVE

    View Slide

  36. {
    name: “Ana”
    email: “[email protected]”,
    phone: “11 98698-3010”,
    address: “rua da aparecida 304 apt34 ”,
    }

    View Slide

  37. R.evolve({
    email: R.toLower,
    phone: R.replace(‘-’, ‘’),
    })(data)
    // {
    // name: “Ana”
    // email: “[email protected]”,
    // phone: “11 9869983010”
    // address: ...
    // }

    View Slide

  38. APPLY SPEC

    View Slide

  39. const createObj = R.applySpec({
    counter: R.inc,
    userData: { phone: R.trim},
    })
    createObj(0, “ 98499-1900”)
    // {1, userData{ phone: “9849919000”}}

    View Slide

  40. TryCatch
    Cond
    Memoization
    Lenses

    View Slide

  41. BOM TOOLBOX

    View Slide

  42. COOKBOOK

    View Slide

  43. ● Rambda - github.com/selfrefactor/rambda
    ● Ramda-fantasy - github.com/ramda/ramda-fantasy
    ● Thinking Ramda -
    randycoulman.com/blog/2016/05/24/thinking-in-ramda-getting
    -started
    ● Ramda - Derek Stavis(Pagar.me talks)

    View Slide

  44. OBRIGADA :)
    speakerdeck.com/anabastos
    anabastos.me
    github.com/anabastos
    @naluhh
    @anapbastos

    View Slide