This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const distance = (ax, ay) => (bx, by) => Math.hypot(ax - bx, ay - by); | |
| const finalDistance = distance(5,5); | |
| /* | |
| console.log(finalDistance(10,10)) // 7.0710678118654755 | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Pseudo-Functional JavaScript */ | |
| const rectangle = (height, width) => {}; | |
| // Modules disciplined with pure functons do not | |
| // need to protect against parameter mutation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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.`) } | |
| }); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Object Oriented JavaScript */ | |
| class Animal { | |
| constructor(name) { | |
| this.name = name; | |
| } | |
| speak() { | |
| console.log(`${this.name} makes a noise.`); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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}; | |
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Higher-Order JavaScript */ | |
| const rectangle = (height, width) => { | |
| return () => { | |
| // "height" and "width" are available privately | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Higher-Order JavaScript */ | |
| const rectangle = (height, width) => { | |
| const calcArea = () => height * width; | |
| return { | |
| get area(){ | |
| return calcArea(); | |
| }, | |
| calcArea | |
| } | |
| }; |