SlideShare uma empresa Scribd logo
1 de 51
Baixar para ler offline
José Manuel Lucas @jmlweb
Esta charla es una monada
Introducción a FP en
JavaScript con Ramda
HOLA TENERIFE!
I’m José Manuel Lucas
I live in Murcia
I’m José Manuel Lucas
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.
https://github.com/
jmlweb/jsdaycan2019
Monads
- A monad is just a monoid in the category of endofunctors.
Keep calm and code fp!
- I only know basic maths!
Functional Programming Concepts
Immutability
objects/arrays are copied by reference
Immutability
objects/arrays are copied by reference
Immutability
Create a new object/array for updates
Object.assignDestructuring
Side effects
Dependency on external state/vars
Unreliable for other parts of your application depending on
the same state.
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…)
Side effects
Dependency on external state/vars
You need to wrap your tests with mocks for dates and
other unpredictable stuff.
Pure functions
Depend only on
arguments received
Good for testing!
Higher Order Functions
Receive or return a function
myArr.map(v => v * 2)
HOF
Composing functions
“Chain” the results of functions in reverse order
Piping functions
“Chain” the results of functions in normal order
Point free / tacit style
Omitting args when they are not needed
myArr.map(v => doSomething(v))
myArr.map(doSomething)
Currying
Currying
Allowing a function with x args to be called also like
x functions with 1 arg
Partial application
Creating a new function as a result of applying some
arguments to the original one
But… not everything can be pure!
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…
Let’s get ready to Ramda!
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)
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)
Ramda
Math functions
- Operations (add, divide, multiply, subtract, mathMod,
modulo).
- Utils (dec, inc, negate)
- Lists (product, sum)
- Median & mean (mean, median)
Ramda
__
- Special placeholder for “gaps” in curried functions
Ramda
tap
- Useful to perform side effects in a safer way. Executes the
function received as first argument, but returns the original value.
Ramda
defaultTo
- Returns the value defined in the first arg if the second one is null
or undefined.
Ramda
prop, nth, path
- Get a value from an object or list in a safe way (Maybe)
Ramda
head, last, tail, init
- head: Get first item.
- last: Get last item.
- tail: Every item except first.
- init: Every item except last.
Ramda
adjust, evolve
- Update a given index of a list, or some keys of an object with
the provided function(s)
Ramda
update, assoc
- Set a given index of a list, or a key of an object with the
provided value
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.
Ramda
dissoc, path
- Remove a key or a nested path from an object
Ramda
applySpec
- Creates an object by applying functions to the source provided
Ramda
juxt
- Creates an array by applying functions to the source provided
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.
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.
Ramda
complement
- Receives a function as argument. When called, it will return true
for falsy values, and false for truthy values.
Ramda
not
- It returns true for falsy values, and false for truthy values.
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.
Ramda
cond
- Maps predicates to calc functions. Both are applied to the args
received. If no predicate matches, it returns undefined.
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
Ramda
where
- Validates objects in a declarative way.
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.
Ramda
apply
- Executes the function defined in the first arg, with the params
defined in the second arg (array).
Time for some demos!
- stateManagerExample
- promisesExample
- parseDataExample
Thank you!

Mais conteúdo relacionado

Mais procurados

Functional programming in scala
Functional programming in scalaFunctional programming in scala
Functional programming in scala
Stratio
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)
Vu Tran Lam
 
STC 2016 Programming Language Storytime
STC 2016 Programming Language StorytimeSTC 2016 Programming Language Storytime
STC 2016 Programming Language Storytime
Sarah Kiniry
 

Mais procurados (20)

camel-scala.pdf
camel-scala.pdfcamel-scala.pdf
camel-scala.pdf
 
Functional programming in scala
Functional programming in scalaFunctional programming in scala
Functional programming in scala
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
 
What is the deal with Elixir?
What is the deal with Elixir?What is the deal with Elixir?
What is the deal with Elixir?
 
JavaScript: Core Part
JavaScript: Core PartJavaScript: Core Part
JavaScript: Core Part
 
Definitions of Functional Programming
Definitions of Functional ProgrammingDefinitions of Functional Programming
Definitions of Functional Programming
 
Control structures functions and modules in python programming
Control structures functions and modules in python programmingControl structures functions and modules in python programming
Control structures functions and modules in python programming
 
Intro to Scala
 Intro to Scala Intro to Scala
Intro to Scala
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8
 
It's All About Morphisms
It's All About MorphismsIt's All About Morphisms
It's All About Morphisms
 
C++ metaprogramming
C++ metaprogrammingC++ metaprogramming
C++ metaprogramming
 
Java 8 lambdas expressions
Java 8 lambdas expressionsJava 8 lambdas expressions
Java 8 lambdas expressions
 
Operators & Casts
Operators & CastsOperators & Casts
Operators & Casts
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
The Awesome Python Class Part-5
The Awesome Python Class Part-5The Awesome Python Class Part-5
The Awesome Python Class Part-5
 
The Awesome Python Class Part-3
The Awesome Python Class Part-3The Awesome Python Class Part-3
The Awesome Python Class Part-3
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)
 
STC 2016 Programming Language Storytime
STC 2016 Programming Language StorytimeSTC 2016 Programming Language Storytime
STC 2016 Programming Language Storytime
 

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

Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love Game
Antony Stubbs
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
Hiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
Hiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
Hiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
Hiroshi Ono
 

Semelhante a Esta charla es una monada - Introducción a FP en JavaScript con Ramda (20)

Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love Game
 
Is there a perfect data-parallel programming language? (Experiments with More...
Is there a perfect data-parallel programming language? (Experiments with More...Is there a perfect data-parallel programming language? (Experiments with More...
Is there a perfect data-parallel programming language? (Experiments with More...
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Programming Android Application in Scala.
Programming Android Application in Scala.Programming Android Application in Scala.
Programming Android Application in Scala.
 
MODEL OF A PROGRAM AS MULTITHREADED STOCHASTIC AUTOMATON AND ITS EQUIVALENT T...
MODEL OF A PROGRAM AS MULTITHREADED STOCHASTIC AUTOMATON AND ITS EQUIVALENT T...MODEL OF A PROGRAM AS MULTITHREADED STOCHASTIC AUTOMATON AND ITS EQUIVALENT T...
MODEL OF A PROGRAM AS MULTITHREADED STOCHASTIC AUTOMATON AND ITS EQUIVALENT T...
 
Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: Notes
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
BayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore HaskellBayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore Haskell
 
FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)
 
Applicative style programming
Applicative style programmingApplicative style programming
Applicative style programming
 
New c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_ii
 
Scala - core features
Scala - core featuresScala - core features
Scala - core features
 
Introduction to OpenMP
Introduction to OpenMPIntroduction to OpenMP
Introduction to OpenMP
 
Free your lambdas
Free your lambdasFree your lambdas
Free your lambdas
 
AI Programming language (LISP)
AI Programming language (LISP)AI Programming language (LISP)
AI Programming language (LISP)
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 

Último

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Último (20)

Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 

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