BooleanCat / go-functional

go-functional is a library of iterators to augment the standard library
MIT License
405 stars 23 forks source link

Add CollectErr consumer and related helpers #125

Closed BooleanCat closed 1 month ago

BooleanCat commented 2 months ago

Please provide a brief description of the change.

A common form of iteratoring with an iter.Seq will be pairs of values where the right side is an error (for exampling iterating over lines in an io.Reader. CollectErr is a helper that will collect the left values from the iterator and the right error values will be joined into a single error.

Which issue does this change relate to?

None.

Contribution checklist.

Additional context

I've also added some helper functions to extend the power of this change. op.Apply{Left/Right} are to be used with Map2 in order to apply a function to only left or right values. I'm considering Filter{Left/Right} also.

BooleanCat commented 2 months ago

@jlc-christie Can you think of a better name than CollectErr?

codecov[bot] commented 2 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 100.00%. Comparing base (2c10c17) to head (6dee8f3).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #125 +/- ## ========================================= Coverage 100.00% 100.00% ========================================= Files 31 31 Lines 367 378 +11 ========================================= + Hits 367 378 +11 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

jlc-christie commented 2 months ago

@jlc-christie Can you think of a better name than CollectErr?

I think that makes sense since we're not being generic over the right side iterator, although do you already support a CollectLeft and CollectRight? Might be beneficial to add those and then defer to CollectRight with a type constraint for CollectErr implementation?

BooleanCat commented 1 month ago

@jlc-christie Can you think of a better name than CollectErr?

I think that makes sense since we're not being generic over the right side iterator, although do you already support a CollectLeft and CollectRight? Might be beneficial to add those and then defer to CollectRight with a type constraint for CollectErr implementation?

Currently the way to collect the two values separately would be to do something like:

lines, errs := itx.LinesString(reader).Unzip()
return lines.Collect(), errors.Join(errs.Collect()...)