Kaynağa Gözat

Splitting Last and LastOrDefault.

Bart De Smet 8 yıl önce
ebeveyn
işleme
d506415451

+ 0 - 88
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Last.cs

@@ -66,62 +66,6 @@ namespace System.Linq
             return source.Where(predicate).Last(cancellationToken);
         }
 
-        public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source)
-        {
-            if (source == null)
-                throw new ArgumentNullException(nameof(source));
-
-            return LastOrDefault(source, CancellationToken.None);
-        }
-
-        public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
-        {
-            if (source == null)
-                throw new ArgumentNullException(nameof(source));
-
-            return LastOrDefault_(source, cancellationToken);
-        }
-
-        public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
-        {
-            if (source == null)
-                throw new ArgumentNullException(nameof(source));
-            if (predicate == null)
-                throw new ArgumentNullException(nameof(predicate));
-
-            return LastOrDefault(source, predicate, CancellationToken.None);
-        }
-
-        public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
-        {
-            if (source == null)
-                throw new ArgumentNullException(nameof(source));
-            if (predicate == null)
-                throw new ArgumentNullException(nameof(predicate));
-
-            return source.Where(predicate).LastOrDefault(cancellationToken);
-        }
-
-        public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
-        {
-            if (source == null)
-                throw new ArgumentNullException(nameof(source));
-            if (predicate == null)
-                throw new ArgumentNullException(nameof(predicate));
-
-            return LastOrDefault(source, predicate, CancellationToken.None);
-        }
-
-        public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
-        {
-            if (source == null)
-                throw new ArgumentNullException(nameof(source));
-            if (predicate == null)
-                throw new ArgumentNullException(nameof(predicate));
-
-            return source.Where(predicate).LastOrDefault(cancellationToken);
-        }
-
         private static async Task<TSource> Last_<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
         {
             var last = default(TSource);
@@ -156,37 +100,5 @@ namespace System.Linq
 
             return last;
         }
-
-        private static async Task<TSource> LastOrDefault_<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
-        {
-            var last = default(TSource);
-            var hasLast = false;
-
-            if (source is IList<TSource> list)
-            {
-                var count = list.Count;
-                if (count > 0)
-                {
-                    return list[count - 1];
-                }
-            }
-
-            var e = source.GetAsyncEnumerator();
-
-            try
-            {
-                while (await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
-                {
-                    hasLast = true;
-                    last = e.Current;
-                }
-            }
-            finally
-            {
-                await e.DisposeAsync().ConfigureAwait(false);
-            }
-
-            return !hasLast ? default(TSource) : last;
-        }
     }
 }

+ 101 - 0
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/LastOrDefault.cs

@@ -0,0 +1,101 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the Apache 2.0 License.
+// See the LICENSE file in the project root for more information. 
+
+using System.Collections.Generic;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace System.Linq
+{
+    public static partial class AsyncEnumerable
+    {
+        public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source)
+        {
+            if (source == null)
+                throw new ArgumentNullException(nameof(source));
+
+            return LastOrDefault(source, CancellationToken.None);
+        }
+
+        public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
+        {
+            if (source == null)
+                throw new ArgumentNullException(nameof(source));
+
+            return LastOrDefault_(source, cancellationToken);
+        }
+
+        public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
+        {
+            if (source == null)
+                throw new ArgumentNullException(nameof(source));
+            if (predicate == null)
+                throw new ArgumentNullException(nameof(predicate));
+
+            return LastOrDefault(source, predicate, CancellationToken.None);
+        }
+
+        public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
+        {
+            if (source == null)
+                throw new ArgumentNullException(nameof(source));
+            if (predicate == null)
+                throw new ArgumentNullException(nameof(predicate));
+
+            return source.Where(predicate).LastOrDefault(cancellationToken);
+        }
+
+        public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
+        {
+            if (source == null)
+                throw new ArgumentNullException(nameof(source));
+            if (predicate == null)
+                throw new ArgumentNullException(nameof(predicate));
+
+            return LastOrDefault(source, predicate, CancellationToken.None);
+        }
+
+        public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
+        {
+            if (source == null)
+                throw new ArgumentNullException(nameof(source));
+            if (predicate == null)
+                throw new ArgumentNullException(nameof(predicate));
+
+            return source.Where(predicate).LastOrDefault(cancellationToken);
+        }
+
+        private static async Task<TSource> LastOrDefault_<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
+        {
+            var last = default(TSource);
+            var hasLast = false;
+
+            if (source is IList<TSource> list)
+            {
+                var count = list.Count;
+                if (count > 0)
+                {
+                    return list[count - 1];
+                }
+            }
+
+            var e = source.GetAsyncEnumerator();
+
+            try
+            {
+                while (await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
+                {
+                    hasLast = true;
+                    last = e.Current;
+                }
+            }
+            finally
+            {
+                await e.DisposeAsync().ConfigureAwait(false);
+            }
+
+            return !hasLast ? default(TSource) : last;
+        }
+    }
+}