This file contains 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 deepMapWith = (fn) => | |
function innerdeepMapWith (tree) { | |
return Array.prototype.map.call(tree, (element) => | |
Array.isArray(element) | |
? innerdeepMapWith(element) | |
: fn(element) | |
); | |
} | |
/** |
This file contains 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 pluckWith = (attr) => mapWith(getWith(attr)); | |
// or even better | |
const pluckWith = compose(mapWith, getWith); | |
/** | |
* Example | |
*/ | |
const inventories = [ |
This file contains 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 getWith = (attr) => (object) => object[attr] | |
/** | |
* Examples: | |
*/ | |
const inventory = { | |
apples: 0, | |
oranges: 144, | |
eggs: 36 | |
}; |
This file contains 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 memoized = (fn) => { | |
const lookupTable = {}; | |
return function (...args) { | |
const key = JSON.stringify(this, args); | |
return lookupTable[key] || (lookupTable[key] = fn.apply(this, args)); | |
} | |
} |
This file contains 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
// Copy an object by extending an empty object: | |
Object.assign({}, { | |
apples: 12, | |
oranges: 12 | |
}) | |
//=> { apples: 12, oranges: 12 } | |
// Extend one object with another: | |
const inventory = { | |
apples: 12, |
This file contains 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 flipAndCurry = (fn) => | |
(first) => (second) => fn(second, first); | |
// Example: | |
// https://gist.github.com/stefanfrede/596597d8b544de08491364eab20053c6 | |
const mapWith = flipAndCurry(map); | |
// flipAndCurry throws the current context away, so it can’t be used to flip methods. | |
// A small alteration gets the job done: | |
const flipAndCurry = (fn) => |
This file contains 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 mapWith = (fn) => (list) => list.map(fn); | |
// Example: | |
const squaresOf = mapWith(n => n * n); | |
squaresOf([1, 2, 3, 4, 5]) | |
//=> [1, 4, 9, 16, 25] |
This file contains 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 pipeline = (...fns) => | |
(value) => | |
fns.reduce((acc, fn) => fn(acc), value); | |
// Given: | |
const addOne = (number) => number + 1; | |
const doubleOf = (number) => number * 2; |
This file contains 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 compose = (a, ...rest) => | |
rest.length === 0 | |
? a | |
: (c) => a(compose(...rest)(c)) | |
// Given: | |
const addOne = (number) => number + 1; | |
const doubleOf = (number) => number * 2; |
This file contains 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 compose = (a, b) => | |
(c) => a(b(c)) | |
// Given: | |
const addOne = (number) => number + 1; | |
const doubleOf = (number) => number * 2; | |
// Instead of: |