Skip to content

Commit

Permalink
Merge pull request #415 from alexmurari/patch-1
Browse files Browse the repository at this point in the history
Avoid multiple enumeration
  • Loading branch information
vkhorikov authored Jun 11, 2022
2 parents f24e7dc + d2bbca1 commit 569b3c4
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CSharpFunctionalExtensions/Maybe/MaybeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ public static IEnumerable<T> Choose<T>(this IEnumerable<Maybe<T>> source)

public static Maybe<T> TryFirst<T>(this IEnumerable<T> source)
{
source = source as ICollection<T> ?? source.ToList();

if (source.Any())
{
return Maybe<T>.From(source.First());
Expand All @@ -350,6 +352,8 @@ public static Maybe<T> TryFirst<T>(this IEnumerable<T> source, Func<T, bool> pre

public static Maybe<T> TryLast<T>(this IEnumerable<T> source)
{
source = source as ICollection<T> ?? source.ToList();

if (source.Any())
{
return Maybe<T>.From(source.Last());
Expand Down Expand Up @@ -396,4 +400,4 @@ public static void Deconstruct<T>(this Maybe<T> result, out bool hasValue, out T
value = result.GetValueOrDefault();
}
}
}
}

0 comments on commit 569b3c4

Please sign in to comment.