SlideShare a Scribd company logo
Globalcode – Open4education
Ramda
Derek Stavis
Software Engineer
Globalcode – Open4education
Who?
Derek Stavis
github.com/derekstavis
Software Engineer
Ruby, JavaScript, Python, C
Node, React & Webpack Advocate
Globalcode – Open4education
Have you been using functional
helper libraries?
Globalcode – Open4education
Underscore?
🤔
Globalcode – Open4education
Lodash?
🤔
Globalcode – Open4education
Really?
🙄
Globalcode – Open4education
Those are really good libraries
Globalcode – Open4education
Weren't designed with a functional
mindset from day 0
Globalcode – Open4education
Ramda
A functional programming library
http://ramdajs.com
Globalcode – Open4education
Meet Ramda
Emphasizes a purer functional style
Immutability and side-effect free functions
Ramda functions are automatically curried
Argument ordering convenient to currying
Keep rightmost the data to be operated
Globalcode – Open4education
Small, single-responsibility and
reusable functions
Globalcode – Open4education
Ramda shines on currying
Globalcode – Open4education
Ramda shines on currying
var names = [
{ name: 'Pablo' },
{ name: 'Escobar' },
{ name: 'Gaviria' }
]
var getName = R.prop('name')
var pickNames = R.map(getName, names)
pickNames()
=> [ 'Pablo', 'Escobar', 'Gaviria' ]
Globalcode – Open4education
Ramda shines on currying
var names = [
{ name: 'Pablo' },
{ name: 'Escobar' },
{ name: 'Gaviria' }
]
var getName = R.prop('name')
var pickNames = R.map(getName)
pickNames(names)
=> [ 'Pablo', 'Escobar', 'Gaviria' ]
Globalcode – Open4education
Currying?
😨
Globalcode – Open4education
Currying was named after
Haskell Curry
One of the first to investigate such technique
Globalcode – Open4education
Turn a function that expects
multiple arguments into one that,
when supplied fewer arguments,
returns a new function that awaits
the remaining ones
Globalcode – Open4education
Ramda is about currying
const cookPasta = R.curry((water, heat, pasta) => { ... })
// Have everything needed
cookPasta (water, heat, pasta)
// Forgot to buy pasta
cookPasta (water, heat)(pasta)
// No gas and no pasta
cookPasta (water)(heat, pasta)
// Had to go twice to supermarket
cookPasta (water)(heat)(pasta)
Globalcode – Open4education
This is truly powerful
Globalcode – Open4education
Ramda is all curried
😎
Globalcode – Open4education
Point-free programming
🖖
Globalcode – Open4education
Ramda is all curried
R.add(2, 3) //=> 5
R.add(7)(10) //=> 17
R.contains(2, [1, 2, 3]) //=> true
R.contains(1)([1, 2, 3]) //=> true
R.replace(/foo/g, 'bar', 'foo foo') //=> 'bar bar'
R.replace(/foo/g, 'bar')('foo foo') //=> 'bar bar'
R.replace(/foo/g)('bar')('foo foo') //=> 'bar bar'
Globalcode – Open4education
Apply the parameters left-to-right
when you have them
👉
Globalcode – Open4education
But what if you'd like to apply the
leftmost or intermediate arguments
before the rightmost?
Globalcode – Open4education
Leave argument placeholders
✍
Globalcode – Open4education
Functional placeholder
R.minus(R.__, 5)(10) //=> 5
R.divide(R.__, 5)(10) //=> 17
R.contains(R.__, [1, 2, 3])(2) //=> true
R.contains(R.__, [1, 2, 3])(1) //=> true
R.contains(R.__, R.__)(1)([1, 2, 3]) //=> true
Globalcode – Open4education
Or do partial application
Globalcode – Open4education
Ramda does partial application
both left-to-right and right-to-left
Globalcode – Open4education
In fact, there's no currying as in
theoretical math, Ramda currying
is, in reality, partial application
Globalcode – Open4education
Partial application
const greet = (salutation, title, name) =>
`${salutation}, ${title} ${name}!`
const greetMrCrowley = R.partialRight(greet, ['Mr.', 'Crowley'])
greetMrCrowley('Hello') //=> 'Hello, Mr. Crowley!'
const greetMr = R.partial(greet, ['Hello', 'Mr.'])
const greetMs = R.partial(greet, ['Hello', 'Ms.'])
greetMr('Mendel') // => 'Hello, Mr. Mendel'
greetMs('Curie') // => 'Hello, Ms. Curie'
Globalcode – Open4education
Ramda also makes available some
cool and very useful functions
Globalcode – Open4education
Avoiding you to rewrite the same
stuff that you always have to
Globalcode – Open4education
Ramda functions are divided into
various categories
Globalcode – Open4education
Ramda functions categories
Function
Math
List
Logic
Object
Relation
Globalcode – Open4education
Function functions
// Always return the same thing, ignoring arguments
const name = R.always('Derek')
name('Willian') //=> Derek
// Functional if then else, a.k.a. Maybe Monad
const findName = R.ifElse(
R.has('name'),
R.prop('name'),
R.always('No name')
)
findName({ title: 'Mr.', name: 'Derek' }) //=> Derek
findName({ title: 'Mr.' }) //=> No name
// Pipe functions left-to-right. First function can have any arity
// Others must have arity of exactly one
const calculate = R.pipe(Math.pow, R.negate, R.inc);
calculate(3, 2); // -(3^2) + 1
Globalcode – Open4education
Math functions
// Addition
R.add(1, 2) //=> 3
// Subtraction
R.subtract(2, 1) //=> 1
// Sum all elements
R.sum([1, 1, 1]) //=> 3
// Increment
R.inc(41) //=> 42
// Decrement
R.dec(43) //=> 42
// Calculate the mean
R.mean([2, 7, 9]) //=> 6
Globalcode – Open4education
List functions
// Check if all elements match a predicate
R.all(R.gt(4), [1, 2, 3]) //=> true
// Check if a number is inside a list
R.contains(3, [1, 2, 3])
// Drop elements from start
R.drop(1, ['foo', 'bar', 'baz']) //=> ['bar', 'baz']
// Find elements using a predicate
const list = [{a: 1}, {a: 2}, {a: 3}]
R.find(R.propEq('a', 2))(list) //=> {a: 2}
R.find(R.propEq('a', 4))(list) //=> undefined
// Flatten a list of list into a single list
const list = [1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]
R.flatten(list) //=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Globalcode – Open4education
Logic functions
// Combine predicates into a single function
var gt10 = x => x > 10
var even = x => x % 2 === 0
var gt10even = R.both(gt10, even)
gt10even(100) //=> true
gt10even(101) //=> false
// Functional switch case (aka. pattern matching?)
var whatHappensAtTemperature = R.cond([
[R.equals(0), R.always('water freezes at 0°C')],
[R.equals(100), R.always('water boils at 100°C')],
[R.T, temp => 'nothing special happens at ' + temp + '°C']
])
whatHappensAtTemperature(0) //=> 'water freezes at 0°C'
whatHappensAtTemperature(50) //=> 'nothing special happens at 50°C'
whatHappensAtTemperature(100) //=> 'water boils at 100°C'
Globalcode – Open4education
Object functions
// Add a key and value to an already existing object
R.assoc('c', 3, {a: 1, b: 2}) //=> {a: 1, b: 2, c: 3}
// Remove a key from an already existing object
R.dissoc('b', {a: 1, b: 2, c: 3}) //=> {a: 1, c: 3}
// Do the same with a nested path
R.assocPath(['a', 'b', 'c'], 42, {a: {b: {c: 0}}})
//=> {a: {b: {c: 42}}}
// Check if an object contains a key
var hasName = R.has('name')
hasName({name: 'alice'}) //=> true
hasName({name: 'bob'}) //=> true
hasName({}) //=> false
// Pick some properties from objects
var abby = {name: 'Abby', age: 7, hair: 'blond', grade: 2}
var fred = {name: 'Fred', age: 12, hair: 'brown', grade: 7}
R.project(['name', 'grade'], [abby, fred])
//=> [{name: 'Abby', grade: 2}, {name: 'Fred', grade: 7}]
Globalcode – Open4education
Relation functions
// Lots of comparison functions
// Kinda tricky, but also kinda natural
R.gt(2, 1) // 2 > 1
R.gt(2, 2) // 2 > 2
R.gte(2, 2) // 2 >= 2
R.gte(3, 2) // 3 >= 2
R.lt(2, 1) // 2 < 1
R.lt(2, 2) // 2 < 2
R.lte(2, 2) // 2 <= 2
R.lte(3, 2) // 3 <= 2
// Restrict a number to a range
R.clamp(1, 10, -1) // => 1
R.clamp(1, 10, 11) // => 10
// Intersect two lists
R.intersection([1,2,3,4], [7,6,4,3]) //=> [4, 3]
Globalcode – Open4education
Ramda works well with Promise
Globalcode – Open4education
Ramda and Promises
app.get('/v1/plants/:id', auth.active, (req, res) => {
db.plants.find({ id: req.params.id })
.then(R.head)
.then(R.bind(res.send, res))
})
Globalcode – Open4education
Ramda and Promises
function timesValues (number) {
return spot(number)
.then(spot =>
db.collection('zone_type_values')
.findOne({ id: spot.zone_type_value_id })
.then(R.prop('time_values'))
.then(R.assoc('timesValues', R.__, spot))
.then(R.pickAll(['id', 'name', 'timesValues']))
)
}
Globalcode – Open4education
Thanks for watching
Questions?
github.com/derekstavis
twitter.com/derekstavis
facebook.com/derekstavis

