浏览代码

Simplify code using local functions

Bart De Smet 6 年之前
父节点
当前提交
15fb36bba0

+ 55 - 57
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/All.cs

@@ -17,7 +17,27 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return AllCore(source, predicate, cancellationToken);
+            return Core();
+
+            async Task<bool> Core()
+            {
+                var e = source.GetAsyncEnumerator(cancellationToken);
+
+                try
+                {
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
+                    {
+                        if (!predicate(e.Current))
+                            return false;
+                    }
+                }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
+
+                return true;
+            }
         }
 
         public static Task<bool> AllAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
@@ -27,7 +47,27 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return AllCore(source, predicate, cancellationToken);
+            return Core();
+
+            async Task<bool> Core()
+            {
+                var e = source.GetAsyncEnumerator(cancellationToken);
+
+                try
+                {
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
+                    {
+                        if (!await predicate(e.Current).ConfigureAwait(false))
+                            return false;
+                    }
+                }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
+
+                return true;
+            }
         }
 
 #if !NO_DEEP_CANCELLATION
@@ -38,69 +78,27 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return AllCore(source, predicate, cancellationToken);
-        }
-#endif
+            return Core();
 
-        private static async Task<bool> AllCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
-        {
-            var e = source.GetAsyncEnumerator(cancellationToken);
-
-            try
-            {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
-                {
-                    if (!predicate(e.Current))
-                        return false;
-                }
-            }
-            finally
+            async Task<bool> Core()
             {
-                await e.DisposeAsync().ConfigureAwait(false);
-            }
-
-            return true;
-        }
+                var e = source.GetAsyncEnumerator(cancellationToken);
 
-        private static async Task<bool> AllCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
-        {
-            var e = source.GetAsyncEnumerator(cancellationToken);
-
-            try
-            {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
+                try
                 {
-                    if (!await predicate(e.Current).ConfigureAwait(false))
-                        return false;
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
+                    {
+                        if (!await predicate(e.Current, cancellationToken).ConfigureAwait(false))
+                            return false;
+                    }
                 }
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
-            }
-
-            return true;
-        }
-
-#if !NO_DEEP_CANCELLATION
-        private static async Task<bool> AllCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
-        {
-            var e = source.GetAsyncEnumerator(cancellationToken);
-
-            try
-            {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
+                finally
                 {
-                    if (!await predicate(e.Current, cancellationToken).ConfigureAwait(false))
-                        return false;
+                    await e.DisposeAsync().ConfigureAwait(false);
                 }
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
-            }
 
-            return true;
+                return true;
+            }
         }
 #endif
     }

+ 69 - 71
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Any.cs

@@ -15,7 +15,21 @@ namespace System.Linq
             if (source == null)
                 throw Error.ArgumentNull(nameof(source));
 
-            return AnyCore(source, cancellationToken);
+            return Core();
+
+            async Task<bool> Core()
+            {
+                var e = source.GetAsyncEnumerator(cancellationToken);
+
+                try
+                {
+                    return await e.MoveNextAsync().ConfigureAwait(false);
+                }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
+            }
         }
 
         public static Task<bool> AnyAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
@@ -25,7 +39,27 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return AnyCore(source, predicate, cancellationToken);
+            return Core();
+
+            async Task<bool> Core()
+            {
+                var e = source.GetAsyncEnumerator(cancellationToken);
+
+                try
+                {
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
+                    {
+                        if (predicate(e.Current))
+                            return true;
+                    }
+                }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
+
+                return false;
+            }
         }
 
         public static Task<bool> AnyAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
@@ -35,7 +69,27 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return AnyCore(source, predicate, cancellationToken);
+            return Core();
+
+            async Task<bool> Core()
+            {
+                var e = source.GetAsyncEnumerator(cancellationToken);
+
+                try
+                {
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
+                    {
+                        if (await predicate(e.Current).ConfigureAwait(false))
+                            return true;
+                    }
+                }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
+
+                return false;
+            }
         }
 
 #if !NO_DEEP_CANCELLATION
@@ -46,83 +100,27 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return AnyCore(source, predicate, cancellationToken);
-        }
-#endif
-
-        private static async Task<bool> AnyCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
-        {
-            var e = source.GetAsyncEnumerator(cancellationToken);
+            return Core();
 
-            try
+            async Task<bool> Core()
             {
-                return await e.MoveNextAsync().ConfigureAwait(false);
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
-            }
-        }
+                var e = source.GetAsyncEnumerator(cancellationToken);
 
