feat(array): add cartesianProduct and combinations#1713
Merged
Conversation
Adds two array utilities for enumerating tuples: - `product(...arrs)` returns the Cartesian product in lexicographic order. - `combinations(arr, r)` returns all r-length combinations. Interface follows Python's `itertools.product` and `itertools.combinations`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1713 +/- ##
=======================================
Coverage 99.85% 99.85%
=======================================
Files 500 502 +2
Lines 4685 4730 +45
Branches 1361 1368 +7
=======================================
+ Hits 4678 4723 +45
Misses 7 7 🚀 New features to boost your workflow:
|
Replace the "lexicographic order / odometer" shorthand with a step-by-step description of how the rightmost array advances first and resets when its left neighbor advances. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Disambiguate from arithmetic product. The math term "Cartesian product" makes the intent obvious at the call site. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
Closes #1665, #1061
Adds two array utilities for enumerating tuples:
product(...arrs)— Cartesian product of input arrays, in lexicographic order with the rightmost array advancing fastest. Returns[[]]with no args; returns[]if any input is empty. Overloads (1–4 arrays) preserve tuple element types.combinations(arr, r)— allr-length combinations fromarr, in lexicographic order by position. Throws on negative or non-integerr. Returns[[]]whenr === 0,[]whenr > arr.length.Interface follows Python's
itertools.productanditertools.combinations. Algorithm forcombinationsis the standard index-odometer approach from the Python reference implementation.productallocates the result up front and fills each tuple with positional modulo arithmetic, avoiding intermediate prefix arrays.Docs added in all four languages (en/ko/ja/zh_hans).
Test plan
yarn vitest run src/array/product src/array/combinations— 16 tests passing locallyyarn tsc --noEmit— cleanyarn eslint src/array/product.ts src/array/product.spec.ts src/array/combinations.ts src/array/combinations.spec.ts src/array/index.ts— clean🤖 Generated with Claude Code