feat(iterator): add es-toolkit/iterator lazy iterator module (Phase 1)#1815
Open
raon0211 wants to merge 3 commits into
Open
feat(iterator): add es-toolkit/iterator lazy iterator module (Phase 1)#1815raon0211 wants to merge 3 commits into
raon0211 wants to merge 3 commits into
Conversation
Introduce `es-toolkit/iterator` (data-first) and `es-toolkit/fp/iterator` (data-last, pipe-friendly), built on native Iterator Helper semantics: functions operate on the single-shot iterators returned by `.values()` and return lazy `IteratorObject`s whose prototype is `Iterator.prototype`, so results chain with native `.map()/.take()/.toArray()` etc. Functions already covered by the TC39 Iterator Helpers (map/filter/take/drop/ flatMap/reduce/toArray/...) are intentionally NOT reimplemented in the data-first module — only operators the proposal lacks are added. The pipe module exposes thin data-last adapters (native delegation for the standard helpers, wrappers for the rest) so they compose in `pipe` without changes to the existing lazy machinery. Phase 1 surface: - iterator (data-first): range, iterate, takeWhile, dropWhile, chunk, zip, scan, uniqBy, toSet, count, partition - fp/iterator (data-last): the above + native-delegating map, filter, take, drop, flatMap, toArray, reduce, forEach, find, some, every Implementation uses hand-rolled `next()` iterator objects rather than generators (measured ~2x faster for the same transforms). Packaging mirrors the fp module: dev + publishConfig exports, files entries, and postbuild root-export shims (including a nested fp/iterator shim).
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
- Remove `iterate`: niche infinite generator outside the module's pragmatic scope; numeric sequences are covered by `range`. - Remove `toSet`: redundant with native `new Set(iterator)` since the lazy iterators are iterable. - Add `head` (data-first + fp/iterator): returns the first element or undefined, reading a single element so it is safe on infinite iterators and useful as a pipe terminal where native `.next().value` is awkward.
Restore `iterate(seed, getNext)`, a lazy infinite source that yields `seed, getNext(seed), getNext(getNext(seed)), ...` — the standard "iterate" of Haskell/Clojure/Kotlin generateSequence/Java Stream.iterate. Data-first only (it is a source used as a pipe's first argument), so no fp/iterator wrapper.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Adds
es-toolkit/iterator— a lazy-by-default utility module for iterators — pluses-toolkit/fp/iteratorfor use insidepipe. This is Phase 1: a representative slice that locks the design pattern before expanding.The module mirrors native TC39 Iterator Helper semantics: functions take the single-shot iterators returned by
.values()and return lazyIteratorObjects. The result objects are created withObject.create(Iterator.prototype), so they are iterable, single-shot, and chain with native.map()/.filter()/.take()/.toArray()— measured to add no overhead over a plain object literal.Design decisions
map,filter,take,drop,flatMap,reduce,toArray,forEach,some,every,find) are not reimplemented in the data-first module — per es-toolkit's "don't implement Stage 3+ proposals" principle. Only operators the proposal lacks are added. For the same reasontoSetis not provided (new Set(iterator)already does it).es-toolkit/fp/iteratorprovides the data-last/pipe surface. Inside apipeyou can't chain native.map()ergonomically, so this module exposes thin data-last adapters: native delegation (map(fn) => it => Iterator.from(it).map(fn)) for the standard helpers, and wrappers around the data-first functions for the rest. They carry no.lazymetadata, so they compose through the existingpipewhile staying fully lazy — no changes to the lazy machinery.next()over generators. A quick benchmark (map → filter → consume, 1M elements) showed manual iterator objects ~2× faster thanyield-based generators, and faster than native helpers. The internaliterator()helper absorbs the boilerplate.Iterator.fromonly where a method is called (the fp native-delegation adapters), not in every data-first function — those only call.next(), which works on any iterator.Phase 1 surface
es-toolkit/iterator(data-first):range,iterate,takeWhile,dropWhile,chunk,zip,scan,uniqBy,head,count,partitiones-toolkit/fp/iterator(data-last): the above (except therange/iteratesources) + native-delegatingmap,filter,take,drop,flatMap,toArray,reduce,forEach,find,some,everyNotable behavior choices
zipstops at the shortest source (rather than padding to the longest like arrayzip), so it stays safe with infinite iterators.scanemits the initial value first (scan-left), so output length isn + 1.headis provided as a named terminal even thoughiterator.next().valuecovers it, because it can't be chained ergonomically insidepipe.iterate/rangeare infinite-capable sources and must be bounded by a short-circuiting helper (take,takeWhile) before being consumed.Testing
src/iteratorandsrc/fp/iterator, including laziness proofs (side-effect counters), early-termination on infinite sources, single-shot contract, and native-helper chaining.tsc --noEmitandeslintclean.Follow-ups (next phases)
windowed,flatten,intersperse,concat,pairwise,tail,compact, …) and terminals (last,min/max,sum/sumBy,groupBy, …).sortBy,reverse,shuffle, …) and docs in all 4 languages. Per the fp precedent, per-function docs are deferred from this PR.