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).
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.
12. Side effects
Dependency on external state/vars
Unreliable for other parts of your application depending on
the same state.
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. Side effects
Dependency on external state/vars
You need to wrap your tests with mocks for dates and
other unpredictable stuff.
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…
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)
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.
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. 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. Ramda
complement
- Receives a function as argument. When called, it will return true
for falsy values, and false for truthy values.
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. Ramda
cond
- Maps predicates to calc functions. Both are applied to the args
received. If no predicate matches, it returns undefined.
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
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. Ramda
apply
- Executes the function defined in the first arg, with the params
defined in the second arg (array).
50. Time for some demos!
- stateManagerExample
- promisesExample
- parseDataExample