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

feat: adding aggregate operation #64

Closed
wants to merge 3 commits into from

Conversation

Farenheith
Copy link
Member

@Farenheith Farenheith commented Apr 19, 2023

The aggregation operation allows you to execute several aggregations in just one command.
Here is an example of use:

const result = fluent([10, 20, 30]).aggregate((x, agg) => ({
        avg: agg.avg(x),
        avg2: agg.avg(x * 2),
        first: agg.first(x),
        first2: agg.first(x % 20 === 0),
        last: agg.last(x),
        last2: agg.last(x % 40 === 0),
        max: agg.max(x),
        max2: agg.max(1 / x),
        min: agg.min(x),
        min2: agg.min(1 / x),
        modMultiply: agg.modMultiply(x, 11),
        modMultiply2: agg.modMultiply(x * 2, 11),
        modSum: agg.modSum(x, 11),
        modSum2: agg.modSum(x * 2, 11),
        multiply: agg.multiply(x),
        multiply2: agg.multiply(x * 2),
        sum: agg.sum(x),
        sum2: agg.sum(x * 2),
}));

In the example, result will resolve to the following value:

{
        avg: 20,
        avg2: 40,
        first: 10,
        first2: 20,
        last: 30,
        last2: undefined,
        max: 30,
        max2: 10,
        min: 10,
        min2: 30,
        modMultiply: 5,
        modMultiply2: 7,
        modSum: 5,
        modSum2: 10,
        multiply: 6000,
        multiply2: 48000,
        sum: 60,
        sum2: 120,
}

Look that every distinct call for each aggregation method generates a distinct aggregation, so, you can use multiple times the same aggregation!
There are some limitations though: it's important that every call is always made in the same order, otherwise, this control will not work.
But let's say, for any reason, you can't follow this direction. In this case, you can indicate to the aggregation function an id to orientate it:

const result = fluent([5, 6, 9]).aggregate((x, agg, acc) =>
    acc + (x % 2 === 1 ? agg.modSum(x, 3, 1) : agg.modSum(x, 5, 2)),
    0,
);

The example above will generate a result equal to (5 mod 3) + (6 mod 5) + ((5 + 9) mod 3), which is 5.

In general, is better to keep things simple to understand unless extremely necessary, as aggregations with such bifurcations can be quite hard to understand and to debug.

src/recipes/aggregate-recipe.ts Outdated Show resolved Hide resolved
src/recipes/aggregate-recipe.ts Show resolved Hide resolved
src/recipes/aggregate-recipe.ts Show resolved Hide resolved
src/recipes/aggregate-recipe.ts Show resolved Hide resolved
src/recipes/aggregate-recipe.ts Show resolved Hide resolved
@Farenheith Farenheith force-pushed the 25-create-aggregate-operation branch from 64c9320 to 7ca7c96 Compare April 19, 2023 22:22
@sonarcloud
Copy link

sonarcloud bot commented Apr 20, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 6 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@codeclimate
Copy link

codeclimate bot commented Apr 20, 2023

Code Climate has analyzed commit 2383a06 and detected 4 issues on this pull request.

Here's the issue category breakdown:

Category Count
Duplication 4

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 98.8%.

View more on Code Climate.

@sonarcloud
Copy link

sonarcloud bot commented Jul 2, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 9 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@Farenheith Farenheith marked this pull request as draft November 7, 2024 01:17
@Farenheith
Copy link
Member Author

This operation is fast, but I'm not sure how useful it is and just an overkill.

Copy link

sonarcloud bot commented Nov 11, 2024

@Farenheith
Copy link
Member Author

This operation has lost to branch:

separated x 35,916 ops/sec ±1.28% (91 runs sampled)
manual branch x 45,482 ops/sec ±0.52% ([9](https://github.com/codibre/fluent-iterable/actions/runs/11777649581/job/32802469594#step:7:10)4 runs sampled)
banching x 80,455 ops/sec ±4.94% (34 runs sampled)
aggregate x 43,233 ops/sec ±0.52% (93 runs sampled)

Closing it

@Farenheith Farenheith closed this Nov 11, 2024
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

Successfully merging this pull request may close these issues.

Create aggregate operation
1 participant