Skip to content

Conversation

BooleanCat
Copy link
Owner

@BooleanCat BooleanCat commented Jul 7, 2024

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.

  • I have read and understood the CONTRIBUTING guidelines
  • All commits in my PR conform to the commit hygiene section
  • I have added relevant tests
  • 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 BooleanCat requested a review from jlc-christie July 7, 2024 10:30
@BooleanCat BooleanCat self-assigned this Jul 7, 2024
@BooleanCat
Copy link
Owner Author

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

Copy link

codecov bot commented Jul 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jlc-christie
Copy link
Collaborator

@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
Copy link
Owner Author

@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 BooleanCat merged commit 56afa4a into main Jul 19, 2024
@BooleanCat BooleanCat deleted the collect-err branch July 19, 2024 13:17
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.

2 participants