-        private static async Task<bool> AnyCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
-        {
-            var e = source.GetAsyncEnumerator(cancellationToken);
-
-            try
-            {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
+                try
                 {
-                    if (predicate(e.Current))
-                        return true;
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
+                    {
+                        if (await predicate(e.Current, cancellationToken).ConfigureAwait(false))
+                            return true;
+                    }
                 }
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
-            }
-
-            return false;
-        }
-
-        private static async Task<bool> AnyCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
-        {
-            var e = source.GetAsyncEnumerator(cancellationToken);
-
-            try
-            {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
+                finally
                 {
-                    if (await predicate(e.Current).ConfigureAwait(false))
-                        return true;
+                    await e.DisposeAsync().ConfigureAwait(false);
                 }
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
-            }
 
-            return false;
-        }
-
-#if !NO_DEEP_CANCELLATION
-        private static async Task<bool> AnyCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
-        {
-            var e = source.GetAsyncEnumerator(cancellationToken);
-
-            try
-            {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
-                {
-                    if (await predicate(e.Current, cancellationToken).ConfigureAwait(false))
-                        return true;
-                }
+                return false;
             }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
-            }
-
-            return false;
         }
 #endif
     }

+ 6 - 11
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Contains.cs

@@ -15,7 +15,12 @@ namespace System.Linq
             if (source == null)
                 throw Error.ArgumentNull(nameof(source));
 
-            return ContainsCore(source, value, cancellationToken);
+            if (source is ICollection<TSource> collection)
+            {
+                return Task.FromResult(collection.Contains(value));
+            }
+
+            return ContainsCore(source, value, comparer: null, cancellationToken);
         }
 
         public static Task<bool> ContainsAsync<TSource>(this IAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken = default)
@@ -26,16 +31,6 @@ namespace System.Linq
             return ContainsCore(source, value, comparer, cancellationToken);
         }
 
-        private static Task<bool> ContainsCore<TSource>(IAsyncEnumerable<TSource> source, TSource value, CancellationToken cancellationToken)
-        {
-            if (source is ICollection<TSource> collection)
-            {
-                return Task.FromResult(collection.Contains(value));
-            }
-
-            return ContainsCore(source, value, comparer: null, cancellationToken);
-        }
-
         private static async Task<bool> ContainsCore<TSource>(IAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
         {
             var e = source.GetAsyncEnumerator(cancellationToken);

+ 72 - 79
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Count.cs

@@ -16,43 +16,6 @@ namespace System.Linq
             if (source == null)
                 throw Error.ArgumentNull(nameof(source));
 
-            return CountCore(source, cancellationToken);
-        }
-
-        public static Task<int> CountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
-        {
-            if (source == null)
-                throw Error.ArgumentNull(nameof(source));
-            if (predicate == null)
-                throw Error.ArgumentNull(nameof(predicate));
-
-            return CountCore(source, predicate, cancellationToken);
-        }
-
-        public static Task<int> CountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
-        {
-            if (source == null)
-                throw Error.ArgumentNull(nameof(source));
-            if (predicate == null)
-                throw Error.ArgumentNull(nameof(predicate));
-
-            return CountCore(source, predicate, cancellationToken);
-        }
-
-#if !NO_DEEP_CANCELLATION
-        public static Task<int> CountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
-        {
-            if (source == null)
-                throw Error.ArgumentNull(nameof(source));
-            if (predicate == null)
-                throw Error.ArgumentNull(nameof(predicate));
-
-            return CountCore(source, predicate, cancellationToken);
-        }
-#endif
-
-        private static Task<int> CountCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
-        {
             switch (source)
             {
                 case ICollection<TSource> collection:
@@ -90,86 +53,116 @@ namespace System.Linq
             }
         }
 
-        private static async Task<int> CountCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
+        public static Task<int> CountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
         {
-            var count = 0;
+            if (source == null)
+                throw Error.ArgumentNull(nameof(source));
+            if (predicate == null)
+                throw Error.ArgumentNull(nameof(predicate));
 
-            var e = source.GetAsyncEnumerator(cancellationToken);
+            return Core();
 
-            try
+            async Task<int> Core()
             {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
+                var count = 0;
+
+                var e = source.GetAsyncEnumerator(cancellationToken);
+
+                try
                 {
-                    if (predicate(e.Current))
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
                     {
-                        checked
+                        if (predicate(e.Current))
                         {
-                            count++;
+                            checked
+                            {
+                                count++;
+                            }
                         }
                     }
                 }
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
-            }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
 
-            return count;
+                return count;
+            }
         }
 
