feat(collections/unstable): accept Iterable in interleave#7088
feat(collections/unstable): accept Iterable in interleave#7088tomas-zijdemans wants to merge 6 commits intodenoland:mainfrom
Iterable in interleave#7088Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7088 +/- ##
=======================================
Coverage 94.43% 94.43%
=======================================
Files 630 630
Lines 50566 50584 +18
Branches 8969 8975 +6
=======================================
+ Hits 47750 47768 +18
Misses 2247 2247
Partials 569 569 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
bartlomieju
left a comment
There was a problem hiding this comment.
Widening from ReadonlyArray to Iterable is a nice improvement. A few concerns:
The two-array fast path has subtle ordering differences for unequal-length inputs
The general path interleaves in round-robin order: it picks one from each array per round, skipping exhausted arrays. The two-array fast path emits all remaining elements from a first, then all remaining from b. For equal-length inputs these are equivalent, but for unequal-length inputs the behavior differs from the general path.
Example with 3+ arrays (general path): interleave([1,2,3], ["a"], [true, false]) produces [1, "a", true, 2, false, 3] — continuing round-robin after "a" is exhausted.
With the two-array fast path: interleave([1,2,3], ["a"]) would produce [1, "a", 2, 3] — same result as general path here since there are only two arrays. Actually on closer inspection, for two arrays the tail elements only come from one side, so it matches. This is fine.
Missing test coverage for the fast paths
The new tests only cover iterables (Set, generator). There are no tests specifically exercising:
- The two-array fast path with unequal lengths (to verify it matches general-path semantics)
- The equal-length fast path with 3+ arrays
These fast paths should have explicit tests confirming identical output to the general path.
Array.isArray check vs Array.from for all inputs
The approach is sensible — avoid copying arrays that are already arrays. One edge case: a subclass of Array would pass Array.isArray but might have overridden iteration behavior. This is extremely unlikely in practice, just noting it.
|
Agree with all points. Here is what I changed:
|
bartlomieju
left a comment
There was a problem hiding this comment.
The Iterable widening looks good — clean API improvement. Two minor items:
minLength is computed for all calls but only used by the two-array fast path
Move it inside the if (arrayCount === 2) block (or just compute it there as Math.min(a.length, b.length)). No need to track it for 3+ inputs.
Missing test: first array shorter than second
The "handles second array longer" test has [1, 2, 3], ["a"] (first is longer). Add a test with the first array shorter, e.g. interleave([1], ["a", "b", "c"]) → [1, "a", "b", "c"], to exercise the second tail loop in the fast path.
|
Good catch. I dropped For the test "first array shorter than second" is already available. See line 30 in the test file |
interleavenow widens the API toIterablewithout extra cost for array callers. This is in line with the general direction of the collections module.interleaveis very close tozipthat does not supportIterable, but looking at the module as a whole, I think it'szipthat needs to change.Also added a two-array fast path (no inner loop, no per-pair bounds check) and a branch for equal-length inputs where the length guard is unnecessary. This should help with two very common use cases. The general unequal-length path is unchanged.