question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature: Extensions for IEnumerable<Maybe<T>>

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
vkhorikovcommented, Sep 13, 2021

Yeah, I use maybes.Where().Select() a lot too. A new extension method wouldn’t hurt. Regarding naming, FilterValues would 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.

1reaction
vkhorikovcommented, Sep 19, 2021

I didn’t know about Choose in F#, but now that I read about it, it makes sense. Just for future reference, here’s the difference between choose and filter:

List.choose : ('T -> 'U option) -> 'T list -> 'U list
List.filter : ('T -> bool) -> 'T list -> 'T list

Let’s keep Choose. We’ve already gone the F# way when we separated different OnSuccess overloads into Map and Bind, 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found