Skip to content

Commit

Permalink
Minor optimizations to collection flattening
Browse files Browse the repository at this point in the history
  • Loading branch information
nlkl committed Oct 16, 2017
1 parent fd60f90 commit 6442429
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/Optional/Collections/OptionCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ public static class OptionCollectionExtensions
public static IEnumerable<T> Values<T>(this IEnumerable<Option<T>> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return source.SelectMany(option => option.ToEnumerable());

foreach (var option in source)
{
if (option.HasValue)
{
yield return option.Value;
}
}
}

/// <summary>
Expand All @@ -30,7 +37,14 @@ public static IEnumerable<T> Values<T>(this IEnumerable<Option<T>> source)
public static IEnumerable<T> Values<T, TException>(this IEnumerable<Option<T, TException>> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return source.SelectMany(option => option.ToEnumerable());

foreach (var option in source)
{
if (option.HasValue)
{
yield return option.Value;
}
}
}

/// <summary>
Expand All @@ -43,15 +57,13 @@ public static IEnumerable<TException> Exceptions<T, TException>(this IEnumerable
{
if (source == null) throw new ArgumentNullException(nameof(source));

IEnumerable<TException> ExceptionToEnumerable(Option<T, TException> option)
foreach (var option in source)
{
if (!option.HasValue)
{
yield return option.Exception;
}
}

return source.SelectMany(ExceptionToEnumerable);
}

/// <summary>
Expand Down

0 comments on commit 6442429

Please sign in to comment.