export function* map(xs, fn) { for (const x of xs) { yield fn(x); } } export function reduce(xs, fn, initial) { let value = initial; for (const x of xs) { value = fn(value, x); } return value; } export function* chunkBy(xs, keyFn, coalesceFn) { let chunk; let key; for (const x of xs) { const newKey = keyFn(x); if (key === undefined) { chunk = [x]; } else if (coalesceFn(key, newKey)) { chunk.push(x); } else { yield { key, chunk }; chunk = [x]; } key = newKey; } if (chunk !== undefined) { yield { key, chunk }; } }