More Related Content

PDF
Ramda lets write declarative js
PDF
React for Beginners
PDF
Jggug 2010 330 Grails 1.3 観察
PPTX
Grails queries
PDF
TDC2016POA | Trilha Programacao Funcional - Ramda JS como alternativa a under...
PDF
Introduction to JQ
PDF
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
PDF
jq: JSON - Like a Boss
Ramda lets write declarative js
React for Beginners
Jggug 2010 330 Grails 1.3 観察
Grails queries
TDC2016POA | Trilha Programacao Funcional - Ramda JS como alternativa a under...
Introduction to JQ
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
jq: JSON - Like a Boss

What's hot (17)

PDF
Xlab #1: Advantages of functional programming in Java 8
PDF
Being functional in PHP (DPC 2016)
PDF
はじめてのGroovy
PPTX
FunctionalJS - George Shevtsov
PDF
Programming with Python and PostgreSQL
PDF
名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン
PDF
Python postgre sql a wonderful wedding
PDF
TDC2016SP - Trilha Frameworks JavaScript
PDF
Hypers and Gathers and Takes! Oh my!
PDF
Programmation fonctionnelle en JavaScript
PDF
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
PPTX
Coffee script
PDF
Introduction kot iin
PDF
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
PDF
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
PDF
Javascript ES6 generators
PDF
"PostgreSQL and Python" Lightning Talk @EuroPython2014
Xlab #1: Advantages of functional programming in Java 8
Being functional in PHP (DPC 2016)
はじめてのGroovy
FunctionalJS - George Shevtsov
Programming with Python and PostgreSQL
名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン
Python postgre sql a wonderful wedding
TDC2016SP - Trilha Frameworks JavaScript
Hypers and Gathers and Takes! Oh my!
Programmation fonctionnelle en JavaScript
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Coffee script
Introduction kot iin
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Javascript ES6 generators
"PostgreSQL and Python" Lightning Talk @EuroPython2014
Ad

