feat: add IEnumerable<T>.Choose for Maybe

This commit is contained in:
Kyle Ratti 2022-12-23 12:35:10 -05:00
parent a045c76140
commit dafaae0fab
No known key found for this signature in database
GPG Key ID: 4D429B6287C68DD9

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace FruityFoundation.Base.Structures;
@ -23,4 +24,10 @@ public static class MaybeExtensions
public static T? ToNullable<T>(this Maybe<T> item) where T : struct =>
item.HasValue ? item.Value : null;
public static IEnumerable<TOutput> Choose<TInput, TOutput>(this IEnumerable<TInput> enumerable, Func<TInput, Maybe<TOutput>> mapper) =>
enumerable
.Select(mapper)
.Where(x => x.HasValue)
.Select(x => x.Value);
}