Skip to content
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,8 @@ The following window reducers are supported:
* *variance* - the variance per [Welford’s algorithm](https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_online_algorithm)
* *difference* - the difference between the last and first window value
* *ratio* - the ratio of the last and first window value
* *first* - the first value
* *last* - the last value
* a function to be passed an array of *k* values

By default, **anchor** is *middle* and **reduce** is *mean*.
Expand Down
22 changes: 22 additions & 0 deletions src/transforms/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ function maybeReduce(reduce = "mean") {
case "variance": return reduceSubarray(variance);
case "difference": return reduceDifference;
case "ratio": return reduceRatio;
case "first": return reduceFirst;
case "last": return reduceLast;
}
}
if (typeof reduce !== "function") throw new Error("invalid reduce");
Expand Down Expand Up @@ -130,3 +132,23 @@ function reduceRatio(k, s) {
}
};
}

function reduceFirst(k, s) {
return {
map(I, S, T) {
for (let i = 0, n = I.length - k; i < n; ++i) {
T[I[i + s]] = S[I[i]];
}
}
};
}

function reduceLast(k, s) {
return {
map(I, S, T) {
for (let i = 0, n = I.length - k; i < n; ++i) {
T[I[i + s]] = S[I[i + k - 1]];
}
}
};
}