Feature: Extensions for IEnumerable<Maybe<T>>
See original GitHub issueI was wondering if there was any value in extensions like these:
public static IEnumerable<T> MapValues<T, K>(this IEnumerable<Maybe<T>> maybes) =>
maybes.Where(m => m.HasValue).Select(m => m.Value);
public static IEnumerable<K> MapValues<T, K>(this IEnumerable<Maybe<T>> maybes, Func<T, K> selector) =>
maybes.Where(m => m.HasValue).Select(m => selector(m.Value));
I do run into situations where I have a collection of Maybe<T> and I want to filter to only the items in the collection that have a value and then map that value.
Currently, I use the implementation maybes.Where(m => m.HasValue).Select(m => m.Value), and even though there’s no other way to do it, I’d prefer not to see it in my code 😃
I’m not sold on the name .MapValues(), but I’m not sure what would be appropriate.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (6 by maintainers)
Top Results From Across the Web
c# - Extension Method on IEnumerable
You don't need an extension method. You can just say var sumOfSquares = list.Sum(x => x * x);. Obviously you can make it...
Read more >The Maybe Monad in C#: More methods
Here, we invoke GetLogContents twenty times. The Select method returns an enumerable of type IEnumerable<Maybe<string>>. GetItemsWithValue ...
Read more >Lets build a maybe library in C# - Maybe extensions
I thought I would add a couple of extensions to the Maybe monad to show how using ... IMaybe<T> MaybeFirst<T>(this IEnumerable<T> enumerable) =>...
Read more >IEnumerable should have a extension for creating fixed ...
I believe most projects in need of this functionality have their own extension to IEnumerable<T> and the goal of this proposal is to...
Read more >c# - Extension method replacing elements from an ...
The only advantage is in the function which utilizes iterator because it's a bit shorter but it's actually the slowest out of the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Yeah, I use
maybes.Where().Select()a lot too. A new extension method wouldn’t hurt. Regarding naming,FilterValueswould probably be better. The method contains both filtration (Where) and mapping (Select), but I think the emphasis here should be on filtration, the mapping part is going to be obvious from the signature. I’m open for other suggestions too, though.I didn’t know about
Choosein F#, but now that I read about it, it makes sense. Just for future reference, here’s the difference betweenchooseandfilter:Let’s keep
Choose. We’ve already gone the F# way when we separated differentOnSuccessoverloads intoMapandBind, so I prefer to stick to the F# style for consistency.@seangwright Regarding the documentation, I’ve been planning to do it for a long time, but currently my priority is the new course, I hope to work on the documentation once I finish it. Your readme updates look good, feel free to submit a PR, I’ll merge them. I also plan to do a separate website with the docs, similar to FluentValidation ( https://fluentvalidation.net/ ), the readme file will be the backbone for that future docs website.
Edit: I see that you already submitted a PR, looking into at.