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
Building Functional Islands
Search
Mark Jones
April 28, 2016
Technology
2
560
Building Functional Islands
A talk about functional programming at the April 2016 Front End London
http://frontendlondon.co.uk
Mark Jones
April 28, 2016
Tweet
Share
Other Decks in Technology
See All in Technology
LeSSに潜む「隠れWF病」とその処方箋
lycorptech_jp
PRO
2
120
とあるユーザー企業におけるリスクベースで考えるセキュリティ業務のお話し
4su_para
3
320
[AWS JAPAN 生成AIハッカソン] Dialog の紹介
yoshimi0227
0
150
Amazon FSx for NetApp ONTAPを利用するにあたっての要件整理と設計のポイント
non97
1
160
MAMを軸とした動画ハンドリングにおけるAI活用前提の整備と次世代ビジョン / abema-ai-mam
cyberagentdevelopers
PRO
1
110
VPC間の接続方法を整理してみた #自治体クラウド勉強会
non97
1
850
ガチ勢によるPipeCD運用大全〜滑らかなCI/CDを添えて〜 / ai-pipecd-encyclopedia
cyberagentdevelopers
PRO
3
210
プロダクトチームへのSystem Risk Records導入・運用事例の紹介/Introduction and Case Studies on Implementing and Operating System Risk Records for Product Teams
taddy_919
1
170
【若手エンジニア応援LT会】AWS Security Hubの活用に苦労した話
kazushi_ohata
0
170
初心者に Vue.js を 教えるには
tsukuha
5
390
Jr. Championsになって、強く連携しながらAWSをもっと使いたい!~AWSに対する期待と行動~
amixedcolor
0
190
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
5
49k
Featured
See All Featured
Into the Great Unknown - MozCon
thekraken
31
1.5k
Gamification - CAS2011
davidbonilla
80
5k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.2k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
[RailsConf 2023] Rails as a piece of cake
palkan
51
4.9k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Making Projects Easy
brettharned
115
5.9k
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5.2k
KATA
mclloyd
29
13k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.6k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
355
29k
Transcript
Building Functional Islands
Audience Participation
What is Functional Programming?
Statements const nums = [1, 2, 3]; const doubled =
[]; for (let i = 0; i < nums.length; i++) { doubled.push(nums[i] * 2); }
Statements const nums = [1, 2, 3]; const doubled =
[]; for (let i = 0; i < nums.length; i++) { doubled.push(nums[i] * 2); }
Statements const nums = [1, 2, 3]; const doubled =
[]; for (let i = 0; i < nums.length; i++) { doubled.push(nums[i] * 2); }
Statements const nums = [1, 2, 3]; const doubled =
[]; for (let i = 0; i < nums.length; i++) { doubled.push(nums[i] * 2); }
function double(x) { return x * 2; } map([1, 2,
3], double); Expressions
function double(x) { return x * 2; } map([1, 2,
3], double); // [(1 * 2), (2 * 2), (3 * 2)] Expressions
function double(x) { return x * 2; } map([1, 2,
3], double); // [(1 * 2), (2 * 2), (3 * 2)] // [2, 4, 6] Expressions
Why Functional Programming?
Functions Island Building Block #1
First-Class Functions function onClick() { // I'm a first-class function
} document.body.addEventListener('click', onClick);
function onClick() { // I get called by a higher-order
function } document.body.addEventListener('click', onClick); Higher-Order Functions
Higher-Order Functions function logify(fn) { return (...args) => { console.log(args);
return fn(...args); }; } const logifyAdd = logify(add); function add(x, y) { return x + y; }
Higher-Order Functions function logify(fn) { return (...args) => { console.log(args);
return fn(...args); }; } const logifyAdd = logify(add); function add(x, y) { return x + y; }
Higher-Order Functions function logify(fn) { return (...args) => { console.log(args);
return fn(...args); }; } const logifyAdd = logify(add); function add(x, y) { return x + y; }
Higher-Order Functions const logifyAdd = logify(add); logifyAdd(1, 2); // [1,
2] // 3
Higher-Order Functions The Benefits
Pure Functions function add(x, y) { // I'm a pure
function return x + y; }
Pure Functions add(1, 2) + add(3, 4);
Pure Functions add(1, 2) + add(3, 4); // 3 +
add(3, 4);
Pure Functions add(1, 2) + add(3, 4); // 3 +
add(3, 4); // 3 + 7;
Pure Functions add(1, 2) + add(3, 4); // 3 +
add(3, 4); // 3 + 7; // 10;
function addAndSomethingElse(x, y) { // I'm an impure function doSomethingElse();
return x + y; } Pure Functions
Pure Functions addAndSomethingElse(1, 2) // ???
Pure Functions The Benefits
Pure Functions The Reality
Immutability Island Building Block #2
Immutable values const nums = [1, 2, 3]; const person
= { name: 'mark', age: 29 }; nums[0] = 2; // [2, 2, 3] person.age = 27; // { name: 'mark', age: 27 }
Immutable values const nums = [1, 2, 3]; const person
= { name: 'mark', age: 29 }; nums[0] = 2; // [2, 2, 3] person.age = 27; // { name: 'mark', age: 27 }
Immutable values const nums = [1, 2, 3]; const person
= { name: 'mark', age: 29 }; nums[0] = 2; // [2, 2, 3] person.age = 27; // { name: 'mark', age: 27 }
Immutable values const nums = [1, 2, 3]; const person
= { name: 'mark', age: 29 }; nums[0] = 2; // [2, 2, 3] person.age = 27; // { name: 'mark', age: 27 }
Object.freeze const nums = Object.freeze([1, 2, 3]); const person =
Object.freeze({ name: 'mark', age: 29 }); nums[0] = 2; // [1, 2, 3] person.age = 27; // { name: 'mark', age: 29 }
Object.freeze const nums = Object.freeze([1, 2, 3]); const person =
Object.freeze({ name: 'mark', age: 29 }); nums[0] = 2; // [1, 2, 3] person.age = 27; // { name: 'mark', age: 29 }
Object.freeze const nums = Object.freeze([1, 2, 3]); const person =
Object.freeze({ name: 'mark', age: 29 }); nums[0] = 2; // [1, 2, 3] person.age = 27; // { name: 'mark', age: 29 }
Object.freeze const employee = Object.freeze({ department: 'Eng', profile: { name:
'mark', age: 29 } }); employee.profile.age = 27; // {...{ name: 'mark', age: 27 } }
Object.freeze const employee = Object.freeze({ department: 'Eng', profile: { name:
'mark', age: 29 } }); employee.profile.age = 27; // {...{ name: 'mark', age: 27 } }
Object.freeze const employee = Object.freeze({ department: 'Eng', profile: { name:
'mark', age: 29 } }); employee.profile.age = 27; // {...{ name: 'mark', age: 27 } }
deepFreeze const employee = deepFreeze({ department: 'Eng', profile: { name:
'mark', age: 29 } }); employee.profile.age = 27; // {...{ name: 'mark', age: 29 } }
Immutability The Benefits
Immutability The Reality
Currying Island Building Block #3
Currying const add = curry((x, y) => { return x
+ y; }); const succ = add(1); succ(1); // 2
Currying const add = curry((x, y) => { return x
+ y; }); const succ = add(1); succ(1); // 2
Currying const add = curry((x, y) => { return x
+ y; }); const succ = add(1); succ(1); // 2
Currying const devs = [ { firstName: 'mark' }, {
firstName: 'sally' } ]; const firstNames = map(devs, (dev) => { return dev.firstName; });
Currying const devs = [ { firstName: 'mark' }, {
firstName: 'sally' } ]; const firstNames = map(devs, (dev) => { return dev.firstName; });
Currying const devs = [ { firstName: 'mark' }, {
firstName: 'sally' } ]; const firstNames = map(devs, (dev) => { return dev.firstName; });
Currying const devs = [ { firstName: 'mark' }, {
firstName: 'sally' } ]; const getFirstNames = map(prop('firstName')); const firstNames = getFirstNames(devs);
Currying const devs = [ { firstName: 'mark' }, {
firstName: 'sally' } ]; const getFirstNames = map(prop('firstName')); const firstNames = getFirstNames(devs);
Currying const devs = [ { firstName: 'mark' }, {
firstName: 'sally' } ]; const getFirstNames = map(prop('firstName')); const firstNames = getFirstNames(devs);
Currying const devs = [ { firstName: 'mark' }, {
firstName: 'sally' } ]; const getFirstNames = map(prop('firstName')); const firstNames = getFirstNames(devs);
Currying The Benefits
Currying The Reality
Composition Island Building Block #4
Composition const value = 1; const gRes = g(value); const
fgRes = f(gRes);
Composition const value = 1; const gRes = g(value); const
fgRes = f(gRes);
Composition const value = 1; const gRes = g(value); const
fgRes = f(gRes);
Composition const value = 1; const gRes = g(value); const
fgRes = f(gRes);
Composition function fg(x) { return f(g(x)); } fg(1);
Composition const fg = compose(f, g); fg(1);
Composition function getFilmIdsFromResponse(resp) { return getIds(getFilms(JSON.parse(resp))); } getFilmIdsFromResponse('{ "films": [{
id: 1 }, ...], "actors": [...] }');
Composition function getFilmIdsFromResponse(resp) { return getIds(getFilms(JSON.parse(resp))); } getFilmIdsFromResponse('{ "films": [{
id: 1 }, ...], "actors": [...] }');
Composition function getFilmIdsFromResponse(resp) { return getIds(getFilms(JSON.parse(resp))); } getFilmIdsFromResponse('{ "films": [{
id: 1 }, ...], "actors": [...] }');
Composition function getFilmIdsFromResponse(resp) { return getIds(getFilms(JSON.parse(resp))); } getFilmIdsFromResponse('{ "films": [{
id: 1 }, ...], "actors": [...] }');
Composition function getFilmIdsFromResponse(resp) { return getIds(getFilms(JSON.parse(resp))); } getFilmIdsFromResponse('{ "films": [{
id: 1 }, ...], "actors": [...] }');
Composition const getIds = map(prop('id')); const getFilmIdsFromResponse = compose(getIds, prop('films'),
JSON.parse); getFilmIdsFromResponse('{ "films": [...], "actors": [...] }');
Composition const getIds = map(prop('id')); const getFilmIdsFromResponse = compose(getIds, prop('films'),
JSON.parse); getFilmIdsFromResponse('{ "films": [...], "actors": [...] }');
Composition const getIds = map(prop('id')); const getFilmIdsFromResponse = compose(getIds, prop('films'),
JSON.parse); getFilmIdsFromResponse('{ "films": [...], "actors": [...] }');
Composition const getIds = map(prop('id')); const getFilmIdsFromResponse = compose(getIds, prop('films'),
JSON.parse); getFilmIdsFromResponse('{ "films": [...], "actors": [...] }');
Composition const getIds = map(prop('id')); const getFilmIdsFromResponse = compose(getIds, prop('films'),
JSON.parse); getFilmIdsFromResponse('{ "films": [...], "actors": [...] }');
Composition The Benefits
Composition The Reality
I’m confused. What’s a functional island again?
Further reading / watch list (free) https://drboolean.gitbooks.io/mostly-adequate-guide/content/ Professor Frisby's Mostly
Adequate Guide to Functional Programming http://ramdajs.com/ A practical functional library for Javascript programmers https://github.com/lodash/lodash/wiki/FP-Guide lodash/fp guide https://www.youtube.com/watch?v=m3svKOdZijA Hey Underscore, You're Doing It Wrong! https://github.com/substack/deep-freeze deepFreeze
Further reading / watch list (paid) https://frontendmasters.com/courses/functional-js-lite/ Functional Lite JavaScript
https://frontendmasters.com/courses/functional-javascript/ Hardcore Functional Programming in JavaScript
Thank you. @mark_ jones