-
-
Save stephen-lazaro/d8a36d1370749ec70ac1144cf401061e to your computer and use it in GitHub Desktop.
This file contains 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
// Again, producing an aggregation | |
const makeAggregation = (operation, initial) => ({ | |
operation: operation, | |
initialValue: initial | |
}); | |
// Again, combining aggregations | |
const combineAggregationsReduction = (combined, next) => | |
makeAggregation((prior, nextValue) => | |
// A bit finicky, admittedly | |
[ | |
…combined.operation( | |
prior.slice(0, -1), | |
nextValue | |
), | |
next.operation( | |
prior[prior.length - 1], | |
nextValue | |
) | |
], | |
[…combined.initialValue, next.initialValue] | |
); | |
// Again, just setting up the first aggregation | |
const liftToArray = aggregate => | |
makeAggregation( | |
([a], b) => [aggregate.operation(a, b)], | |
[aggregate.initialValue] | |
); | |
// Again, reduce our list of aggregates into one aggregate | |
const combineAggregation = aggregates => | |
aggregates.slice(1).reduce( | |
combineAggregationsReduction, | |
liftToArray(aggregates[0]) | |
); | |
// Now, we can combine arbitrarily many dynamically | |
// determined aggregations to later run in a single pass | |
const getAggregates = aggregates => { | |
const combined = combineAggregation(aggregates); | |
return reports => | |
reports.reduce( | |
combined.operation, | |
combined.initialValue | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment