Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Less Frustration, More Readable Code: Functional Programming in JavaScript with RamdaJS
Search
Syntax Conference
June 08, 2018
Technology
0
140
Less Frustration, More Readable Code: Functional Programming in JavaScript with RamdaJS
A presentation by Nate Taylor at Syntax Conference (SyntaxCon) 2018, in Charleston, South Carolina
Syntax Conference
June 08, 2018
Tweet
Share
More Decks by Syntax Conference
See All by Syntax Conference
PouchDB, the Database that Syncs
syntaxcon
1
200
Building Smarter Apps with Cognitive APIs
syntaxcon
0
56
WordPress As An Application Foundation
syntaxcon
0
56
Building Security into Your Data Back-End
syntaxcon
0
66
Baby Got Back(end) – How to Choose A Backend for Your Mobile App
syntaxcon
0
110
An Introduction to MonoGame
syntaxcon
0
150
Just Gimme the Data!
syntaxcon
0
49
Other Decks in Technology
See All in Technology
プラットフォームってつくることより計測することが重要なんじゃないかという話 / Platform Engineering Meetup #8
taishin
1
350
ユーザーストーリーのレビューを自動化したみたの
bun913
1
420
JSON攻略法.pdf
miyakemito
8
5k
レガシーをぶっ壊せ。AEONで始めるDevRelの話 / Qiita Night 2024-2-22
aeonpeople
3
1.3k
どうするコスト最適化のトレードオフ
tetsuyaooooo
1
500
エンジニア候補者向け資料2024.04.24.pdf
macloud
0
3.3k
On Your Data を超えていく!
hirotomotaguchi
2
660
データベース02: データベースの概念
trycycle
0
150
KubeConにproposalを送りたい人へのアドバイス
sat
PRO
3
240
JAWS-UG Bedrock Claude Night
yamahiro
3
560
障害対応をちょっとずつよくしていくための 演習の作りかた
heleeen
0
110
Java EE/Jakarta EEの現状と将来―クラウドネイティブ時代にJava EEは対応できるのか?―
takakiyo
1
140
Featured
See All Featured
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
221
21k
RailsConf 2023
tenderlove
4
540
Designing on Purpose - Digital PM Summit 2013
jponch
110
6.5k
Practical Orchestrator
shlominoach
182
9.7k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
116
18k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
60
14k
Navigating Team Friction
lara
178
13k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
244
20k
Into the Great Unknown - MozCon
thekraken
10
990
Facilitating Awesome Meetings
lara
42
5.6k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
20
1.9k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
352
28k
Transcript
NATE TAYLOR Developer | Conference Speaker Less Frustration, More Readable
Code: Functional Programming with RamdaJS
© 2017 NATE TAYLOR @taylonr taylonr.com let candyBars = [milkyWay,
hersheys, snickers]; let regularSize = []; candyBars.forEach(function(c){ c.versions.forEach(function(v) { if(v.size === 'regular’){ regularSize.push({name: c.name, versions: [v]}); } }); }); Have You Seen This Code?
RamdaJS © 2017 NATE TAYLOR @taylonr taylonr.com
A practical functional library for JavaScript programmers. © 2017 NATE
TAYLOR @taylonr taylonr.com What is Ramda?
Overarching Goals © 2017 NATE TAYLOR @taylonr taylonr.com Immutable
An object whose state cannot change after being created ©
2017 NATE TAYLOR @taylonr taylonr.com Immutable
C a n d y b a r s !
© 2017 NATE TAYLOR @taylonr taylonr.com
© 2017 NATE TAYLOR @taylonr taylonr.com Don’t Lick My Candy
Bar The Big Idea
Overarching Goals © 2017 NATE TAYLOR @taylonr taylonr.com Immutable Currying
The process of transforming a function that takes multiple arguments
into a function that takes just a single argument and returns another function if any arguments are still needed. © 2017 NATE TAYLOR @taylonr taylonr.com Currying
C a n d y b a r s !
© 2017 NATE TAYLOR @taylonr taylonr.com
© 2017 NATE TAYLOR @taylonr taylonr.com contains('peanuts’, candy) containsPeanuts =
contains('peanuts'); containsPeanuts(candy); Currying
© 2017 NATE TAYLOR @taylonr taylonr.com Define Once, Use Everywhere
The Big Idea
Overarching Goals © 2017 NATE TAYLOR @taylonr taylonr.com Immutable Currying
Composition
The act of pipelining the result of one function, to
the input of another, creating an entirely new function. © 2017 NATE TAYLOR @taylonr taylonr.com Function Composition
C a n d y b a r s !
© 2017 NATE TAYLOR @taylonr taylonr.com
© 2017 NATE TAYLOR @taylonr taylonr.com const getMultipiecePeanutCandy = (candy)
=> { const multiPiece = eliminateSinglePieces(candy); return eliminateNonPeanuts(multiPiece); } Function Composition
© 2017 NATE TAYLOR @taylonr taylonr.com const getMultipieceCandy = (candy)
=> eliminatePeanuts(eliminateSinglePieces(candy)); Function Composition
© 2017 NATE TAYLOR @taylonr taylonr.com Increase Readability The Big
Idea
S a m p l e R a m d
a F u n c t i o n s © 2017 NATE TAYLOR @taylonr taylonr.com
Using Multiple Functions © 2017 NATE TAYLOR @taylonr taylonr.com const
arr = ['Milky Way', 'Twix', 'Snickers’, ‘3 Musketeers', 'm&ms']; const containsCandy = (candy) => { contains(candy, take(2, sortBy(toLower, arr))); } containsCandy('Milky Way'); //false
Using Multiple Functions © 2017 NATE TAYLOR @taylonr taylonr.com const
arr = ['Milky Way', 'Twix', 'Snickers’, ‘3 Musketeers', 'm&ms']; const containsCandy = (candy) => { contains(candy, take(2, sortBy(toLower, arr))); } containsCandy('Milky Way'); //false
Pipe © 2017 NATE TAYLOR @taylonr taylonr.com const containsCandy =
(candy) => { let arr = ['Milky Way', 'Twix', 'Snickers’, '3 Musketeers', 'm&ms']; return pipe( sortBy(toLower), take(2), contains(candy) )(arr) } containsCandy('Milky Way'); //false
List Functions © 2017 NATE TAYLOR @taylonr taylonr.com
all | any | none © 2017 NATE TAYLOR @taylonr
taylonr.com R.all(_ => _ % 2 == 0, [1, 2, 3, 4]) //false R.any(_ => _ % 2 == 0, [1, 2, 3, 4]) //true R.none(_ => _ % 2 == 0, [1, 2, 3, 4]) //false
zip © 2017 NATE TAYLOR @taylonr taylonr.com zip([1, 2, 3],
['a', 'b', 'c']); //=> [[1, 'a'], [2, 'b'], [3, 'c']]
Combining Together © 2017 NATE TAYLOR @taylonr taylonr.com var csvHeaders
= ['accountName', 'assetName', 'quantity’]; var firstRow = ['accountName', 'assetName', 'quantity']; var createObj = x => {return {csv: x[0], val: x[1]};}; var csvMatchesValue = x => x.csv === x.val; var isHeader = R.pipe( R.zip(csvHeaders), R.map(createObj), R.all(csvMatchesValue) ); isHeader(firstRow)
Object Functions © 2017 NATE TAYLOR @taylonr taylonr.com
path | pathOr © 2017 NATE TAYLOR @taylonr taylonr.com let
milkyWay = { name: 'Milky Way’, nutrition: { fat: 10 } }; R.path(['nutrition', 'fat’]); //10 R.path(['nutrition', 'sodium’]); //undefined R.pathOr('0%', ['nutrition', 'vitamins', 'A’]); //0%
pick © 2017 NATE TAYLOR @taylonr taylonr.com let milkyWay =
{ name: 'Milky Way', calories: 210, funSize: false, nutrition: { fat: 10, carbohydrate: 41, protein: 2.3 } }; R.pick(['name', 'calories'], milkyWay); //{name: 'Milky Way', calories: 210}
Other Categories © 2017 NATE TAYLOR @taylonr taylonr.com Logic Math
String Relation
Why Use Ramda? © 2017 NATE TAYLOR @taylonr taylonr.com
Any fool can write code that a computer can understand.
Good programmers write code that humans can understand. © 2017 NATE TAYLOR @taylonr taylonr.com -- Martin Fowler
© 2017 NATE TAYLOR @taylonr taylonr.com var buyACandyBar = (me,
cashier) => { pipe( driveToStore(), findAllCandyBars(), eliminateKingSize(), eliminatePeanuts(), takeOne(), pay(cashier) )(me); }; Readable Code
How Do I Get Started? © 2017 NATE TAYLOR @taylonr
taylonr.com Start With Lists Eliminate For Loops Think Small Think Transformation
NATE TAYLOR @taylonr|
[email protected]
THANK YOU