Bianca Gandolfo PRO
Slide decks for learning JavaScript
@BIANCAGANDO
FEM 2017
SCOPE
@BIANCAGANDO
FEM 2017
OVERVIEW
@BIANCAGANDO
FEM 2017
Evidence
What clues have we found out about Miss Scarlet?
@BIANCAGANDO
FEM 2017
HIGHER-ORDER FUNCTIONS AND CALLBACKS
@BIANCAGANDO
FEM 2017
element.addEventListener("change",() => {
console.log("Our evidence is updated");
});
1. Takes a function as an input (argument)
@BIANCAGANDO
FEM 2017
const newClue = (name) => {
const length = name.length;
return (weapon) => {
let clue = length + weapon.length;
return !!(clue % 1);
};
};
2. Returns a function as the output
@BIANCAGANDO
FEM 2017
CALLBACKS
const ifElse = (condition, isTrue, isFalse) => { return condition ? isTrue() : isFalse(); }; ifElse(true, () => { console.log(true);}, () => { console.log(false);} );
"I saw Rev. Green with a pistol" - Miss Scarlet
@BIANCAGANDO
FEM 2017
const ifElse = (condition, isTrue, isFalse) => { return condition ? isTrue() : isFalse(); }; const logTrue = () => {console.log(true);};const logFalse = () => {console.log(false);}; ifElse(true, logTrue, logFalse);
@BIANCAGANDO
FEM 2017
PASSING ARGUMENTS
var increment = function(n){ return n + 1; };
var square = function(n){ return n * n; };
var doMathSoIDontHaveTo = function(n, func){ return func(n); };
doMathSoIDontHaveTo(5, square); //25
doMathSoIDontHaveTo(4, increment);
@BIANCAGANDO
FEM 2017
Exercise: Translate into ES6
var increment = function(n){ return n + 1; };
var square = function(n){ return n*n; };
var doMathSoIDontHaveTo = function(n, func){ return func(n); };
doMathSoIDontHaveTo(5, square);
doMathSoIDontHaveTo(4, increment);
@BIANCAGANDO
FEM 2017
//How do we pass arguments?
const ifElse = (condition, isTrue, isFalse, p) => {
return condition ? isTrue(p) : isFalse(p);
};
ifElse(true, fn1, fn2, 'HI');
PASSING ARGUMENTS
@BIANCAGANDO
FEM 2017
const ifElse = (condition, isTrue, isFalse, ...args) => {console.log(args) //['HI', 'BYE', 'HOLA']return condition ? isTrue(...args) : isFalse(...args);isTrue('HI', 'BYE', 'HOLA') };ifElse(true, fn1, fn2, 'HI', 'BYE', 'HOLA');
How was this done pre-ES6?
@BIANCAGANDO
FEM 2017
const ifElse = (condition, isTrue, isFalse) => { const args = [].slice.call(arguments,3)return condition ? isTrue.apply(this, args) : isFalse.apply(this, args); }; const logTrue = (msgs) => { console.log(msgs); }; const logFalse = (msgs) => { console.log(msgs); }; ifElse(true, logTrue, logFalse, 'a', 'b');
@BIANCAGANDO
FEM 2017
Exercise
@BIANCAGANDO
FEM 2017
Exercise 2
Figure out which room no one claims to be the night of the murder from this data set
@BIANCAGANDO
FEM 2017
Evidence
What clues have we found about
Reverend Green?
@BIANCAGANDO
FEM 2017
FUNCTIONAL UTILITIES
@BIANCAGANDO
FEM 2017
Currying
_.curry(func, [arity=func.length])
Â
Â
var abc = function(a, b, c) {
return [a, b, c];
};
var curried = _.curry(abc);
curried(1)(2)(3);
// => [1, 2, 3]
curried(1, 2)(3);
// => [1, 2, 3]
@BIANCAGANDO
FEM 2017
Exercise: Currying
_.curry(func) {
 //...
}
Implement curry() that only takes up to 2 arguments
@BIANCAGANDO
FEM 2017
Composing
const consider = (name) => {
return `I think it could be... ${name}`;
};
const exclaim = (statement) => {
return `${statement.toUpperCase()}!`;
};
const blame = _.compose(consider, exclaim);
blame('you');
=> 'I think it could be... YOU!'
@BIANCAGANDO
FEM 2017
Exercise Compose
Implement your own compose function
@BIANCAGANDO
FEM 2017
Evidence
What clues do we have on Professor Plum?
"I never got along with the Colonel, but he was acting suspicious when he picked up the candlestick"
- Prof Plum
@BIANCAGANDO
FEM 2017
Extra Credit: Exercise
Implement _.unary(func)
Â
_.map(['6', '8', '10'], _.unary(parseInt));
// => [6, 8, 10]
@BIANCAGANDO
FEM 2017
Advanced Scope
"I think I saw Professor Plum in the Ballroom that night..."
@BIANCAGANDO
FEM 2017
const myAlert = () => { const x = "Help! I think I found a clue!"; const alerter = () => { alert(x); }; alerter(); };alert()
@BIANCAGANDO
FEM 2017
const myAlert = () => {
const x = "Help! I think I found a clue!";
const alerter = (){
alert(x);
};
setTimeout(alerter, 1000)
console.log('what happens first? this log or the alert?')
};
@BIANCAGANDO
FEM 2017
const myAlert = () => {
const x = "Help! I think I found a clue!";
let count = 0;
const alerter = (){
alert(`${x} ${++count}`);
};
return alerter;
};
const funcAlert = myAlert();
const funcAlert2 = myAlert();
funcAlert();
@BIANCAGANDO
FEM 2017
const newClue = (name) => { const length = name.length; return (weapon) => { let clue = length + weapon.length; return !!(clue % 2); }; };
@BIANCAGANDO
FEM 2017
function countClues() {
var n = 0;
return {
count: function() { return ++n; },
reset: function() { return n = 0; }
};
};
//ES6 equivalent
const countClues = () => {
let n = 0;
return {
count: () => n++,
reset: () => n = 0
};
};
@BIANCAGANDO
FEM 2017
const countClues = () => {
let n = 0;
return {
count: () => n++,
reset: () => n = 0
};
};
const c = countClues(), d = countClues();
c.count();
d.count();
c.reset();
c.count();
d.count();
@BIANCAGANDO
FEM 2017
Closure RECIPE
@BIANCAGANDO
FEM 2017
EXECUTION
Text
@BIANCAGANDO
FEM 2017
GOTCHA!
const findSomeone = () => { const speak = () => { console.log(who); }; let who = 'Why hello there, Prof Plum!'; return speak; };const someoneSpeak = findSomeone()
@BIANCAGANDO
FEM 2017
const makeTimer = () => {
let elapsed = 0;
const stopwatch = () => { return elapsed; };
const increase = () => elapsed++;
setInterval(increase, 1000);
return stopwatch;
};
let timer = makeTimer();
@BIANCAGANDO
FEM 2017
const makeTimer = () => {
console.log('initialized');
let elapsed = 0;
console.log(elapsed);
const stopwatch = () => {
console.log('stopwatch');
return elapsed;
};
const increase = () => elapsed++;
setInterval(increase, 1000);
return stopwatch;
};
const timer = makeTimer();
FEM Nov 2017
@BiancaGando
And the murder is...
HAH! No cheating.
By Bianca Gandolfo
Slides to accompany the frontend masters course, Fundamentals to Functional Programming v2. http://www.frontendmasters.com