Viewers also liked (17)

PDF
Packing it all: JavaScript module bundling from 2000 to now
PDF
Hardcore functional programming
PPTX
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
PDF
Lars thorup-react-and-redux-2016-09
PDF
Warsaw Frontend Meetup #1 - Webpack
PDF
Webpack DevTalk
PDF
Webpack Tutorial, Uppsala JS
PPTX
Webpack Introduction
PDF
Forget Grunt and Gulp! Webpack and NPM rule them all!
PPTX
Webpack and Web Performance Optimization
PDF
Webpack: your final module bundler
PPTX
Packing for the Web with Webpack
PDF
webpack 入門
PPTX
前端界流傳的神奇招式
PDF
Advanced webpack
PDF
Webpack
Packing it all: JavaScript module bundling from 2000 to now
Hardcore functional programming
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
Lars thorup-react-and-redux-2016-09
Warsaw Frontend Meetup #1 - Webpack
Webpack DevTalk
Webpack Tutorial, Uppsala JS
Webpack Introduction
Forget Grunt and Gulp! Webpack and NPM rule them all!
Webpack and Web Performance Optimization
Webpack: your final module bundler
Packing for the Web with Webpack
webpack 入門
前端界流傳的神奇招式
Advanced webpack
Webpack
Ad

Similar to Ramda, a functional JavaScript library (20)

