fromfrom icon indicating copy to clipboard operation
fromfrom copied to clipboard

Aggregate operations with (and without) groupBy

Open dogoku opened this issue 5 years ago • 1 comments

Hi, I was wondering if there are any plans to add basic aggregate operators, i.e sum, avg, min, max, etc, for a whole collection or as part of a groupBy operation?

Alternatively do you have any suggestions on how to efficiently implement per group aggregation? So far I've come up with this (from your playground examples), but perhaps there's a better approach:

import { from } from "fromfrom";
import data from "./data";

const result = from(data)
  .groupBy(u => u.country)
  .toObject(
    g => g.key,
    g => ({
      items: g.items,
      count: g.items.length,
      sum: g.items.reduce((s, i) => s + i.score, 0),
      avg: g.items.reduce((s, i) => s + i.score, 0) / g.items.length,
      min: g.items.reduce((s, i) => Math.min(s, i.score), Infinity),
      max: g.items.reduce((s, i) => Math.max(s, i.score), -Infinity),
    })
  );

P.s: In case you are interested, I've pitted a number of similar libs and fromfrom consistently comes on top in terms of perf (atleast in Firefox, other browsers are temperamental): https://codesandbox.io/s/angry-wind-zu0gj

The fact that it is also the smallest in size, 100% coverage, type-safe, is admirable.

dogoku avatar May 19 '19 08:05 dogoku

Hi @dogoku and thank you for your questions and comments.

Regarding aggregate operations, the answer is in short yes. It would be possible (and not too hard) to add operations like sum, avg, min, max, etc. The only concern I have is that they only make sense for numeric data. I wanted to get started with a smaller API surface and extend when need be. I think it's also important to considered case by case what should be added and what should not be, to keep the API (and lib) from getting too bloated.

I think the way you are aggregating data per group in your example is the best way to do it at the moment. If (and when) we have those separate aggregate operators, they can be used to simplify the example.

And really cool to hear that you have done benchmarking! I haven't had the time myself yet to do any performance comparisons, but it's really encouraging to hear that the performance is at least not too bad at the moment.

tomi avatar May 30 '19 13:05 tomi