Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lazily materialize the groups #2031

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export function keyword(input, name, allowed) {

// Promotes the specified data to an array as needed.
export function arrayify(data) {
if (typeof data?.transform === "function") data = data.transform();
return data == null || data instanceof Array || data instanceof TypedArray ? data : Array.from(data);
}

Expand Down
10 changes: 9 additions & 1 deletion src/transforms/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function groupn(
x, // optionally group on x
y, // optionally group on y
{
data: reduceData = reduceIdentity,
data: reduceData = reduceIdentityLazy,
filter,
sort,
reverse,
Expand Down Expand Up @@ -153,6 +153,7 @@ function groupn(
groupFacets.push(groupFacet);
}
maybeSort(groupFacets, sort, reverse);
if (reduceData === reduceIdentityLazy) groupData.transform = () => groupData.map((d) => d());
return {data: groupData, facets: groupFacets};
}),
...(!hasOutput(outputs, "x") && (GX ? {x: GX} : {x1, x2})),
Expand Down Expand Up @@ -236,6 +237,7 @@ export function maybeGroup(I, X) {
export function maybeReduce(reduce, value, fallback = invalidReduce) {
if (reduce == null) return fallback(reduce);
if (typeof reduce.reduceIndex === "function") return reduce;
if (typeof reduce.reduceIndexLazy === "function") return reduce;
if (typeof reduce.reduce === "function" && isObject(reduce)) return reduceReduce(reduce); // N.B. array.reduce
if (typeof reduce === "function") return reduceFunction(reduce);
if (/^p\d{2}$/i.test(reduce)) return reduceAccessor(percentile(reduce));
Expand Down Expand Up @@ -366,6 +368,12 @@ export const reduceIdentity = {
}
};

const reduceIdentityLazy = {
reduceIndex(I, X) {
return () => take(X, I);
}
};

export const reduceFirst = {
reduceIndex(I, X) {
return X[I[0]];
Expand Down