@jonathanfmills
Jonathan Mills
A Skeptics Guide to
Functional Style
JavaScript
Functional Style JavaScript
So Hot Right Now
Functions
are first class objects
OBJECTS
Functions are first class
What is
Functional Programming?
In computer
science, functional
programming is a
programming paradigm
a style of building the structure
and elements of computer
programs
that treats computation as the
evaluation of mathematical
functions
that treats computation as the
evaluation of mathematical
functions
ƒ(x) = x + 2
ƒ(3) = 3 + 2 = 5
and avoids changing-state and
mutable data.
Nothing is immutable
in JavaScript
@jonathanfmills
Jonathan Mills
A Skeptics Guide to
Functional Style
JavaScript
"The programmers in that segment like
functional programming because it makes
code clearer, better structured, and it
prevents many classes of errors."
Scala founder Martin Odersky
They quote the
following phrases...
Pure Functions are Awesome!
Side effects are evil!
Without clear reasons for them…
"The programmers in that segment like
functional programming because it makes
code clearer, better structured, and it
prevents many classes of errors."
Scala founder Martin Odersky
It’s the tooling that
does the work!
@taylonr
The bit flips
when you think about it this way
0 > 1-
You can’t avoid Side Effects
Not all functions are pure
Not everything has to be
immutable
Functional style is about
using
less brain power
and
making things simpler
It is about
small,
composable
things
a style of building the structure
and elements of computer
programs
a style of building the structure
and elements of computer
programs
That is where I broke down, I think in systems…
Functional Style
thinks in the smallest possible items
You have to have both, it’s not either
Demo
Lets look at some arrays
let list = [1,2,3,4,5];
//Add 1 to each item...
for(let i = 0; i<list.length; i++){
list[i] = list[i] + 1;
}
console.log(list);
For loop example
let list = [1,2,3,4,5];
//Add 1 to each item...
for(let i = 0; i<list.length; i++){
list[i] = list[i] + 1;
}
console.log(list); // [ 2, 3, 4, 5, 6 ]
For loop example
let list = [1,2,3,4,5];
//Add 1 to each item...
for(let i = 0; i<list.length; i++){
list[i] = list[i] + 1;
}
console.log(list); // [ 2, 3, 4, 5, 6 ]
For loop example
Iteration Code
let list = [1,2,3,4,5];
//Add 1 to each item...
for(let i = 0; i<list.length; i++){
list[i] = list[i] + 1;
}
console.log(list); // [ 2, 3, 4, 5, 6 ]
For loop example
Worker Code
Lets break this up.
let list = [1,2,3,4,5];
//Add 1 to each item...
list.map(/*a function*/);
console.log(list);
map replaces for
Iteration Code
let list = [1,2,3,4,5];
//Add 1 to each item...
for(let i = 0; i<list.length; i++){
list[i] = list[i] + 1;
}
console.log(list); // [ 2, 3, 4, 5, 6 ]
It’s the same thing
Iteration Code
let list = [1,2,3,4,5];
//Add 1 to each item...
list.map(/*a function*/);
console.log(list);
Forget iteration code forever!!
Iteration Code
What are we doing to each item?
let list = [1,2,3,4,5];
//Add 1 to each item...
for(let i = 0; i<list.length; i++){
list[i] = list[i] + 1;
}
console.log(list); // [ 2, 3, 4, 5, 6 ]
For loop example
Worker Code
let list = [1,2,3,4,5];
//Add 1 to each item...
list.map(/*a function*/);
console.log(list);
Forget iteration code forever!!
Worker Code
Pass a pure function to do the work
In computer programming, a function may be considered
a pure function if both of the following statements about
the function hold:
Pure Function
The function always evaluates the same result value
given the same argument value(s).
Pure Function
ƒ(x) = x + 2
function add(a,b){return a+b}; //Pure
math.random(); //Not pure
Evaluation of the result does not cause any semantically
observable side effect or output, such as mutation of
mutable objects or output to I/O devices
Pure Function
let sum = 0;
function add(a){sum = sum + a}; // Not pure
Its not a ruleLess Brain PowerLess to track
Why pure?
let list = [1,2,3,4,5];
//Add 1 to each item...
list.map(function(item){return item+1});
console.log(list);
Add you function in!
Worker Code
let list = [1,2,3,4,5];
//Add 1 to each item...
list.map((item)=>{return item+1});
console.log(list);
Arrow functions!
Worker Code
let list = [1,2,3,4,5];
//Add 1 to each item...
list.map(item=>item+1);
console.log(list);
Simpler!
Worker Code
let list = [1,2,3,4,5];
//Add 1 to each item...
list.map(item=>item+1);
console.log(list);
This looks nice but can be unclear!
let list = [1,2,3,4,5];
let addOne = item=>item+1
//Add 1 to each item...
list.map(addOne);
console.log(list);
No anonymous functions
let list = [1,2,3,4,5];
//Add 1 to each item...
for(let i = 0; i<list.length; i++){
list[i] = list[i] + 1;
}
console.log(list); // [ 2, 3, 4, 5, 6 ]
let list = [1,2,3,4,5];
let addOne = item=>item+1
list.map(addOne);
console.log(list);
and avoids changing-state and
mutable data.
let list = [1,2,3,4,5];
let addOne = item=>item+1
list.map(addOne);
console.log(list); // [ 1, 2, 3, 4, 5]
Why am I forced to change list?
let list = [1,2,3,4,5];
let addOne = item=>item+1
const newList = list.map(addOne);
console.log(newList); // [ 2, 3, 4, 5, 6]
No changing…
let list = [1,2,3,4,5];
let addOne = item=>item+1
list = list.map(addOne);
console.log(newList); // [ 2, 3, 4, 5, 6]
Changing
Less brain power
Easier to validate
Obja === Objb
Enforces Options
Immutability
What about more complicated loops?
Filter items!
let list = [1,2,3,4,5];
let isEven = item=>{return !(item%2)}
list = list.filter(isEven);
console.log(list); // [ 2, 4 ]
let list = [1,2,3,4,5];
let sum = 0;
for(let i = 0; i<list.length; i++){
sum = list[i] + sum;
}
console.log(list); // 15
Not everything is self contained…..
let list = [1,2,3,4,5];
let sumList = (sum, item)=>{return sum + item};
const sum= list.reduce(sumList, 0);
console.log(sum);
Lets add one
then sum them!
let list = [1,2,3,4,5];
let addOne = item=>item+1;
let sumList = (sum,item)=>{return item + sum};
let val = list.map(addOne).reduce(sumList, 0);
console.log(val); // 20
let list = [1,2,3,4,5];
let addOne = item=>item+1;
let sumList = (sum,item)=>{return item + sum};
let val = list.map(addOne).reduce(sumList, 0);
console.log(val); // 20
Find Items
let list = ['jon','jack','Rachel','Susan','Sarah'];
Find Items
let list = ['jon','jack','Rachel','Susan','Sarah'];
let getPerson = item=>item==='Rachel';
const item = list.find(getPerson);
console.log(item);
Find Items
let list = ['jon','jack','Rachel','Susan','Sarah'];
let getPerson = item=>item==='Rachel';
const item = list.find(getPerson ('Rachel'));
console.log(item);
In mathematics and computer science, a higher-order
function (also functional, functional form or functor) is
a function that does at least one of the following: takes one
or more functions as arguments (i.e., procedural
parameters), returns a function as its result.
Higher Order Functions
Find Items
let list = ['jon','jack','Rachel','Susan','Sarah'];
let getPerson = function(search){return item=>item===search;}
const item = list.find(getPerson('Rachel'));
console.log(item); // Rachel
Find Items
let list = ['jon','jack','Rachel','Susan','Sarah'];
let getPerson = search=>item=>item===search;
const item = list.find(getPerson('Rachel'));
console.log(item); // Rachel
Find items!
let list = ['jon','jack','Rachel','Susan','Sarah'];
let getPerson = search=>item=>item===search;
const item = list.findIndex(getPerson('Rachel'));
console.log(item); // 2
Currying!
let sum = add(a,b);
Simple example
In mathematics and computer science, currying is the technique
of translating the evaluation of a function that takes multiple
arguments (or a tuple of arguments) into evaluating a sequence
of functions, each with a single argument.
Currying
let sum = add(a,b);
Simple example
let sum = add(a)(b);
Simple example
buttons = [
{ name: 'One', message: 'Button One' },
{ name: 'Two', message: 'Button Two' },
{ name: 'Three', message: 'Button Three' }
];
Bigger example
<button onClick={this.onClick} name={item.name}>
Bigger example
onClick = (event)=>{
//loop over buttons to get messages
}
Bigger example
onClick = (index, event)=>{
//I don’t have to loop over buttons to get messages
}
Bigger example
let sum = add(a,b);
Simple example
let sum = add(a)(b);
Simple example
Find Items
let list = ['jon','jack','Rachel','Susan','Sarah'];
let getOne = search=>item=>item===search;
const item = list.find(getOne('Rachel'));
console.log(item); // Rachel
getOnClick(index){
return function(event){message=buttons[index]}
}
Bigger example
<button onClick={this.getOnClick(index)} name={item.name}>
Bigger example
Monads
Once you understand Monads, you can no longer explain
them to someone who does not…
Crockford’s Law
In functional programming, a monad is a design pattern that defines how functions, actions,
inputs, and outputs can be used together to build generic types, with the following
organization:
1. Define a data type, and how values of that data type are combined.
2. Create functions that use the data type, and compose them together into actions,
following the rules defined in the first step.
Monad
Monads are things that get around the “no side effects”
rule…
Monad
The Identity Monad
function Identity(value) {
this.value = value;
}
function Identity(value) {
this.value = value;
}
var x = new Identity(5);
console.log(x.value); // 5
Identity.prototype.bind = function(transform) {
return transform(this.value);
};
function Identity(value) {
this.value = value;
}
Identity.prototype.bind = function(transform) {
return transform(this.value);
};
var x = new Identity(5);
x = x.bind((value)=> new Identity(value+2));
console.log(x.value); // 7
Monads are not required
in JavaScript…
we can have side effects…
"The programmers in that segment like
functional programming because it makes
code clearer, better structured, and it
prevents many classes of errors."
Scala founder Martin Odersky
It’s the tooling that
does the work!
function Identity(value) {
this.value = value;
}
Identity.prototype.bind = function(transform) {
return transform(this.value);
};
var x = new Identity(5);
x = x.bind((value)=> new Identity(value+2));
console.log(x.value); //8
function Identity(value) {
this.value = value;
}
Identity.prototype.bind = function(transform) {
return transform(this.value);
};
var x = new Identity(5);
x = x.bind((value)=> new Identity(value+2));
x.value = 5 // <- this
console.log(x.value); // ?
It’s the tooling that
does the work!
For JavaScript,
functional style a mindset..
Simplify the pieces…
Keep the interactions simple
Functional style is about
using
less brain power
and
making things simpler