PPT
2007 09 10 Fzi Training Groovy Grails V Ws
PPT
Easy R
PPT
r,rstats,r language,r packages
PPTX
Groovy
KEY
JavaScript Growing Up
PDF
A limited guide to intermediate and advanced Ruby
PDF
GPars For Beginners
PDF
Mongoskin - Guilin
PPTX
Introducing PHP Latest Updates
PDF
Metaprogramovanie #1
PPTX
Ruby from zero to hero
PPTX
Functional programming in javascript
PPT
Functional techniques in Ruby
PPT
Functional techniques in Ruby
PDF
php AND MYSQL _ppt.pdf
PDF
Php Tutorials for Beginners
PPT
Php my sql - functions - arrays - tutorial - programmerblog.net
PDF
Tackling repetitive tasks with serial or parallel programming in R
PDF
Why react matters
PDF
R basics
2007 09 10 Fzi Training Groovy Grails V Ws
Easy R
r,rstats,r language,r packages
Groovy
JavaScript Growing Up
A limited guide to intermediate and advanced Ruby
GPars For Beginners
Mongoskin - Guilin
Introducing PHP Latest Updates
Metaprogramovanie #1
Ruby from zero to hero
Functional programming in javascript
Functional techniques in Ruby
Functional techniques in Ruby
php AND MYSQL _ppt.pdf
Php Tutorials for Beginners
Php my sql - functions - arrays - tutorial - programmerblog.net
Tackling repetitive tasks with serial or parallel programming in R
Why react matters
R basics

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Approach and Philosophy of On baking technology
PDF
KodekX | Application Modernization Development
PPTX
Cloud computing and distributed systems.
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Encapsulation theory and applications.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
20250228 LYD VKU AI Blended-Learning.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Understanding_Digital_Forensics_Presentation.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Network Security Unit 5.pdf for BCA BBA.
Per capita expenditure prediction using model stacking based on satellite ima...
Chapter 3 Spatial Domain Image Processing.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Empathic Computing: Creating Shared Understanding
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
cuic standard and advanced reporting.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Approach and Philosophy of On baking technology
KodekX | Application Modernization Development
Cloud computing and distributed systems.
sap open course for s4hana steps from ECC to s4
Spectral efficient network and resource selection model in 5G networks
MIND Revenue Release Quarter 2 2025 Press Release
Encapsulation theory and applications.pdf