-        private static async Task<int> CountCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
+        public static Task<int> CountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
         {
-            var count = 0;
+            if (source == null)
+                throw Error.ArgumentNull(nameof(source));
+            if (predicate == null)
+                throw Error.ArgumentNull(nameof(predicate));
 
-            var e = source.GetAsyncEnumerator(cancellationToken);
+            return Core();
 
-            try
+            async Task<int> Core()
             {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
+                var count = 0;
+
+                var e = source.GetAsyncEnumerator(cancellationToken);
+
+                try
                 {
-                    if (await predicate(e.Current).ConfigureAwait(false))
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
                     {
-                        checked
+                        if (await predicate(e.Current).ConfigureAwait(false))
                         {
-                            count++;
+                            checked
+                            {
+                                count++;
+                            }
                         }
                     }
                 }
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
-            }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
 
-            return count;
+                return count;
+            }
         }
 
 #if !NO_DEEP_CANCELLATION
-        private static async Task<int> CountCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
+        public static Task<int> CountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
         {
-            var count = 0;
+            if (source == null)
+                throw Error.ArgumentNull(nameof(source));
+            if (predicate == null)
+                throw Error.ArgumentNull(nameof(predicate));
 
-            var e = source.GetAsyncEnumerator(cancellationToken);
+            return Core();
 
-            try
+            async Task<int> Core()
             {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
+                var count = 0;
+
+                var e = source.GetAsyncEnumerator(cancellationToken);
+
+                try
                 {
-                    if (await predicate(e.Current, cancellationToken).ConfigureAwait(false))
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
                     {
-                        checked
+                        if (await predicate(e.Current, cancellationToken).ConfigureAwait(false))
                         {
-                            count++;
+                            checked
+                            {
+                                count++;
+                            }
                         }
                     }
                 }
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
-            }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
 
-            return count;
+                return count;
+            }
         }
 #endif
     }

+ 30 - 33
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/First.cs

@@ -15,7 +15,14 @@ namespace System.Linq
             if (source == null)
                 throw Error.ArgumentNull(nameof(source));
 
-            return FirstCore(source, cancellationToken);
+            return Core();
+
+            async Task<TSource> Core()
+            {
+                var first = await TryGetFirst(source, cancellationToken).ConfigureAwait(false);
+
+                return first.HasValue ? first.Value : throw Error.NoElements();
+            }
         }
 
         public static Task<TSource> FirstAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
@@ -25,7 +32,14 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return FirstCore(source, predicate, cancellationToken);
+            return Core();
+
+            async Task<TSource> Core()
+            {
+                var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
+
+                return first.HasValue ? first.Value : throw Error.NoElements();
+            }
         }
 
         public static Task<TSource> FirstAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
@@ -35,7 +49,14 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return FirstCore(source, predicate, cancellationToken);
+            return Core();
+
+            async Task<TSource> Core()
+            {
+                var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
+
+                return first.HasValue ? first.Value : throw Error.NoElements();
+            }
         }
 
 #if !NO_DEEP_CANCELLATION
@@ -46,38 +67,14 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return FirstCore(source, predicate, cancellationToken);
-        }
-#endif
-
-        private static async Task<TSource> FirstCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
-        {
-            var first = await TryGetFirst(source, cancellationToken).ConfigureAwait(false);
-
-            return first.HasValue ? first.Value : throw Error.NoElements();
-        }
+            return Core();
 
-        private static async Task<TSource> FirstCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
-        {
-            var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
-
-            return first.HasValue ? first.Value : throw Error.NoElements();
-        }
-
-        private static async Task<TSource> FirstCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
-        {
-            var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
-
-            return first.HasValue ? first.Value : throw Error.NoElements();
-        }
-
-#if !NO_DEEP_CANCELLATION
-
-        private static async Task<TSource> FirstCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
-        {
-            var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
+            async Task<TSource> Core()
+            {
+                var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
 
-            return first.HasValue ? first.Value : throw Error.NoElements();
+                return first.HasValue ? first.Value : throw Error.NoElements();
+            }
         }
 #endif
     }

+ 30 - 32
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/FirstOrDefault.cs

@@ -15,7 +15,14 @@ namespace System.Linq
             if (source == null)
                 throw Error.ArgumentNull(nameof(source));
 
-            return FirstOrDefaultCore(source, cancellationToken);
+            return Core();
+
+            async Task<TSource> Core()
+            {
+                var first = await TryGetFirst(source, cancellationToken).ConfigureAwait(false);
+
+                return first.HasValue ? first.Value : default;
+            }
         }
 
         public static Task<TSource> FirstOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
@@ -25,7 +32,14 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return FirstOrDefaultCore(source, predicate, cancellationToken);
+            return Core();
+
+            async Task<TSource> Core()
+            {
+                var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
+
+                return first.HasValue ? first.Value : default;
+            }
         }
 
         public static Task<TSource> FirstOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
@@ -35,7 +49,14 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return FirstOrDefaultCore(source, predicate, cancellationToken);
+            return Core();
+
+            async Task<TSource> Core()
+            {
+                var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
+
+                return first.HasValue ? first.Value : default;
+            }
         }
 
 #if !NO_DEEP_CANCELLATION
