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: Add TryAggregate for Option and Result #28

Merged
merged 1 commit into from
Jan 1, 2025

Conversation

skarllot
Copy link
Contributor

This PR introduces the TryAggregate method for the Option and Result types, enabling aggregation with built-in short-circuiting for failure cases.

Key Features

  • TryAggregate for Option: Aggregates a sequence, returning Some on success or None on the first failure.
  • TryAggregate for Result: Aggregates a sequence, propagating the first Err or returning the accumulated Ok value.

Example Usage

Option:

var result = numbers.TryAggregate(0, (acc, item) => item > 0 ? Some(acc + item) : None);
// Returns: Some(x) or None

Result:

var result = numbers.TryAggregate(0, (acc, item) => item < 5 ? Ok(acc + item) : Err("Too large"));
// Returns: Ok(x) or Err("Too large")

Benefits

  • Simplifies conditional sequence aggregation.
  • Reduces boilerplate for error propagation in Option and Result.

Testing & Documentation

  • Comprehensive unit tests validate various scenarios.
  • Documentation added for both Option and Result.

@skarllot
Copy link
Contributor Author

I don't think that it applies to Validation but I'm open for suggestions.

@@ -19,7 +20,7 @@ public static partial class Option
[GenerateTransformer]
public static Validation<Option<TValid>, TInvalid> Traverse<T, TValid, TInvalid>(
this Option<T> source,
Func<T, Validation<TValid, TInvalid>> selector)
[InstantHandle] Func<T, Validation<TValid, TInvalid>> selector)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always wondered if there was an attribute to tell the analyzer that the function is being invoked immediately - didn't know this was it, thanks!

@jeffijoe jeffijoe merged commit aaef39c into taxfyle:main Jan 1, 2025
2 checks passed
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