diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2025-03-23 14:33:07 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2025-03-23 14:33:07 -0400 |
| commit | 876472299d67f8fe3a789b7777b9d8ee44297b23 (patch) | |
| tree | db62f5d1e15d871f8a73ce20b40cd53053d12f85 /ui/lib/iterator.js | |
| parent | fa0f653f141efee3f5a01e1fa696d29140ec12c2 (diff) | |
| parent | f788ea84e25a4f7216ca0604aeb216346403b6ef (diff) | |
Merge branch 'prop/restartable-state'
Diffstat (limited to 'ui/lib/iterator.js')
| -rw-r--r-- | ui/lib/iterator.js | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/ui/lib/iterator.js b/ui/lib/iterator.js new file mode 100644 index 0000000..1d6a740 --- /dev/null +++ b/ui/lib/iterator.js @@ -0,0 +1,35 @@ +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 }; + } +} |
