Skip to content

Instantly share code, notes, and snippets.

@julienetie
julienetie / ho-js-8.js
Last active July 4, 2020 15:44
ho-js-8.js
const distance = (ax, ay) => (bx, by) => Math.hypot(ax - bx, ay - by);
const finalDistance = distance(5,5);
/*
console.log(finalDistance(10,10)) // 7.0710678118654755
*/
@julienetie
julienetie / ho-js.7.js
Last active July 4, 2020 15:45
ho-js.7.js
/* Pseudo-Functional JavaScript */
const rec = height => width => ({
get area (){return height * width}
});
/*
// Assume 1 value is temporarily available
const calcArea = rec(10);
const square = calcArea(10); // Now the other arrives
console.log(square.area); // 100
@julienetie
julienetie / ho-js.6.js
Last active July 4, 2020 14:38
ho-js.6
/* Pseudo-Functional JavaScript */
const rectangle = (height, width) => {};
// Modules disciplined with pure functons do not
// need to protect against parameter mutation.
@julienetie
julienetie / ho-js.5.1.js
Last active July 4, 2020 15:43
ho-js.5.1.js
/* Higher-Order JavaScript */
const animal = name => ({
speak: () => console.log(`${name} makes a noise.`)
});
const dog = (parent, name) => {
return Object.assign({}, parent(name), {
speak () { console.log(`${name} barks.`) }
});
};
/* Object Oriented JavaScript */
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise.`);
}
}
@julienetie
julienetie / ho-js.4.js
Last active July 4, 2020 12:41
ho-js.4.js
/*
If you don't want something mutable to remain mutable, create a new context.
*/
const createBasket = (externalMutable) => {
const privateConstant = 123;
let privateMutable = 'abc';
return {
get getValues(){ // New context
return {privateConstant, privateMutable, externalMutable};
},
@julienetie
julienetie / ho-js-3.1.js
Last active July 4, 2020 15:43
ho-js-3.1.js
/* Higher-Order JavaScript */
const point = (x, y) => {
point.distance = (a, b) => {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.hypot(dx, dy);
};
Object.freeze(point);
return {x, y};
@julienetie
julienetie / ho-js-3.2.js
Last active July 4, 2020 14:55
ho-js-3.2.js
/* Object Oriented JavaScript */
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
static distance(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
@julienetie
julienetie / ho-js-1.2.js
Last active July 4, 2020 11:19
ho-js-1.2.js
/* Higher-Order JavaScript */
const rectangle = (height, width) => {
return () => {
// "height" and "width" are available privately
}
}
@julienetie
julienetie / ho-js-2.2.js
Last active July 4, 2020 15:42
ho-js-2.2.js
/* Higher-Order JavaScript */
const rectangle = (height, width) => {
const calcArea = () => height * width;
return {
get area(){
return calcArea();
},
calcArea
}
};