Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Esta charla es una monada - Introducción a FP en JavaScript con Ramda

173 views

Published on

Muchos desarrolladores quieren acercarse al mundo de la programación funcional en su lenguaje favorito (¡JavaScript, por supuesto!), pero se encuentran con un mundo lleno de palabras raras, como "mónada", "cálculo lambda", "funciones de alto orden", "transparencia referencial" o "efectos colaterales". En esta charla, desmitificaremos cada una de estas palabras, conoceremos las ventajas de la programación funcional con respecto a otras metodologías y veremos algunos ejemplos prácticos con la librería de utilidades más usada (Ramda).

Published in: Software
  • Be the first to comment

  • Be the first to like this

Esta charla es una monada - Introducción a FP en JavaScript con Ramda

  1. 1. José Manuel Lucas @jmlweb Esta charla es una monada Introducción a FP en JavaScript con Ramda
  2. 2. HOLA TENERIFE! I’m José Manuel Lucas
  3. 3. I live in Murcia I’m José Manuel Lucas
  4. 4. Who am I? I’m José Manuel Lucas •Frontendnstein at SEWAN Spain. •JS lover & FP wannabe. •Almograte (Almogrote advocate). •Ramda is our FP utility lib of choice, because of its simplicity, documentation and community.
  5. 5. https://github.com/ jmlweb/jsdaycan2019
  6. 6. Monads - A monad is just a monoid in the category of endofunctors.
  7. 7. Keep calm and code fp! - I only know basic maths!
  8. 8. Functional Programming Concepts
  9. 9. Immutability objects/arrays are copied by reference
  10. 10. Immutability objects/arrays are copied by reference
  11. 11. Immutability Create a new object/array for updates Object.assignDestructuring
  12. 12. Side effects Dependency on external state/vars Unreliable for other parts of your application depending on the same state.
  13. 13. Side effects Dependency on external state/vars Added difficulty in tests. You need to know the context (how many times the function has been called, the initial value…)
  14. 14. Side effects Dependency on external state/vars You need to wrap your tests with mocks for dates and other unpredictable stuff.
  15. 15. Pure functions Depend only on arguments received Good for testing!
  16. 16. Higher Order Functions Receive or return a function myArr.map(v => v * 2) HOF
  17. 17. Composing functions “Chain” the results of functions in reverse order
  18. 18. Piping functions “Chain” the results of functions in normal order
  19. 19. Point free / tacit style Omitting args when they are not needed myArr.map(v => doSomething(v)) myArr.map(doSomething)
  20. 20. Currying
  21. 21. Currying Allowing a function with x args to be called also like x functions with 1 arg
  22. 22. Partial application Creating a new function as a result of applying some arguments to the original one
  23. 23. But… not everything can be pure!
  24. 24. Not everything can be pure But we can isolate side effects - Creating pure functions which receives the side effect as argument, so everything becomes predictable. - Making our functions resilient, returning default values instead of throwing errors, checking types, if a property or method exists given an object…
  25. 25. Let’s get ready to Ramda!
  26. 26. Ramda Utils library (like lodash) but based on FP concepts - Plain JS data (collections = arrays, structures = objects). - All ramda functions are automatically curried and produce immutable results. - The data to be operated with usually is provided as latest arg. - Enforces piping/composing (pipe and compose functions)
  27. 27. Ramda Point free versions of native functions - Strings (match, replace, split, test, toLower, toUpper, trim, toString). - Lists (filter, map, reduce, forEach, join, reverse, length, indexOf, includes) - Objects (keys, values)
  28. 28. Ramda Math functions - Operations (add, divide, multiply, subtract, mathMod, modulo). - Utils (dec, inc, negate) - Lists (product, sum) - Median & mean (mean, median)
  29. 29. Ramda __ - Special placeholder for “gaps” in curried functions
  30. 30. Ramda tap - Useful to perform side effects in a safer way. Executes the function received as first argument, but returns the original value.
  31. 31. Ramda defaultTo - Returns the value defined in the first arg if the second one is null or undefined.
  32. 32. Ramda prop, nth, path - Get a value from an object or list in a safe way (Maybe)
  33. 33. Ramda head, last, tail, init - head: Get first item. - last: Get last item. - tail: Every item except first. - init: Every item except last.
  34. 34. Ramda adjust, evolve - Update a given index of a list, or some keys of an object with the provided function(s)
  35. 35. Ramda update, assoc - Set a given index of a list, or a key of an object with the provided value
  36. 36. Ramda remove, drop, dropLast - remove: Receives the starting index as first arg, the number of items to remove as second, and the array as third. - drop: Removes x items of the array from the start. - dropLast: Removes x items of the array from the end.
  37. 37. Ramda dissoc, path - Remove a key or a nested path from an object
  38. 38. Ramda applySpec - Creates an object by applying functions to the source provided
  39. 39. Ramda juxt - Creates an array by applying functions to the source provided
  40. 40. Ramda always, identity, T, F - always: Receives an arg and returns a function that always returns that arg, ignoring the new arg received. - identity: Returns again the argument received. - T: Returns true, ignoring the arg received. - F: Returns false, ignoring the arg received.
  41. 41. Ramda both, either - both: Receives two predicates as args. Returns true when both predicates are truthy. - either: Receives two predicates as args. Returns true when any predicate is truthy.
  42. 42. Ramda complement - Receives a function as argument. When called, it will return true for falsy values, and false for truthy values.
  43. 43. Ramda not - It returns true for falsy values, and false for truthy values.
  44. 44. Ramda ifElse (Left/Right Monad) - Receives a predicate and two functions. If the predicate returns true for the argument, the first function is applied. Otherwise, the second is the one applied.
  45. 45. Ramda cond - Maps predicates to calc functions. Both are applied to the args received. If no predicate matches, it returns undefined.
  46. 46. Ramda cond - Maps predicates to calc functions. Both are applied to the args received. If no predicate matches, it returns undefined. T forces to return something
  47. 47. Ramda where - Validates objects in a declarative way.
  48. 48. Ramda converge - Executes the functions defined in the second arg agains the value provided. Then, it merges the results with the function provided as first arg.
  49. 49. Ramda apply - Executes the function defined in the first arg, with the params defined in the second arg (array).
  50. 50. Time for some demos! - stateManagerExample - promisesExample - parseDataExample
  51. 51. Thank you!

×