A Skeptics guide to functional style javascript

  • 1.
    @jonathanfmills Jonathan Mills A SkepticsGuide to Functional Style JavaScript
  • 2.
  • 4.
  • 5.
  • 6.
  • 8.
  • 9.
    a style ofbuilding the structure and elements of computer programs
  • 10.
    that treats computationas the evaluation of mathematical functions
  • 11.
    that treats computationas the evaluation of mathematical functions
  • 12.
  • 13.
    ƒ(3) = 3+ 2 = 5
  • 14.
    and avoids changing-stateand mutable data.
  • 15.
  • 16.
    @jonathanfmills Jonathan Mills A SkepticsGuide to Functional Style JavaScript
  • 17.
    "The programmers inthat segment like functional programming because it makes code clearer, better structured, and it prevents many classes of errors." Scala founder Martin Odersky
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
    "The programmers inthat segment like functional programming because it makes code clearer, better structured, and it prevents many classes of errors." Scala founder Martin Odersky
  • 23.
    It’s the toolingthat does the work!
  • 24.
  • 25.
    The bit flips whenyou think about it this way 0 > 1-
  • 26.
    You can’t avoidSide Effects
  • 27.
  • 28.
    Not everything hasto be immutable
  • 29.
    Functional style isabout using less brain power and making things simpler
  • 30.
  • 31.
    a style ofbuilding the structure and elements of computer programs
  • 32.
    a style ofbuilding the structure and elements of computer programs
  • 33.
    That is whereI broke down, I think in systems…
  • 34.
    Functional Style thinks inthe smallest possible items
  • 35.
    You have tohave both, it’s not either
  • 36.
    Demo Lets look atsome arrays
  • 37.
    let list =[1,2,3,4,5]; //Add 1 to each item... for(let i = 0; i<list.length; i++){ list[i] = list[i] + 1; } console.log(list); For loop example
  • 38.
    let list =[1,2,3,4,5]; //Add 1 to each item... for(let i = 0; i<list.length; i++){ list[i] = list[i] + 1; } console.log(list); // [ 2, 3, 4, 5, 6 ] For loop example
  • 39.
    let list =[1,2,3,4,5]; //Add 1 to each item... for(let i = 0; i<list.length; i++){ list[i] = list[i] + 1; } console.log(list); // [ 2, 3, 4, 5, 6 ] For loop example Iteration Code
  • 40.
    let list =[1,2,3,4,5]; //Add 1 to each item... for(let i = 0; i<list.length; i++){ list[i] = list[i] + 1; } console.log(list); // [ 2, 3, 4, 5, 6 ] For loop example Worker Code
  • 41.
  • 42.
    let list =[1,2,3,4,5]; //Add 1 to each item... list.map(/*a function*/); console.log(list); map replaces for Iteration Code
  • 43.
    let list =[1,2,3,4,5]; //Add 1 to each item... for(let i = 0; i<list.length; i++){ list[i] = list[i] + 1; } console.log(list); // [ 2, 3, 4, 5, 6 ] It’s the same thing Iteration Code
  • 44.
    let list =[1,2,3,4,5]; //Add 1 to each item... list.map(/*a function*/); console.log(list); Forget iteration code forever!! Iteration Code
  • 45.
    What are wedoing to each item?
  • 46.
    let list =[1,2,3,4,5]; //Add 1 to each item... for(let i = 0; i<list.length; i++){ list[i] = list[i] + 1; } console.log(list); // [ 2, 3, 4, 5, 6 ] For loop example Worker Code
  • 47.
    let list =[1,2,3,4,5]; //Add 1 to each item... list.map(/*a function*/); console.log(list); Forget iteration code forever!! Worker Code
  • 48.
    Pass a purefunction to do the work
  • 49.
    In computer programming,a function may be considered a pure function if both of the following statements about the function hold: Pure Function
  • 50.
    The function alwaysevaluates the same result value given the same argument value(s). Pure Function
  • 51.
  • 52.
    function add(a,b){return a+b};//Pure math.random(); //Not pure
  • 53.
    Evaluation of theresult does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices Pure Function
  • 54.
    let sum =0; function add(a){sum = sum + a}; // Not pure
  • 55.
    Its not aruleLess Brain PowerLess to track Why pure?
  • 56.
    let list =[1,2,3,4,5]; //Add 1 to each item... list.map(function(item){return item+1}); console.log(list); Add you function in! Worker Code
  • 57.
    let list =[1,2,3,4,5]; //Add 1 to each item... list.map((item)=>{return item+1}); console.log(list); Arrow functions! Worker Code
  • 58.
    let list =[1,2,3,4,5]; //Add 1 to each item... list.map(item=>item+1); console.log(list); Simpler! Worker Code
  • 59.
    let list =[1,2,3,4,5]; //Add 1 to each item... list.map(item=>item+1); console.log(list); This looks nice but can be unclear!
  • 60.
    let list =[1,2,3,4,5]; let addOne = item=>item+1 //Add 1 to each item... list.map(addOne); console.log(list); No anonymous functions
  • 61.
    let list =[1,2,3,4,5]; //Add 1 to each item... for(let i = 0; i<list.length; i++){ list[i] = list[i] + 1; } console.log(list); // [ 2, 3, 4, 5, 6 ]
  • 62.
    let list =[1,2,3,4,5]; let addOne = item=>item+1 list.map(addOne); console.log(list);
  • 63.
    and avoids changing-stateand mutable data.
  • 64.
    let list =[1,2,3,4,5]; let addOne = item=>item+1 list.map(addOne); console.log(list); // [ 1, 2, 3, 4, 5] Why am I forced to change list?
  • 65.
    let list =[1,2,3,4,5]; let addOne = item=>item+1 const newList = list.map(addOne); console.log(newList); // [ 2, 3, 4, 5, 6] No changing…
  • 66.
    let list =[1,2,3,4,5]; let addOne = item=>item+1 list = list.map(addOne); console.log(newList); // [ 2, 3, 4, 5, 6] Changing
  • 67.
    Less brain power Easierto validate Obja === Objb Enforces Options Immutability
  • 68.
    What about morecomplicated loops?
  • 69.
    Filter items! let list= [1,2,3,4,5]; let isEven = item=>{return !(item%2)} list = list.filter(isEven); console.log(list); // [ 2, 4 ]
  • 70.
    let list =[1,2,3,4,5]; let sum = 0; for(let i = 0; i<list.length; i++){ sum = list[i] + sum; } console.log(list); // 15 Not everything is self contained…..
  • 71.
    let list =[1,2,3,4,5]; let sumList = (sum, item)=>{return sum + item}; const sum= list.reduce(sumList, 0); console.log(sum);
  • 72.
  • 73.
    let list =[1,2,3,4,5]; let addOne = item=>item+1; let sumList = (sum,item)=>{return item + sum}; let val = list.map(addOne).reduce(sumList, 0); console.log(val); // 20
  • 74.
    let list =[1,2,3,4,5]; let addOne = item=>item+1; let sumList = (sum,item)=>{return item + sum}; let val = list.map(addOne).reduce(sumList, 0); console.log(val); // 20
  • 75.
    Find Items let list= ['jon','jack','Rachel','Susan','Sarah'];
  • 76.
    Find Items let list= ['jon','jack','Rachel','Susan','Sarah']; let getPerson = item=>item==='Rachel'; const item = list.find(getPerson); console.log(item);
  • 77.
    Find Items let list= ['jon','jack','Rachel','Susan','Sarah']; let getPerson = item=>item==='Rachel'; const item = list.find(getPerson ('Rachel')); console.log(item);
  • 78.
    In mathematics andcomputer science, a higher-order function (also functional, functional form or functor) is a function that does at least one of the following: takes one or more functions as arguments (i.e., procedural parameters), returns a function as its result. Higher Order Functions
  • 79.
    Find Items let list= ['jon','jack','Rachel','Susan','Sarah']; let getPerson = function(search){return item=>item===search;} const item = list.find(getPerson('Rachel')); console.log(item); // Rachel
  • 80.
    Find Items let list= ['jon','jack','Rachel','Susan','Sarah']; let getPerson = search=>item=>item===search; const item = list.find(getPerson('Rachel')); console.log(item); // Rachel
  • 81.
    Find items! let list= ['jon','jack','Rachel','Susan','Sarah']; let getPerson = search=>item=>item===search; const item = list.findIndex(getPerson('Rachel')); console.log(item); // 2
  • 82.
  • 83.
    let sum =add(a,b); Simple example
  • 84.
    In mathematics andcomputer science, currying is the technique of translating the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument. Currying
  • 85.
    let sum =add(a,b); Simple example
  • 86.
    let sum =add(a)(b); Simple example
  • 88.
    buttons = [ {name: 'One', message: 'Button One' }, { name: 'Two', message: 'Button Two' }, { name: 'Three', message: 'Button Three' } ]; Bigger example
  • 89.
  • 90.
    onClick = (event)=>{ //loopover buttons to get messages } Bigger example
  • 91.
    onClick = (index,event)=>{ //I don’t have to loop over buttons to get messages } Bigger example
  • 92.
    let sum =add(a,b); Simple example
  • 93.
    let sum =add(a)(b); Simple example
  • 94.
    Find Items let list= ['jon','jack','Rachel','Susan','Sarah']; let getOne = search=>item=>item===search; const item = list.find(getOne('Rachel')); console.log(item); // Rachel
  • 95.
  • 96.
  • 97.
  • 98.
    Once you understandMonads, you can no longer explain them to someone who does not… Crockford’s Law
  • 99.
    In functional programming,a monad is a design pattern that defines how functions, actions, inputs, and outputs can be used together to build generic types, with the following organization: 1. Define a data type, and how values of that data type are combined. 2. Create functions that use the data type, and compose them together into actions, following the rules defined in the first step. Monad
  • 100.
    Monads are thingsthat get around the “no side effects” rule… Monad
  • 101.
  • 102.
  • 103.
    function Identity(value) { this.value= value; } var x = new Identity(5); console.log(x.value); // 5
  • 104.
    Identity.prototype.bind = function(transform){ return transform(this.value); };
  • 105.
    function Identity(value) { this.value= value; } Identity.prototype.bind = function(transform) { return transform(this.value); }; var x = new Identity(5); x = x.bind((value)=> new Identity(value+2)); console.log(x.value); // 7
  • 106.
    Monads are notrequired in JavaScript… we can have side effects…
  • 107.
    "The programmers inthat segment like functional programming because it makes code clearer, better structured, and it prevents many classes of errors." Scala founder Martin Odersky
  • 108.
    It’s the toolingthat does the work!
  • 109.
    function Identity(value) { this.value= value; } Identity.prototype.bind = function(transform) { return transform(this.value); }; var x = new Identity(5); x = x.bind((value)=> new Identity(value+2)); console.log(x.value); //8
  • 110.
    function Identity(value) { this.value= value; } Identity.prototype.bind = function(transform) { return transform(this.value); }; var x = new Identity(5); x = x.bind((value)=> new Identity(value+2)); x.value = 5 // <- this console.log(x.value); // ?
  • 111.
    It’s the toolingthat does the work!
  • 112.
  • 113.
  • 114.
  • 115.
    Functional style isabout using less brain power and making things simpler

Editor's Notes

  • #10 We will come back to this!!! Elements..
  • #11 There is functionall
  • #12 There is functionall
  • #16 We can’t even get types to stay the same…
  • #17 The benefit of FP is in the tooling, JS Doesn’t have the tooling…. So why?
  • #32 We will come back to this!!! Elements..
  • #33 We will come back to this!!! Elements..
  • #43 I don’t want to have to care about iteration again!!!
  • #45 I don’t want to have to care about iteration again!!!
  • #48 I don’t want to have to care about iteration again!!!
  • #57 I don’t want to have to care about iteration again!!!
  • #58 I don’t want to have to care about iteration again!!!
  • #59 I don’t want to have to care about iteration again!!!
  • #60 I don’t want to have to care about iteration again!!!
  • #61 I don’t want to have to care about iteration again!!!
  • #63 I don’t want to have to care about iteration again!!!
  • #65 I don’t want to have to care about iteration again!!!
  • #66 I don’t want to have to care about iteration again!!!
  • #67 I don’t want to have to care about iteration again!!!
  • #74 I don’t want to have to care about iteration again!!!
  • #75 I don’t want to have to care about iteration again!!!