From 64424297274f037afda5eaa785b5815305d29335 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20L=C3=BCck?= Date: Mon, 16 Oct 2017 09:02:02 +0200 Subject: [PATCH] Minor optimizations to collection flattening --- .../Collections/OptionCollectionExtensions.cs | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Optional/Collections/OptionCollectionExtensions.cs b/src/Optional/Collections/OptionCollectionExtensions.cs index f414ffe..da6c77f 100644 --- a/src/Optional/Collections/OptionCollectionExtensions.cs +++ b/src/Optional/Collections/OptionCollectionExtensions.cs @@ -18,7 +18,14 @@ public static class OptionCollectionExtensions public static IEnumerable Values(this IEnumerable> 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; + } + } } /// @@ -30,7 +37,14 @@ public static IEnumerable Values(this IEnumerable> source) public static IEnumerable Values(this IEnumerable> 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; + } + } } /// @@ -43,15 +57,13 @@ public static IEnumerable Exceptions(this IEnumerable { if (source == null) throw new ArgumentNullException(nameof(source)); - IEnumerable ExceptionToEnumerable(Option option) + foreach (var option in source) { if (!option.HasValue) { yield return option.Exception; } } - - return source.SelectMany(ExceptionToEnumerable); } ///