@@ -46,37 +67,14 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return FirstOrDefaultCore(source, predicate, cancellationToken);
-        }
-#endif
-
-        private static async Task<TSource> FirstOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
-        {
-            var first = await TryGetFirst(source, cancellationToken).ConfigureAwait(false);
-
-            return first.HasValue ? first.Value : default;
-        }
-
-        private static async Task<TSource> FirstOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
-        {
-            var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
-
-            return first.HasValue ? first.Value : default;
-        }
+            return Core();
 
-        private static async Task<TSource> FirstOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
-        {
-            var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
-
-            return first.HasValue ? first.Value : default;
-        }
-
-#if !NO_DEEP_CANCELLATION
-        private static async Task<TSource> FirstOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
-        {
-            var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
+            async Task<TSource> Core()
+            {
+                var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
 
-            return first.HasValue ? first.Value : default;
+                return first.HasValue ? first.Value : default;
+            }
         }
 #endif
 

+ 30 - 32
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Last.cs

@@ -15,7 +15,14 @@ namespace System.Linq
             if (source == null)
                 throw Error.ArgumentNull(nameof(source));
 
-            return LastCore(source, cancellationToken);
+            return Core();
+
+            async Task<TSource> Core()
+            {
+                var last = await TryGetLast(source, cancellationToken).ConfigureAwait(false);
+
+                return last.HasValue ? last.Value : throw Error.NoElements();
+            }
         }
 
         public static Task<TSource> LastAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
@@ -25,7 +32,14 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return LastCore(source, predicate, cancellationToken);
+            return Core();
+
+            async Task<TSource> Core()
+            {
+                var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
+
+                return last.HasValue ? last.Value : throw Error.NoElements();
+            }
         }
 
         public static Task<TSource> LastAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
@@ -35,7 +49,14 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return LastCore(source, predicate, cancellationToken);
+            return Core();
+
+            async Task<TSource> Core()
+            {
+                var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
+
+                return last.HasValue ? last.Value : throw Error.NoElements();
+            }
         }
 
 #if !NO_DEEP_CANCELLATION
@@ -46,37 +67,14 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return LastCore(source, predicate, cancellationToken);
-        }
-#endif
-
-        private static async Task<TSource> LastCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
-        {
-            var last = await TryGetLast(source, cancellationToken).ConfigureAwait(false);
-
-            return last.HasValue ? last.Value : throw Error.NoElements();
-        }
-
-        private static async Task<TSource> LastCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
-        {
-            var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
+            return Core();
 
-            return last.HasValue ? last.Value : throw Error.NoElements();
-        }
-
-        private static async Task<TSource> LastCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
-        {
-            var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
-
-            return last.HasValue ? last.Value : throw Error.NoElements();
-        }
-
-#if !NO_DEEP_CANCELLATION
-        private static async Task<TSource> LastCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
-        {
-            var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
+            async Task<TSource> Core()
+            {
+                var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
 
-            return last.HasValue ? last.Value : throw Error.NoElements();
+                return last.HasValue ? last.Value : throw Error.NoElements();
+            }
         }
 #endif
     }

+ 30 - 32
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/LastOrDefault.cs

@@ -15,7 +15,14 @@ namespace System.Linq
             if (source == null)
                 throw Error.ArgumentNull(nameof(source));
 
-            return LastOrDefaultCore(source, cancellationToken);
+            return Core();
+
+            async Task<TSource> Core()
+            {
+                var last = await TryGetLast(source, cancellationToken).ConfigureAwait(false);
+
+                return last.HasValue ? last.Value : default;
+            }
         }
 
         public static Task<TSource> LastOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
@@ -25,7 +32,14 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return LastOrDefaultCore(source, predicate, cancellationToken);
+            return Core();
+
+            async Task<TSource> Core()
+            {
+                var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
+
+                return last.HasValue ? last.Value : default;
+            }
         }
 
         public static Task<TSource> LastOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
@@ -35,7 +49,14 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return LastOrDefaultCore(source, predicate, cancellationToken);
+            return Core();
+
+            async Task<TSource> Core()
+            {
+                var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
+
+                return last.HasValue ? last.Value : default;
+            }
         }
 
 #if !NO_DEEP_CANCELLATION
@@ -46,37 +67,14 @@ namespace System.Linq
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return LastOrDefaultCore(source, predicate, cancellationToken);
-        }
-#endif
-
-        private static async Task<TSource> LastOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
-        {
-            var last = await TryGetLast(source, cancellationToken).ConfigureAwait(false);
-
-            return last.HasValue ? last.Value : default;
-        }
-
-        private static async Task<TSource> LastOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
-        {
-            var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
-
-            return last.HasValue ? last.Value : default;
-        }
+            return Core();
 
