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 unarrow = arrowFunc => { | |
const arrowFuncDefinition = arrowFunc.toString(); | |
const hasEnclosingBraces = arrowFuncDefinition.indexOf('}') === arrowFuncDefinition.length - 1; | |
const newDefinition = | |
`return function${ | |
hasEnclosingBraces ? | |
arrowFuncDefinition.replace('=>', '') : | |
arrowFuncDefinition.replace('=>', '{return ') + '}' | |
}`; |
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 sum = (...args) => | |
Object.assign( | |
sum.bind(null, ...args), | |
{valueOf: () => args.reduce((acc, cur) => acc + cur, 0)} | |
); | |
export default sum; |
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 filter = function* (iterable, cond) { | |
const iterator = iterable[Symbol.iterator](); | |
let cursor = iterator.next(); | |
do { | |
const {value} = cursor; | |
if (cond(value)) { | |
yield value; | |
} | |
} while (!(cursor = iterator.next()).done); | |
}; |
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
/** | |
* Initializes an empty sieve | |
* | |
* @returns {{primes: Set<number>, crossed: Set<number>, greatestComputedMultiples: Map<number, number>}} | |
*/ | |
const createSieve = () => ({ | |
primes: new Set(), | |
crossed: new Set(), | |
greatestComputedMultiples: new Map(), // `prime` -> greatest computed multiple for `prime` | |
}); |
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
/** | |
* Transforms a function of the form: | |
* fn = (p1, ..., pN) => f(p1, ...pN) | |
* to its curried form: | |
* curried = p1 => p2 => ... => pN => fn(p1, p2, ..., pN); | |
* | |
* @param fn | |
* @returns the curried form of fn | |
*/ | |
const curry = 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 memoizeFactory = hashFn => fn => { | |
const memoize = fn => { | |
const cache = {}; | |
return (...args) => { | |
const key = hashFn(args); | |
if (key in cache) { | |
return cache[key]; | |
} |
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
import _ from 'lodash'; | |
const isPlainObject = value => | |
value === Object(value) && | |
!Array.isArray(value); | |
function* leaves(node, path = []) { | |
if (!isPlainObject(node)) { | |
return yield {value: node, path}; |
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
let runningPromiseCount = 0; | |
const examplePromiseRunnerQueue = new Array(10) | |
.fill(0) | |
.map((_, idx) => idx) | |
.map((idx) => { | |
return async () => { | |
console.log( | |
`[${new Date().toISOString()}] Promise ${idx} will run! \n# of running promises: ${runningPromiseCount}` | |
); |
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 DEFAULT_MAX_CONCURRENT_PROMISES = 5; | |
/** | |
* Runs a stream of promises and ensures only `max` promises are run at a time | |
* @param {*} promiseRunnerGen | |
* @param {*} max | |
*/ | |
const run = (promiseRunnerGen, max = DEFAULT_MAX_CONCURRENT_PROMISES) => { | |
const iterator = promiseRunnerGen(); | |
let runningPromiseCount = 0; |
OlderNewer