go-functional icon indicating copy to clipboard operation
go-functional copied to clipboard

Add CollectErr consumer and related helpers

Open BooleanCat opened this issue 1 year ago • 2 comments

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.

  • [X] I have read and understood the CONTRIBUTING guidelines
  • [X] All commits in my PR conform to the commit hygiene section
  • [X] I have added relevant tests
  • [X] I have not added any dependencies

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 avatar Jul 07 '24 10:07 BooleanCat

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

BooleanCat avatar Jul 07 '24 10:07 BooleanCat

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
@@            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.

codecov[bot] avatar Jul 07 '24 10:07 codecov[bot]

@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?

jlc-christie avatar Jul 08 '24 07:07 jlc-christie

@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()...)

BooleanCat avatar Jul 17 '24 19:07 BooleanCat