-        private static async Task<TSource> LastOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
-        {
-            var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
-
-            return last.HasValue ? last.Value : default;
-        }
-
-#if !NO_DEEP_CANCELLATION
-        private static async Task<TSource> LastOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
-        {
-            var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
+            async Task<TSource> Core()
+            {
+                var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
 
-            return last.HasValue ? last.Value : default;
+                return last.HasValue ? last.Value : default;
+            }
         }
 #endif
 

+ 87 - 89
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/LongCount.cs

@@ -15,145 +15,143 @@ namespace System.Linq
             if (source == null)
                 throw Error.ArgumentNull(nameof(source));
 
-            return LongCountCore(source, cancellationToken);
-        }
+            return Core();
 
-        public static Task<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
-        {
-            if (source == null)
-                throw Error.ArgumentNull(nameof(source));
-            if (predicate == null)
-                throw Error.ArgumentNull(nameof(predicate));
+            async Task<long> Core()
+            {
+                var count = 0L;
 
-            return LongCountCore(source, predicate, cancellationToken);
-        }
+                var e = source.GetAsyncEnumerator(cancellationToken);
 
-        public static Task<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
-        {
-            if (source == null)
-                throw Error.ArgumentNull(nameof(source));
-            if (predicate == null)
-                throw Error.ArgumentNull(nameof(predicate));
+                try
+                {
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
+                    {
+                        checked
+                        {
+                            count++;
+                        }
+                    }
+                }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
 
-            return LongCountCore(source, predicate, cancellationToken);
+                return count;
+            }
         }
 
-#if !NO_DEEP_CANCELLATION
-        public static Task<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
+        public static Task<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
         {
             if (source == null)
                 throw Error.ArgumentNull(nameof(source));
             if (predicate == null)
                 throw Error.ArgumentNull(nameof(predicate));
 
-            return LongCountCore(source, predicate, cancellationToken);
-        }
-#endif
+            return Core();
 
-        private static async Task<long> LongCountCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
-        {
-            var count = 0L;
-
-            var e = source.GetAsyncEnumerator(cancellationToken);
-
-            try
-            {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
-                {
-                    checked
-                    {
-                        count++;
-                    }
-                }
-            }
-            finally
+            async Task<long> Core()
             {
-                await e.DisposeAsync().ConfigureAwait(false);
-            }
-
-            return count;
-        }
-
-        private static async Task<long> LongCountCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
-        {
-            var count = 0L;
+                var count = 0L;
 
-            var e = source.GetAsyncEnumerator(cancellationToken);
+                var e = source.GetAsyncEnumerator(cancellationToken);
 
-            try
-            {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
+                try
                 {
-                    if (predicate(e.Current))
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
                     {
-                        checked
+                        if (predicate(e.Current))
                         {
-                            count++;
+                            checked
+                            {
+                                count++;
+                            }
                         }
                     }
                 }
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
-            }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
 
-            return count;
+                return count;
+            }
         }
 
-        private static async Task<long> LongCountCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
+        public static Task<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
         {
-            var count = 0L;
+            if (source == null)
+                throw Error.ArgumentNull(nameof(source));
+            if (predicate == null)
+                throw Error.ArgumentNull(nameof(predicate));
 
-            var e = source.GetAsyncEnumerator(cancellationToken);
+            return Core();
 
-            try
+            async Task<long> Core()
             {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
+                var count = 0L;
+
+                var e = source.GetAsyncEnumerator(cancellationToken);
+
+                try
                 {
-                    if (await predicate(e.Current).ConfigureAwait(false))
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
                     {
-                        checked
+                        if (await predicate(e.Current).ConfigureAwait(false))
                         {
-                            count++;
+                            checked
+                            {
+                                count++;
+                            }
                         }
                     }
                 }
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
-            }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
 
-            return count;
+                return count;
+            }
         }
 
 #if !NO_DEEP_CANCELLATION
-        private static async Task<long> LongCountCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
+        public static Task<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
         {
-            var count = 0L;
+            if (source == null)
+                throw Error.ArgumentNull(nameof(source));
+            if (predicate == null)
+                throw Error.ArgumentNull(nameof(predicate));
 
-            var e = source.GetAsyncEnumerator(cancellationToken);
+            return Core();
 
-            try
+            async Task<long> Core()
             {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
+                var count = 0L;
+
+                var e = source.GetAsyncEnumerator(cancellationToken);
+
+                try
                 {
-                    if (await predicate(e.Current, cancellationToken).ConfigureAwait(false))
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
                     {
-                        checked
+                        if (await predicate(e.Current, cancellationToken).ConfigureAwait(false))
                         {
-                            count++;
+                            checked
+                            {
+                                count++;
+                            }
                         }
                     }
                 }
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
-            }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
 
-            return count;
+                return count;
+            }
         }
 #endif
     }

