Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Introduction to Functional Programming with Jav...
Search
Carolina Pascale Campos
April 19, 2017
Programming
170
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Introduction to Functional Programming with JavaScript
Carolina Pascale Campos
April 19, 2017
Transcript
43 slides
I N T R O D U C T I
O N T O F U N C T I O N A L P R O G R A M M I N G W I T H J AVA S C R I P T
None
W H O A M I ? Carolina Pascale Campos
Software Developer at Codeminer42 Computer Scientist student at UFSCar
F U N C T I O N A L
P R O G R A M M I N G H I S T O RY
W H AT I S F U N C T
I O N A L P R O G R A M M I N G ?
None
F U N C T I O N S F
I R S T- C L A S S C I T I Z E N S
Functions 1 const characterName = (name) => { 2 return
console.log(name); 3 }; 4 5 characterName('Batman'); //Batman
Objects { name: 'Alfred', occupation: 'Butler', villain: false, age: 70
}
Input Array const characters = [ { name: 'Alfred', …},
{ name: 'Batman', …}, { name: 'Harley Quinn’, …}, { name: 'Joker', …} ];
H I G H E R - O R D
E R F U N C T I O N S
Higher-order Functions 1 const villain = (character) => { 2
return character.villain; 3 }; 4 5 const name = (character) => { 6 return character.name; 7 }; 8 9 const villainNames = 10 characters 11 .filter(villain) 12 .map(name); 13 14 console.log(villainNames); // [ 'Harley Quinn', 'Joker' ]
P U R E F U N C T I
O N S S TAT E L E S S
Impure Function 1 let batmanAge = 30; 2 3 function
incrementAge() { 4 return batmanAge++; 5 } 6 7 incrementAge(); //31 8 incrementAge(); //32
Pure Function 1 let batmanAge = 30; 2 3 function
incrementAge(age) { 4 return age++; 5 } 6 7 incrementAge(batmanAge); //31 8 incrementAge(batmanAge); //31 9 console.log(batmanAge);
D E C L A R AT I V E
E X P L I C I T
Declarative programming is “the act of programming in languages that
conform to the mental model of the developer rather than the operational model of the machine”.
None
Imperative Programming 1 function onlyAge(characters) { 2 const age =
[]; 3 const size = characters.length; 4 5 for (let i = 0; i < size; i++) { 6 age.push(characters[i].age); 7 } 8 9 return age; 10 } 11 12 onlyAge(characters); //[ 70, 30, 26, 35 ]
Declarative Programming 1 function onlyAge(cast) { 2 return cast.map(cast =>
cast.age); 3 } 4 5 onlyAge(cast);
I M M U TA B L E
Mutability 1 const hero = { 2 name: 'Batman', 3
age: 30 4 }; 5 6 function changeAge(hero) { 7 const newHero = hero; 8 newHero.age = 31; 9 10 return newHero; 11 } 12 13 const heroDifferentAge = changeAge(hero); 14 console.log(heroDifferentAge, hero);
None
Immutability 1 const hero = { 2 name: 'Batman', 3
age: 30 4 }; 5 6 const changeAge = (hero, newAge) => { 7 return Object.assign( 8 {}, hero, { age: newAge } 9 ); 10 }); 11 12 const newHero = changeAge(hero, 32);
C L O S U R E S E N
C A P S U L AT I O N
Closure 1 const characterQuote = () => { 2 3
const quote = 'I am Batman'; 4 5 const display = () => { 6 return console.log(`${quote}`); 7 }; 8 display(); 9 }; 10 11 characterQuote(); // I am Batman
F I LT E R , M A P, R
E D U C E
Filter
Map
Map
Reduce
M A P
Map 1 const occupations = 2 characters.map((character) => { 3
return character.occupation; 4 }); 5 6 console.log(occupations); 7 // [ 'Butler', 'CEO of Wayne Enterprises', 'Former psychiatrist', 'Vilain' ]
R E D U C E
Reduce 1 const totalAge = 2 characters.reduce((sum, character) => {
3 return sum + character.age; 4 }, 0); 5 6 console.log(totalAge); // 174
C U R RY
Curry 1 const helloBatman = (name) => { 2 return
(greeting) => greeting + name; 3 }; 4 5 const batman = helloBatman('batman'); 6 7 batman('hello'); //hellobatman
PA RT I A L A P P L I
C AT I O N
Partial Application 1 const batman = 40; 2 const joker
= 29; 3 const alfred = 70; 4 5 const sumAges = (batman, joker, alfred) => { 6 return batman + joker + alfred; 7 }; 8 9 const partial = (fn, ...args) => { 10 return fn.bind(null, ...args); 11 }; 12 13 const partialSum = partial(sumAges, batman, joker) 14 15 console.log(partialSum(alfred)); // 139
None
None
Where to find me? https://github.com/carolpc https://twitter.com/CarolinaPascale
Codeminer42
[email protected]
We’re hiring!
More Decks by Carolina Pascale Campos
See All by Carolina Pascale Campos
Building a sustainable codebase with FP - ElixirConfEU
carolpc
2
160
Building a sustainable architecture
carolpc
2
170
Maintaining consistency in distributed systems with an effect machine
carolpc
0
61
Building a sustainable Architecture
carolpc
0
77
Arquitetura de Aplicações Web utilizando Serverless
carolpc
0
130
Working with Serverless - Compact version
carolpc
1
200
Working with Serverless in 2017
carolpc
0
110
Microservices using Node.js and RabbitMQ
carolpc
0
1.8k
Other Decks in Programming
See All in Programming
AIがコードを書いてくれる時代、我々は何を確かめるのか
yosuke_furukawa
PRO
13
6k
React Native経験者が Flutterを30⽇間触って驚いたこと
3saito
0
200
Secure Your Coding Agent Like It’s Malware (v1.5) 🇬🇧 @Code.talks 2026
richargh
0
170
なぜ私たちは「現時点では」KMPの導入を見送ったのか
chigichan24
1
290
AI時代のTerminal環境を UNIX哲学で設計・運用する
yasunori0418
1
360
Claude Codeのおかげでやりたいことがやれるようになったんだよォー!
sms_tech
0
120
TS 7.0 で baseUrl が使えなくなった今、パスエイリアスをどうするか
bicstone
2
700
まずはコードより業務と向き合う:テーブル解体のためにPdMを呼んだ話
sakawe_ee
0
240
AI-Powered Domain-Driven Design
ziobrando
0
460
コード生成時代のAPI定義を支えるRequest Spec移行
pvcresin
0
230
Growing PicoRuby's Ecosystem on ESP32
yuuu
2
140
AWS CDKを理解するために公式の定義を深掘って解説してみる / Understanding AWS CDK by Digging into Its Official Definition
akihisaikeda
3
310
Featured
See All Featured
Fireside Chat
paigeccino
43
4.1k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
2
6.4k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
48k
A Soul's Torment
seathinner
9
3.9k
Large-scale JavaScript Application Architecture
addyosmani
516
110k
Facilitating Awesome Meetings
lara
57
7.2k
First, design no harm
axbom
PRO
2
1.4k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
950
Rebuilding a faster, lazier Slack
samanthasiow
85
9.8k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3.1k
Unsuck your backbone
ammeep
672
58k