Ch-4 Building Competitive Advantage Through Functional-Level Strategy
Building Functional Islands
-
Upload
mark-jones -
Category
Technology
-
view
77 -
download
0
Transcript of Building Functional Islands
Statementsconst nums = [1, 2, 3];const doubled = [];
for (let i = 0; i < nums.length; i++) { doubled.push(nums[i] * 2);}
Statementsconst nums = [1, 2, 3];const doubled = [];
for (let i = 0; i < nums.length; i++) { doubled.push(nums[i] * 2);}
Statementsconst nums = [1, 2, 3];const doubled = [];
for (let i = 0; i < nums.length; i++) { doubled.push(nums[i] * 2);}
Statementsconst 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);// [(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
First-Class Functionsfunction 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 Functionsfunction logify(fn) { return (...args) => { console.log(args); return fn(...args); };}
const logifyAdd = logify(add);
function add(x, y) { return x + y;}
Higher-Order Functionsfunction logify(fn) { return (...args) => { console.log(args); return fn(...args); };}
const logifyAdd = logify(add);
function add(x, y) { return x + y;}
Higher-Order Functionsfunction logify(fn) { return (...args) => { console.log(args); return fn(...args); };}
const logifyAdd = logify(add);
function add(x, y) { return x + y;}
function addAndSomethingElse(x, y) { // I'm an impure function doSomethingElse(); return x + y;}
Pure Functions
Immutable valuesconst nums = [1, 2, 3];const person = { name: 'mark', age: 29 };
nums[0] = 2;// [2, 2, 3]person.age = 27;// { name: 'mark', age: 27 }
Immutable valuesconst nums = [1, 2, 3];const person = { name: 'mark', age: 29 };
nums[0] = 2;// [2, 2, 3]person.age = 27;// { name: 'mark', age: 27 }
Immutable valuesconst nums = [1, 2, 3];const person = { name: 'mark', age: 29 };
nums[0] = 2;// [2, 2, 3]person.age = 27;// { name: 'mark', age: 27 }
Immutable valuesconst nums = [1, 2, 3];const person = { name: 'mark', age: 29 };
nums[0] = 2;// [2, 2, 3]person.age = 27;// { name: 'mark', age: 27 }
Object.freezeconst 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.freezeconst 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.freezeconst 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.freezeconst employee = Object.freeze({ department: 'Eng', profile: { name: 'mark', age: 29 }}); employee.profile.age = 27;// {...{ name: 'mark', age: 27 } }
Object.freezeconst employee = Object.freeze({ department: 'Eng', profile: { name: 'mark', age: 29 }}); employee.profile.age = 27;// {...{ name: 'mark', age: 27 } }
Object.freezeconst 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 } }
Curryingconst devs = [ { firstName: 'mark' }, { firstName: 'sally' }];const firstNames = map(devs, (dev) => { return dev.firstName;});
Curryingconst devs = [ { firstName: 'mark' }, { firstName: 'sally' }];const firstNames = map(devs, (dev) => { return dev.firstName;});
Curryingconst devs = [ { firstName: 'mark' }, { firstName: 'sally' }];const firstNames = map(devs, (dev) => { return dev.firstName;});
Curryingconst devs = [ { firstName: 'mark' }, { firstName: 'sally' }];const getFirstNames = map(prop('firstName'));const firstNames = getFirstNames(devs);
Curryingconst devs = [ { firstName: 'mark' }, { firstName: 'sally' }];const getFirstNames = map(prop('firstName'));const firstNames = getFirstNames(devs);
Curryingconst devs = [ { firstName: 'mark' }, { firstName: 'sally' }];const getFirstNames = map(prop('firstName'));const firstNames = getFirstNames(devs);
Curryingconst devs = [ { firstName: 'mark' }, { firstName: 'sally' }];const getFirstNames = map(prop('firstName'));const firstNames = getFirstNames(devs);
Compositionfunction getFilmIdsFromResponse(resp) { return getIds(getFilms(JSON.parse(resp)));}
getFilmIdsFromResponse('{ "films": [{ id: 1 },...], "actors": [...] }');
Compositionfunction getFilmIdsFromResponse(resp) { return getIds(getFilms(JSON.parse(resp)));}
getFilmIdsFromResponse('{ "films": [{ id: 1 },...], "actors": [...] }');
Compositionfunction getFilmIdsFromResponse(resp) { return getIds(getFilms(JSON.parse(resp)));}
getFilmIdsFromResponse('{ "films": [{ id: 1 },...], "actors": [...] }');
Compositionfunction getFilmIdsFromResponse(resp) { return getIds(getFilms(JSON.parse(resp)));}
getFilmIdsFromResponse('{ "films": [{ id: 1 },...], "actors": [...] }');
Compositionfunction getFilmIdsFromResponse(resp) { return getIds(getFilms(JSON.parse(resp)));}
getFilmIdsFromResponse('{ "films": [{ id: 1 },...], "actors": [...] }');
Compositionconst getIds = map(prop('id'));const getFilmIdsFromResponse = compose(getIds, prop('films'), JSON.parse);
getFilmIdsFromResponse('{ "films": [...],"actors": [...] }');
Compositionconst getIds = map(prop('id'));const getFilmIdsFromResponse = compose(getIds, prop('films'), JSON.parse);
getFilmIdsFromResponse('{ "films": [...],"actors": [...] }');
Compositionconst getIds = map(prop('id'));const getFilmIdsFromResponse = compose(getIds, prop('films'), JSON.parse);
getFilmIdsFromResponse('{ "films": [...],"actors": [...] }');
Compositionconst getIds = map(prop('id'));const getFilmIdsFromResponse = compose(getIds, prop('films'), JSON.parse);
getFilmIdsFromResponse('{ "films": [...],"actors": [...] }');
Compositionconst getIds = map(prop('id'));const getFilmIdsFromResponse = compose(getIds, prop('films'), JSON.parse);
getFilmIdsFromResponse('{ "films": [...],"actors": [...] }');
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