+ 110 - 112
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Single.cs

@@ -15,171 +15,169 @@ namespace System.Linq
             if (source == null)
                 throw Error.ArgumentNull(nameof(source));
 
-            return SingleCore(source, cancellationToken);
-        }
-
-        public static Task<TSource> SingleAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
-        {
-            if (source == null)
-                throw Error.ArgumentNull(nameof(source));
-            if (predicate == null)
-                throw Error.ArgumentNull(nameof(predicate));
-
-            return SingleCore(source, predicate, cancellationToken);
-        }
-
-        public static Task<TSource> SingleAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
-        {
-            if (source == null)
-                throw Error.ArgumentNull(nameof(source));
-            if (predicate == null)
-                throw Error.ArgumentNull(nameof(predicate));
-
-            return SingleCore(source, predicate, cancellationToken);
-        }
-
-#if !NO_DEEP_CANCELLATION
-        public static Task<TSource> SingleAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
-        {
-            if (source == null)
-                throw Error.ArgumentNull(nameof(source));
-            if (predicate == null)
-                throw Error.ArgumentNull(nameof(predicate));
+            return Core();
 
-            return SingleCore(source, predicate, cancellationToken);
-        }
-#endif
-
-        private static async Task<TSource> SingleCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
-        {
-            if (source is IList<TSource> list)
+            async Task<TSource> Core()
             {
-                switch (list.Count)
+                if (source is IList<TSource> list)
                 {
-                    case 0: throw Error.NoElements();
-                    case 1: return list[0];
-                }
+                    switch (list.Count)
+                    {
+                        case 0: throw Error.NoElements();
+                        case 1: return list[0];
+                    }
 
-                throw Error.MoreThanOneElement();
-            }
+                    throw Error.MoreThanOneElement();
+                }
 
-            var e = source.GetAsyncEnumerator(cancellationToken);
+                var e = source.GetAsyncEnumerator(cancellationToken);
 
-            try
-            {
-                if (!await e.MoveNextAsync().ConfigureAwait(false))
+                try
                 {
-                    throw Error.NoElements();
-                }
+                    if (!await e.MoveNextAsync().ConfigureAwait(false))
+                    {
+                        throw Error.NoElements();
+                    }
 
-                var result = e.Current;
-                if (await e.MoveNextAsync().ConfigureAwait(false))
+                    var result = e.Current;
+                    if (await e.MoveNextAsync().ConfigureAwait(false))
+                    {
+                        throw Error.MoreThanOneElement();
+                    }
+
+                    return result;
+                }
+                finally
                 {
-                    throw Error.MoreThanOneElement();
+                    await e.DisposeAsync().ConfigureAwait(false);
                 }
-
-                return result;
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
             }
         }
 
-        private static async Task<TSource> SingleCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
+        public static Task<TSource> SingleAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
         {
-            var e = source.GetAsyncEnumerator(cancellationToken);
+            if (source == null)
+                throw Error.ArgumentNull(nameof(source));
+            if (predicate == null)
+                throw Error.ArgumentNull(nameof(predicate));
 
-            try
+            return Core();
+
+            async Task<TSource> Core()
             {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
-                {
-                    var result = e.Current;
+                var e = source.GetAsyncEnumerator(cancellationToken);
 
-                    if (predicate(result))
+                try
+                {
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
                     {
-                        while (await e.MoveNextAsync().ConfigureAwait(false))
+                        var result = e.Current;
+
+                        if (predicate(result))
                         {
-                            if (predicate(e.Current))
+                            while (await e.MoveNextAsync().ConfigureAwait(false))
                             {
-                                throw Error.MoreThanOneElement();
+                                if (predicate(e.Current))
+                                {
+                                    throw Error.MoreThanOneElement();
+                                }
                             }
-                        }
 
-                        return result;
+                            return result;
+                        }
                     }
-                }
 
-                throw Error.NoElements();
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
+                    throw Error.NoElements();
+                }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
             }
         }
 
-        private static async Task<TSource> SingleCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
+        public static Task<TSource> SingleAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
         {
-            var e = source.GetAsyncEnumerator(cancellationToken);
+            if (source == null)
+                throw Error.ArgumentNull(nameof(source));
+            if (predicate == null)
+                throw Error.ArgumentNull(nameof(predicate));
 
-            try
+            return Core();
+
+            async Task<TSource> Core()
             {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
-                {
-                    var result = e.Current;
+                var e = source.GetAsyncEnumerator(cancellationToken);
 
-                    if (await predicate(result).ConfigureAwait(false))
+                try
+                {
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
                     {
-                        while (await e.MoveNextAsync().ConfigureAwait(false))
+                        var result = e.Current;
+
+                        if (await predicate(result).ConfigureAwait(false))
                         {
-                            if (await predicate(e.Current).ConfigureAwait(false))
+                            while (await e.MoveNextAsync().ConfigureAwait(false))
                             {
-                                throw Error.MoreThanOneElement();
+                                if (await predicate(e.Current).ConfigureAwait(false))
+                                {
+                                    throw Error.MoreThanOneElement();
+                                }
                             }
-                        }
 
-                        return result;
+                            return result;
+                        }
                     }
-                }
 
-                throw Error.NoElements();
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
+                    throw Error.NoElements();
+                }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
             }
         }
 
 #if !NO_DEEP_CANCELLATION
