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

Aggregate operations with (and without) groupBy #45

Open
dogoku opened this issue May 19, 2019 · 1 comment
Open

Aggregate operations with (and without) groupBy #45

dogoku opened this issue May 19, 2019 · 1 comment

Comments

@dogoku
Copy link

dogoku commented May 19, 2019

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 dogoku changed the title Aggregate operations with (and without) Aggregate operations with (and without) groupBy May 19, 2019
@tomi
Copy link
Owner

tomi commented May 30, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants