Skip to content

Instantly share code, notes, and snippets.

@stephen-lazaro
Last active October 5, 2018 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephen-lazaro/d8a36d1370749ec70ac1144cf401061e to your computer and use it in GitHub Desktop.
Save stephen-lazaro/d8a36d1370749ec70ac1144cf401061e to your computer and use it in GitHub Desktop.
// 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