-        private static async Task<TSource> SingleCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
+        public static Task<TSource> SingleAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
         {
-            var e = source.GetAsyncEnumerator(cancellationToken);
+            if (source == null)
+                throw Error.ArgumentNull(nameof(source));
+            if (predicate == null)
+                throw Error.ArgumentNull(nameof(predicate));
+
+            return Core();
 
-            try
+            async Task<TSource> Core()
             {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
-                {
-                    var result = e.Current;
+                var e = source.GetAsyncEnumerator(cancellationToken);
 
-                    if (await predicate(result, cancellationToken).ConfigureAwait(false))
+                try
+                {
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
                     {
-                        while (await e.MoveNextAsync().ConfigureAwait(false))
+                        var result = e.Current;
+
+                        if (await predicate(result, cancellationToken).ConfigureAwait(false))
                         {
-                            if (await predicate(e.Current, cancellationToken).ConfigureAwait(false))
+                            while (await e.MoveNextAsync().ConfigureAwait(false))
                             {
-                                throw Error.MoreThanOneElement();
+                                if (await predicate(e.Current, cancellationToken).ConfigureAwait(false))
+                                {
+                                    throw Error.MoreThanOneElement();
+                                }
                             }
-                        }
 
-                        return result;
+                            return result;
+                        }
                     }
-                }
 
-                throw Error.NoElements();
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
+                    throw Error.NoElements();
+                }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
             }
         }
 #endif

+ 110 - 112
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SingleOrDefault.cs

@@ -15,172 +15,170 @@ namespace System.Linq
             if (source == null)
                 throw Error.ArgumentNull(nameof(source));
 
-            return SingleOrDefaultCore(source, cancellationToken);
-        }
-
-        public static Task<TSource> SingleOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
-        {
-            if (source == null)
-                throw Error.ArgumentNull(nameof(source));
-            if (predicate == null)
-                throw Error.ArgumentNull(nameof(predicate));
-
-            return SingleOrDefaultCore(source, predicate, cancellationToken);
-        }
-
-        public static Task<TSource> SingleOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
-        {
-            if (source == null)
-                throw Error.ArgumentNull(nameof(source));
-            if (predicate == null)
-                throw Error.ArgumentNull(nameof(predicate));
-
-            return SingleOrDefaultCore(source, predicate, cancellationToken);
-        }
-
-#if !NO_DEEP_CANCELLATION
-        public static Task<TSource> SingleOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
-        {
-            if (source == null)
-                throw Error.ArgumentNull(nameof(source));
-            if (predicate == null)
-                throw Error.ArgumentNull(nameof(predicate));
-
-            return SingleOrDefaultCore(source, predicate, cancellationToken);
-        }
-#endif
+            return Core();
 
-        private static async Task<TSource> SingleOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
-        {
-            if (source is IList<TSource> list)
+            async Task<TSource> Core()
             {
-                switch (list.Count)
+                if (source is IList<TSource> list)
                 {
-                    case 0: return default;
-                    case 1: return list[0];
-                }
+                    switch (list.Count)
+                    {
+                        case 0: return default;
+                        case 1: return list[0];
+                    }
 
-                throw Error.MoreThanOneElement();
-            }
+                    throw Error.MoreThanOneElement();
+                }
 
-            var e = source.GetAsyncEnumerator(cancellationToken);
+                var e = source.GetAsyncEnumerator(cancellationToken);
 
-            try
-            {
-                if (!await e.MoveNextAsync().ConfigureAwait(false))
+                try
                 {
-                    return default;
-                }
+                    if (!await e.MoveNextAsync().ConfigureAwait(false))
+                    {
+                        return default;
+                    }
 
-                var result = e.Current;
+                    var result = e.Current;
 
-                if (!await e.MoveNextAsync().ConfigureAwait(false))
+                    if (!await e.MoveNextAsync().ConfigureAwait(false))
+                    {
+                        return result;
+                    }
+                }
+                finally
                 {
-                    return result;
+                    await e.DisposeAsync().ConfigureAwait(false);
                 }
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
-            }
 
