Skip to content

Instantly share code, notes, and snippets.

@airportyh
Created June 25, 2018 15:15
Show Gist options
  • Select an option

  • Save airportyh/969922c82236e438c810b23bd85c8bb2 to your computer and use it in GitHub Desktop.

Select an option

Save airportyh/969922c82236e438c810b23bd85c8bb2 to your computer and use it in GitHub Desktop.
Faith's FP lesson examples
var lhs = 5;
var rhs = 8;
var operator = "+";
var result;
if (operator === "+") {
result = lhs + rhs;
} else if (operator === "-") {
result = lhs - rhs;
} else if (operator === "*") {
result = lhs * rhs;
} else if (operator === "/") {
result = lhs / rhs;
}
var fnDict = {
"+": function(lhs, rhs) { // normal function
return lhs + rhs;
},
"-": (lhs, rhs) => { // arrow function
return lhs - rhs;
},
"*": (lhs, rhs) => lhs * rhs, // implied return if no { } s
"/": (lhs, rhs) => lhs / rhs
};
var lhs = 5;
var rhs = 8;
var operator = "+";
var result;
var fn = fnDict[operator];
result = fn(lhs, rhs);
// or in one line:
// result = fnDict[operator](lhs, rhs);
// variable declaration and assignment, where value is a function expression.
let hello = function (subject) {
console.log("Hello, " + subject + "!");
};
var goodBye = function(subject) {
console.log("Goodbye, " + subject + "!");
};
// or as an arrow function
// let hello = () => {
// console.log("Hello");
// };
// vs a function statement
// function hello() { // a top level named function definition
// console.log("Hello"); // aka a function statement in JS
// }
function call3TimesWith(fn, subject) {
fn(subject);
fn(subject);
fn(subject);
}
call3TimesWith(hello, "Sarah"); // should call hello("Sarah") 3 times
call3TimesWith(goodBye, "Sarah");
// variable declaration and assignment, where value is a function expression.
let hello = function () {
console.log("Hello");
};
// or as an arrow function
// let hello = () => {
// console.log("Hello");
// };
// vs a function statement
// function hello() { // a top level named function definition
// console.log("Hello"); // aka a function statement in JS
// }
function call3Times(fn) {
fn();
fn();
fn();
}
call3Times(hello); // should call hello 3 times
var hello = function() { // rhs is called a function expression
console.log("Hello");
};
var goodBye = function() {
console.log("Goodbye");
};
var someFn = hello; // notice we didn't call hello, i.e. hello()
hello(); // calls hello
someFn(); // calls hello
someFn = goodBye;
someFn(); // call goodbye

Higher Order Functions

A function is a value, and can be stored in a variable, passed to another function as an argument, or returned as a return value by another function.

const numbers = [1, 2, 3];
const numbersDoubled = numbers.map(n => n * 2); // [2, 4, 6]
// Break it down:
// const double = n => n * 2;
// const numbersDoubled = numbers.map(double);
// what if we had to do this ourselves?
let numbersDoubled = [];
for (let n of numbers) {
numbersDoubled.push(n * 2);
}
// what if we had to get the square of each number?
let numbersSquared = [];
for (let n of numbers) {
numbersSquared.push(n * n);
}
const students = [
{ name: "Faith", favoriteShow: "Doctor Who" },
{ name: "Rachel", favoriteShow: "Star Trek" },
{ name: "Nerando", favoriteShow: "Top Gear" },
{ name: "Kris", favoriteShow: "Doctor Who" }
];
// Want to get just the names, and then join them together
let names = [];
for (let student of students) {
names.push(student.name);
}
console.log(names.join(", "));
// Now using array's map (you didn't have to write this template yourself)
let names = students.map((student) => student.name);
console.log(names.join(", "));
// My map works just like array's .map, but instead of
// array.map(fn), you write myMap(array, fn)
// myMap or array.map is like a template, where you only need
// to substitute in one operation within a larger template
function myMap(array, fn) {
let results = [];
for (let item of array) {
results.push(fn(item));
}
return results;
}
// This line uses the template:
let names = myMap(students, (student) => student.name);
console.log(names.join(", "));
let result = [];
for (let item of array) {
result.push(n * n);
}
let result = [];
for (let item of array) {
result.push(student.name);
}
let result = [];
for (let item of array) {
result.push(n * 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment