diff --git a/Base/Structures/MaybeExtensions.cs b/Base/Structures/MaybeExtensions.cs index 55d08cc..733c574 100644 --- a/Base/Structures/MaybeExtensions.cs +++ b/Base/Structures/MaybeExtensions.cs @@ -2,8 +2,21 @@ public static class MaybeExtensions { - public static Maybe FirstOrEmpty(this IEnumerable col) => - col.FirstOrDefault() ?? Maybe.Empty(); + public static Maybe FirstOrEmpty(this IEnumerable collection) + { + using var enumerator = collection.GetEnumerator(); + + return !enumerator.MoveNext() ? Maybe.Empty() : enumerator.Current; + } + + public static Maybe FirstOrEmpty(this IEnumerable collection, Func pred) + { + foreach (var item in collection) + if (pred(item)) + return item; + + return Maybe.Empty(); + } public static T? ToNullable(this Maybe item) where T : struct => item.HasValue ? item.Value : null;