-            throw Error.MoreThanOneElement();
+                throw Error.MoreThanOneElement();
+            }
         }
 
-        private static async Task<TSource> SingleOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
+        public static Task<TSource> SingleOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
         {
-            var e = source.GetAsyncEnumerator(cancellationToken);
+            if (source == null)
+                throw Error.ArgumentNull(nameof(source));
+            if (predicate == null)
+                throw Error.ArgumentNull(nameof(predicate));
+
+            return Core();
 
-            try
+            async Task<TSource> Core()
             {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
-                {
-                    var result = e.Current;
+                var e = source.GetAsyncEnumerator(cancellationToken);
 
-                    if (predicate(result))
+                try
+                {
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
                     {
-                        while (await e.MoveNextAsync().ConfigureAwait(false))
+                        var result = e.Current;
+
+                        if (predicate(result))
                         {
-                            if (predicate(e.Current))
+                            while (await e.MoveNextAsync().ConfigureAwait(false))
                             {
-                                throw Error.MoreThanOneElement();
+                                if (predicate(e.Current))
+                                {
+                                    throw Error.MoreThanOneElement();
+                                }
                             }
-                        }
 
-                        return result;
+                            return result;
+                        }
                     }
-                }
 
-                return default;
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
+                    return default;
+                }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
             }
         }
 
-        private static async Task<TSource> SingleOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
+        public static Task<TSource> SingleOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
         {
-            var e = source.GetAsyncEnumerator(cancellationToken);
+            if (source == null)
+                throw Error.ArgumentNull(nameof(source));
+            if (predicate == null)
+                throw Error.ArgumentNull(nameof(predicate));
 
-            try
+            return Core();
+
+            async Task<TSource> Core()
             {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
-                {
-                    var result = e.Current;
+                var e = source.GetAsyncEnumerator(cancellationToken);
 
-                    if (await predicate(result).ConfigureAwait(false))
+                try
+                {
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
                     {
-                        while (await e.MoveNextAsync().ConfigureAwait(false))
+                        var result = e.Current;
+
+                        if (await predicate(result).ConfigureAwait(false))
                         {
-                            if (await predicate(e.Current).ConfigureAwait(false))
+                            while (await e.MoveNextAsync().ConfigureAwait(false))
                             {
-                                throw Error.MoreThanOneElement();
+                                if (await predicate(e.Current).ConfigureAwait(false))
+                                {
+                                    throw Error.MoreThanOneElement();
+                                }
                             }
-                        }
 
-                        return result;
+                            return result;
+                        }
                     }
-                }
 
-                return default;
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
+                    return default;
+                }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
             }
         }
 
 #if !NO_DEEP_CANCELLATION
-        private static async Task<TSource> SingleOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
+        public static Task<TSource> SingleOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
         {
-            var e = source.GetAsyncEnumerator(cancellationToken);
+            if (source == null)
+                throw Error.ArgumentNull(nameof(source));
+            if (predicate == null)
+                throw Error.ArgumentNull(nameof(predicate));
+
+            return Core();
 
-            try
+            async Task<TSource> Core()
             {
-                while (await e.MoveNextAsync().ConfigureAwait(false))
-                {
-                    var result = e.Current;
+                var e = source.GetAsyncEnumerator(cancellationToken);
 
-                    if (await predicate(result, cancellationToken).ConfigureAwait(false))
+                try
+                {
+                    while (await e.MoveNextAsync().ConfigureAwait(false))
                     {
-                        while (await e.MoveNextAsync().ConfigureAwait(false))
+                        var result = e.Current;
+
+                        if (await predicate(result, cancellationToken).ConfigureAwait(false))
                         {
-                            if (await predicate(e.Current, cancellationToken).ConfigureAwait(false))
+                            while (await e.MoveNextAsync().ConfigureAwait(false))
                             {
-                                throw Error.MoreThanOneElement();
+                                if (await predicate(e.Current, cancellationToken).ConfigureAwait(false))
+                                {
+                                    throw Error.MoreThanOneElement();
+                                }
                             }
-                        }
 
-                        return result;
+                            return result;
+                        }
                     }
-                }
 
-                return default;
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
+                    return default;
+                }
+                finally
+                {
+                    await e.DisposeAsync().ConfigureAwait(false);
+                }
             }
         }
 #endif

+ 0 - 5
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/ToList.cs

@@ -15,11 +15,6 @@ namespace System.Linq
             if (source == null)
                 throw Error.ArgumentNull(nameof(source));
 
-            return ToListCore(source, cancellationToken);
-        }
-
-        private static Task<List<TSource>> ToListCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
-        {
             if (source is IAsyncIListProvider<TSource> listProvider)
                 return listProvider.ToListAsync(cancellationToken).AsTask();