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: Functiona...
Search
Syntax Conference
June 08, 2018
Technology
0
180
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
240
Building Smarter Apps with Cognitive APIs
syntaxcon
0
65
WordPress As An Application Foundation
syntaxcon
0
70
Building Security into Your Data Back-End
syntaxcon
0
86
Baby Got Back(end) – How to Choose A Backend for Your Mobile App
syntaxcon
0
130
An Introduction to MonoGame
syntaxcon
0
220
Just Gimme the Data!
syntaxcon
0
53
Other Decks in Technology
See All in Technology
Rubyで作る論理回路シミュレータの設計の話 - Kashiwa.rb #12
kozy4324
1
320
比起獨自升級 我更喜歡 DevOps 文化 <3
line_developers_tw
PRO
0
260
從四件事帶你見識見識 事件驅動架構設計 (EDA)
line_developers_tw
PRO
0
160
LinkX_GitHubを基点にした_AI時代のプロジェクトマネジメント.pdf
iotcomjpadmin
0
130
ObsidianをMCP連携させてみる
ttnyt8701
2
130
DroidKnights 2025 - Jetpack XR 살펴보기: XR 개발은 어떻게 이루어지는가?
heesung6701
1
150
JSX - 歴史を振り返り、⾯⽩がって、エモくなろう
pal4de
3
1k
AWS と定理証明 〜ポリシー言語 Cedar 開発の舞台裏〜 #fp_matsuri / FP Matsuri 2025
ytaka23
9
2.6k
AI技術トレンド勉強会 #1MCPの基礎と実務での応用
nisei_k
1
230
Devin(Deep) Wiki/Searchの活用で変わる開発の世界観/devin-wiki-search-impact
tomoki10
0
710
Amplifyとゼロからはじめた AIコーディング 成果と展望
mkdev10
1
320
QAはソフトウェアエンジニアリングを学んで実践するのが大事なの
ymty
1
480
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
269
20k
Git: the NoSQL Database
bkeepers
PRO
430
65k
VelocityConf: Rendering Performance Case Studies
addyosmani
329
24k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.6k
Writing Fast Ruby
sferik
628
61k
Speed Design
sergeychernyshev
31
990
Building Applications with DynamoDB
mza
95
6.4k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
357
30k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.2k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
20k
The Invisible Side of Design
smashingmag
299
51k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
52
2.8k
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