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
function* cycle(list) { | |
const arr = [...list] | |
let i = 0 | |
for (;;) { | |
i = (i + ((yield arr[i]) || 1)) % arr.length | |
} | |
} | |
const it = cycle(['a', 'b', 'c']) |
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
if (!Object.values && !Object.entries) { | |
const values = o => Object.keys(o).map(x => o[x]) | |
const entries = o => Object.keys(o).map(x => [x, o[x]]) | |
Object.assign(Object, {values, entries}) | |
} |
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
class Deferred extends Promise { | |
constructor() { | |
let resolve, reject | |
super((...args) => [resolve, reject] = args) | |
Object.assign(this, {resolve, reject}) | |
} | |
} | |
Deferred.tasks = (list, limit = list.length) => { | |
const promises = list.map(task => { | |
function exec(iterator) { |
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 flattenDeep = x => Array.isArray(x) ? [].concat(...x.map(flattenDeep)) : x |
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
class SilenceablePromise<T> extends Promise<T> { | |
silence: () => void | |
constructor( | |
executor: ( | |
resolve: (value?: T | PromiseLike<T>) => void, | |
reject: (reason?: any) => void, | |
) => void, | |
) { | |
let silence: any |
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
export const autobind = (target: any, propertyKey: string, descriptor: PropertyDescriptor) => { | |
Object.defineProperty(target, propertyKey, { | |
...descriptor, | |
value: descriptor.value.bind(target), | |
}) | |
} |
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 fizzBuzz = (a, b) => Array.from(Array(b - a), (_, i) =>a + i).map(x => `${!(x % 5) ? 'Fizz' : ''}${!(x % 3) ? 'Buzz' : ''}` || x) |
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
class LazyArray extends Array { | |
constructor() { | |
super() | |
this[Array.species] = new.target | |
} | |
*lazyMap(fn) { | |
for (let i = 0; i < this.length; i++) { | |
yield fn(this[i]) | |
} |
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 mapUntil = function*<T, K>( | |
fn: (a: T) => K, | |
p: (a: K) => boolean, | |
xs: T[] | |
): IterableIterator<K> { | |
for (const x of xs) { | |
const result = fn(x) | |
if (!p(result)) { | |
break | |
} |
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 makeArray = (length, f) => Array.from({ length }, f); | |
const range = (a, b) => makeArray(Math.abs(b - a) + 1, (_, i) => a + (i * Math.sign(b - a))); |