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
Elements of Functional Programming in JS
Search
Adam Stankiewicz
May 27, 2014
Programming
2
150
Elements of Functional Programming in JS
Lightning talk from Meet.js Wrocław.
Adam Stankiewicz
May 27, 2014
Tweet
Share
More Decks by Adam Stankiewicz
See All by Adam Stankiewicz
LinemanJS
sheerun
0
110
Consuming and Packaging of Web Components
sheerun
4
410
Promises/A+
sheerun
5
400
Other Decks in Programming
See All in Programming
リリース8年目のサービスの1800個のERBファイルをViewComponentに移行した方法とその結果
katty0324
5
3.6k
生成 AI を活用した toitta 切片分類機能の裏側 / Inside toitta's AI-Based Factoid Clustering
pokutuna
0
620
開発効率向上のためのリファクタリングの一歩目の選択肢 ~コード分割~ / JJUG CCC 2024 Fall
ryounasso
0
370
offers_20241022_imakiire.pdf
imakurusu
2
370
cXML という電子商取引の トランザクションを支える プロトコルと向きあっている話
phigasui
3
2.3k
のびしろを広げる巻き込まれ力:偶然を活かすキャリアの作り方/oso2024
takahashiikki
1
410
Server Driven Compose With Firebase
skydoves
0
400
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
350
Importmapを使ったJavaScriptの 読み込みとブラウザアドオンの影響
swamp09
4
1.3k
Dev ContainersとGitHub Codespacesの素敵な関係
ymd65536
1
130
CSC509 Lecture 09
javiergs
PRO
0
120
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
490
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Building Applications with DynamoDB
mza
90
6.1k
GraphQLの誤解/rethinking-graphql
sonatard
66
10k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
3
370
The Language of Interfaces
destraynor
154
24k
Making the Leap to Tech Lead
cromwellryan
132
8.9k
Raft: Consensus for Rubyists
vanstee
136
6.6k
What's in a price? How to price your products and services
michaelherold
243
12k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Music & Morning Musume
bryan
46
6.1k
Unsuck your backbone
ammeep
668
57k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Transcript
Elements of Functional Programming in JavaScript
What is functional programming? No generally accepted definition, but: •
Style of building computer programs • Avoids state and mutable data • Functions as arguments and results
Benefits of functional programming • More concise, readable code •
Confident refactor, better reasoning • Better concurrency? • Performance (function inlining)
JS has some functional elements • First class functions •
Anonymous functions (lambdas) • ???
JS lacks many functional elements • Tail call optimization •
Pattern matching • Forced immutability (type system) • No standard methods for FP (IE <=8) ◦ Array#forEach, Array#map, Array#filter, etc...
Libraries for FP in JavaScript • Underscore • Lo-Dash •
Mout.js (for Node.js)
Short map syntax var characters = [ { 'id': 1,
'name': 'barney', 'age': 26 }, { 'id': 2, 'name': 'fred', 'age': 30 } ]; _.map(characters, function(c) { return c['name']; }) // ['barney', 'fred'] _.map(characters, 'name') // ['barney', 'fred']
Short filter syntax var characters = [ { 'id': 1,
'name': 'barney', 'age': 26 }, { 'id': 2, 'name': 'fred', 'age': 30 } ]; _.filter(characters, function(c) { return c.age == 30; }) // [{ 'id': 2, 'name': 'fred', 'age': 30 }] _.filter(characters, { age: 30 }) // [{ 'id': 2, 'name': 'fred', 'age': 30 }]
General pattern // function(e) { return e['foo']; } // 'foo'
_.sortBy(characters, 'name') // function(e) { return e['foo'] == bar; } // { 'foo': bar } _.every(characters, { 'name': 'barney' })
GroupBy var words = ['one', 'two', 'three'] _.groupBy(words, 'length'); //
{ '3': ['one', 'two'], '5': ['three'] }
IndexBy var users = [ { 'id': 100, 'name': 'Adam'
}, { 'id': 200, 'name': 'Ala' } ]; var indexedUsers = _.indexBy(users, 'id') indexedUsers[100] // { 'id': 100, 'name': 'Adam' } indexedUsers[200] // { 'id': 100, 'name': 'Ala' }
Find var users = [ { 'id': 100, 'name': 'Adam'
}, { 'id': 200, 'name': 'Ala' } ]; _.find(users, { 'id': 100 }); // { 'id': 100, 'name': 'Adam' }
max, min var characters = [ { 'name': 'barney', 'age':
26 }, { 'name': 'fred', 'age': 30 } ]; _.max(characters, 'age') // { 'name': 'fred', 'age': 30 } _.min(characters, 'age') // { 'name': 'barney', 'age': 26 }
reduce var numbers = [1, 2, 3]; _.reduce(numbers, function(sum, element)
{ return sum + element }); // 6
Composing functions var characters = [ { 'name': 'barney', 'age':
26 }, { 'name': 'fred', 'age': 30 }, // ... ]; var ages = _.map(characters, 'age'); // [26, 30] _.reduce(ages, function(a, b) { return a + b }); // [56]
Composing functions var characters = [ { 'name': 'barney', 'age':
26 }, { 'name': 'fred', 'age': 30 }, // ... ]; _(characters) .map('age') .reduce(function(a, b) { return a + b; });
Composing functions var characters = [ { 'name': 'barney', 'age':
26 }, { 'name': 'fred', 'age': 30 }, // ... ]; _(characters) .map('age') .reduce(function(a, b) { return a + b; });
Other features • Set operations (difference, union, intersection) • every,
some, reject, countBy, contains • clone, extend, defaults, merge, mapValues • delay, defer, memoize, once, throttle • now, random, uniqueId, template
Promises A+ • Pattern for running async code • Allows
for return in callbacks • Safely handles exceptions in callbacks • Allows for try/catch -like behavior • Chaining of callbacks, parallel execution promise.then(onSuccess, onFailure)
Promises A+ $http.get('/users/1').then(function(user) { return $http.get("/users/posts"); }).then(function(post) { return render(post);
}).catch(function(reason) { console.log(reason); });
Dzięki