Ramda, a functional JavaScript library

  • 1. Globalcode – Open4education Ramda Derek Stavis Software Engineer
  • 2. Globalcode – Open4education Who? Derek Stavis github.com/derekstavis Software Engineer Ruby, JavaScript, Python, C Node, React & Webpack Advocate
  • 3. Globalcode – Open4education Have you been using functional helper libraries?
  • 7. Globalcode – Open4education Those are really good libraries
  • 8. Globalcode – Open4education Weren't designed with a functional mindset from day 0
  • 9. Globalcode – Open4education Ramda A functional programming library http://ramdajs.com
  • 10. Globalcode – Open4education Meet Ramda Emphasizes a purer functional style Immutability and side-effect free functions Ramda functions are automatically curried Argument ordering convenient to currying Keep rightmost the data to be operated
  • 11. Globalcode – Open4education Small, single-responsibility and reusable functions
  • 13. Globalcode – Open4education Ramda shines on currying var names = [ { name: 'Pablo' }, { name: 'Escobar' }, { name: 'Gaviria' } ] var getName = R.prop('name') var pickNames = R.map(getName, names) pickNames() => [ 'Pablo', 'Escobar', 'Gaviria' ]
  • 14. Globalcode – Open4education Ramda shines on currying var names = [ { name: 'Pablo' }, { name: 'Escobar' }, { name: 'Gaviria' } ] var getName = R.prop('name') var pickNames = R.map(getName) pickNames(names) => [ 'Pablo', 'Escobar', 'Gaviria' ]
  • 16. Globalcode – Open4education Currying was named after Haskell Curry One of the first to investigate such technique
  • 17. Globalcode – Open4education Turn a function that expects multiple arguments into one that, when supplied fewer arguments, returns a new function that awaits the remaining ones
  • 18. Globalcode – Open4education Ramda is about currying const cookPasta = R.curry((water, heat, pasta) => { ... }) // Have everything needed cookPasta (water, heat, pasta) // Forgot to buy pasta cookPasta (water, heat)(pasta) // No gas and no pasta cookPasta (water)(heat, pasta) // Had to go twice to supermarket cookPasta (water)(heat)(pasta)
  • 20. Globalcode – Open4education Ramda is all curried 😎
  • 22. Globalcode – Open4education Ramda is all curried R.add(2, 3) //=> 5 R.add(7)(10) //=> 17 R.contains(2, [1, 2, 3]) //=> true R.contains(1)([1, 2, 3]) //=> true R.replace(/foo/g, 'bar', 'foo foo') //=> 'bar bar' R.replace(/foo/g, 'bar')('foo foo') //=> 'bar bar' R.replace(/foo/g)('bar')('foo foo') //=> 'bar bar'
  • 23. Globalcode – Open4education Apply the parameters left-to-right when you have them 👉
  • 24. Globalcode – Open4education But what if you'd like to apply the leftmost or intermediate arguments before the rightmost?
  • 25. Globalcode – Open4education Leave argument placeholders ✍
  • 26. Globalcode – Open4education Functional placeholder R.minus(R.__, 5)(10) //=> 5 R.divide(R.__, 5)(10) //=> 17 R.contains(R.__, [1, 2, 3])(2) //=> true R.contains(R.__, [1, 2, 3])(1) //=> true R.contains(R.__, R.__)(1)([1, 2, 3]) //=> true
  • 27. Globalcode – Open4education Or do partial application
  • 28. Globalcode – Open4education Ramda does partial application both left-to-right and right-to-left
  • 29. Globalcode – Open4education In fact, there's no currying as in theoretical math, Ramda currying is, in reality, partial application
  • 30. Globalcode – Open4education Partial application const greet = (salutation, title, name) => `${salutation}, ${title} ${name}!` const greetMrCrowley = R.partialRight(greet, ['Mr.', 'Crowley']) greetMrCrowley('Hello') //=> 'Hello, Mr. Crowley!' const greetMr = R.partial(greet, ['Hello', 'Mr.']) const greetMs = R.partial(greet, ['Hello', 'Ms.']) greetMr('Mendel') // => 'Hello, Mr. Mendel' greetMs('Curie') // => 'Hello, Ms. Curie'
  • 31. Globalcode – Open4education Ramda also makes available some cool and very useful functions
  • 32. Globalcode – Open4education Avoiding you to rewrite the same stuff that you always have to
  • 33. Globalcode – Open4education Ramda functions are divided into various categories
  • 34. Globalcode – Open4education Ramda functions categories Function Math List Logic Object Relation
  • 35. Globalcode – Open4education Function functions // Always return the same thing, ignoring arguments const name = R.always('Derek') name('Willian') //=> Derek // Functional if then else, a.k.a. Maybe Monad const findName = R.ifElse( R.has('name'), R.prop('name'), R.always('No name') ) findName({ title: 'Mr.', name: 'Derek' }) //=> Derek findName({ title: 'Mr.' }) //=> No name // Pipe functions left-to-right. First function can have any arity // Others must have arity of exactly one const calculate = R.pipe(Math.pow, R.negate, R.inc); calculate(3, 2); // -(3^2) + 1
  • 36. Globalcode – Open4education Math functions // Addition R.add(1, 2) //=> 3 // Subtraction R.subtract(2, 1) //=> 1 // Sum all elements R.sum([1, 1, 1]) //=> 3 // Increment R.inc(41) //=> 42 // Decrement R.dec(43) //=> 42 // Calculate the mean R.mean([2, 7, 9]) //=> 6
  • 37. Globalcode – Open4education List functions // Check if all elements match a predicate R.all(R.gt(4), [1, 2, 3]) //=> true // Check if a number is inside a list R.contains(3, [1, 2, 3]) // Drop elements from start R.drop(1, ['foo', 'bar', 'baz']) //=> ['bar', 'baz'] // Find elements using a predicate const list = [{a: 1}, {a: 2}, {a: 3}] R.find(R.propEq('a', 2))(list) //=> {a: 2} R.find(R.propEq('a', 4))(list) //=> undefined // Flatten a list of list into a single list const list = [1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]] R.flatten(list) //=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  • 38. Globalcode – Open4education Logic functions // Combine predicates into a single function var gt10 = x => x > 10 var even = x => x % 2 === 0 var gt10even = R.both(gt10, even) gt10even(100) //=> true gt10even(101) //=> false // Functional switch case (aka. pattern matching?) var whatHappensAtTemperature = R.cond([ [R.equals(0), R.always('water freezes at 0°C')], [R.equals(100), R.always('water boils at 100°C')], [R.T, temp => 'nothing special happens at ' + temp + '°C'] ]) whatHappensAtTemperature(0) //=> 'water freezes at 0°C' whatHappensAtTemperature(50) //=> 'nothing special happens at 50°C' whatHappensAtTemperature(100) //=> 'water boils at 100°C'
  • 39. Globalcode – Open4education Object functions // Add a key and value to an already existing object R.assoc('c', 3, {a: 1, b: 2}) //=> {a: 1, b: 2, c: 3} // Remove a key from an already existing object R.dissoc('b', {a: 1, b: 2, c: 3}) //=> {a: 1, c: 3} // Do the same with a nested path R.assocPath(['a', 'b', 'c'], 42, {a: {b: {c: 0}}}) //=> {a: {b: {c: 42}}} // Check if an object contains a key var hasName = R.has('name') hasName({name: 'alice'}) //=> true hasName({name: 'bob'}) //=> true hasName({}) //=> false // Pick some properties from objects var abby = {name: 'Abby', age: 7, hair: 'blond', grade: 2} var fred = {name: 'Fred', age: 12, hair: 'brown', grade: 7} R.project(['name', 'grade'], [abby, fred]) //=> [{name: 'Abby', grade: 2}, {name: 'Fred', grade: 7}]
  • 40. Globalcode – Open4education Relation functions // Lots of comparison functions // Kinda tricky, but also kinda natural R.gt(2, 1) // 2 > 1 R.gt(2, 2) // 2 > 2 R.gte(2, 2) // 2 >= 2 R.gte(3, 2) // 3 >= 2 R.lt(2, 1) // 2 < 1 R.lt(2, 2) // 2 < 2 R.lte(2, 2) // 2 <= 2 R.lte(3, 2) // 3 <= 2 // Restrict a number to a range R.clamp(1, 10, -1) // => 1 R.clamp(1, 10, 11) // => 10 // Intersect two lists R.intersection([1,2,3,4], [7,6,4,3]) //=> [4, 3]
  • 41. Globalcode – Open4education Ramda works well with Promise
  • 42. Globalcode – Open4education Ramda and Promises app.get('/v1/plants/:id', auth.active, (req, res) => { db.plants.find({ id: req.params.id }) .then(R.head) .then(R.bind(res.send, res)) })
  • 43. Globalcode – Open4education Ramda and Promises function timesValues (number) { return spot(number) .then(spot => db.collection('zone_type_values') .findOne({ id: spot.zone_type_value_id }) .then(R.prop('time_values')) .then(R.assoc('timesValues', R.__, spot)) .then(R.pickAll(['id', 'name', 'timesValues'])) ) }
  • 44. Globalcode – Open4education Thanks for watching Questions? github.com/derekstavis twitter.com/derekstavis facebook.com/derekstavis