Skip to content

Comments

[Repo Assist] Add AsyncSeq.sumBy, sumByAsync, average, averageBy, averageByAsync#245

Merged
dsyme merged 2 commits intomainfrom
repo-assist/improve-sumby-average-20260223-705926e5f065fa3a
Feb 24, 2026
Merged

[Repo Assist] Add AsyncSeq.sumBy, sumByAsync, average, averageBy, averageByAsync#245
dsyme merged 2 commits intomainfrom
repo-assist/improve-sumby-average-20260223-705926e5f065fa3a

Conversation

@github-actions
Copy link
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.


Summary

Adds five new aggregation combinators that mirror the corresponding Seq module functions, complementing the existing AsyncSeq.sum:

AsyncSeq.sumBy        : projection:('T -> ^U) -> source:AsyncSeq<'T>            -> Async< ^U>
AsyncSeq.sumByAsync   : projection:('T -> Async< ^U>) -> source:AsyncSeq<'T>    -> Async< ^U>
AsyncSeq.average      : source:AsyncSeq< ^T>                                    -> Async< ^T>
AsyncSeq.averageBy    : projection:('T -> ^U) -> source:AsyncSeq<'T>            -> Async< ^U>
AsyncSeq.averageByAsync : projection:('T -> Async< ^U>) -> source:AsyncSeq<'T> -> Async< ^U>

The average* variants raise InvalidArgumentException when the sequence is empty (same as Seq.average).

Motivation

AsyncSeq.sum already exists, and sumBy/average/averageBy are the natural companions. Users frequently need to compute weighted sums and averages over async streams (e.g. aggregating metrics, sensor readings). Having these avoids users reaching for toArrayAsync + Array.average which buffers the entire sequence.

Implementation

All functions are inline with the appropriate SRTP constraints (same pattern as sum):

  • sumBy / sumByAsync — fold with GenericZero accumulator, applying projection first
  • average / averageBy / averageByAsync — fold tracking (sum, count), then call LanguagePrimitives.DivideByInt

Test Status

Build: succeeded (no errors, 2 pre-existing warnings for groupByAsync)
Tests: all 200 tests pass (194 existing + 6 new)

New tests cover: basic correctness against List.sumBy/List.averageBy, async projections, and empty-sequence exception for average.

Generated by Repo Assist

To install this workflow, run gh aw add githubnext/agentics/workflows/repo-assist.md@2f03fdaafb8c1ae62dfde7e0be762a822a201aeb. View source at https://github.com/githubnext/agentics/tree/2f03fdaafb8c1ae62dfde7e0be762a822a201aeb/workflows/repo-assist.md.

These mirror the corresponding Seq module functions and complement the
existing AsyncSeq.sum combinator:

- sumBy / sumByAsync: map-then-sum using sync/async projection
- average: compute mean of a numeric async sequence
- averageBy / averageByAsync: map-then-average using sync/async projection

All raise InvalidArgumentException on empty sequences (average variants).
6 new tests added; all 200 tests pass.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions
Copy link
Contributor Author

Pull request created: #245

@dsyme dsyme marked this pull request as ready for review February 24, 2026 02:19
@dsyme dsyme merged commit 29d1c0c into main Feb 24, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant