DotNext by .NET Foundation and Contributors

<PackageReference Include="DotNext" Version="4.15.1" />

 Sequence

public static class Sequence
Various methods to work with classes implementing IEnumerable<T> interface.
using DotNext.Buffers; using DotNext.Runtime; using System; using System.Buffers; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; namespace DotNext.Collections.Generic { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public static class Sequence { private sealed class AsyncEnumerable<T> : IAsyncEnumerable<T> { internal sealed class Enumerator : Disposable, IAsyncEnumerator<T>, IAsyncDisposable { private readonly IEnumerator<T> enumerator; private readonly CancellationToken token; public T Current => enumerator.Current; internal Enumerator(IEnumerable<T> enumerable, CancellationToken token) { enumerator = enumerable.GetEnumerator(); this.token = token; } public ValueTask<bool> MoveNextAsync() { if (!token.IsCancellationRequested) return new ValueTask<bool>(enumerator.MoveNext()); return ValueTask.FromCanceled<bool>(token); } protected override void Dispose(bool disposing) { if (disposing) enumerator.Dispose(); base.Dispose(disposing); } public new ValueTask DisposeAsync() { return base.DisposeAsync(); } } private readonly IEnumerable<T> enumerable; internal AsyncEnumerable(IEnumerable<T> enumerable) { this.enumerable = enumerable; } public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken token) { return new Enumerator(enumerable, token); } } [StructLayout(LayoutKind.Auto)] [System.Runtime.CompilerServices.NullableContext(0)] public readonly struct AsyncGenerator<[System.Runtime.CompilerServices.Nullable(2)] T> : IAsyncEnumerable<T> { private sealed class Enumerator : IAsyncEnumerator<T>, IAsyncDisposable { private readonly Func<CancellationToken, ValueTask<Optional<T>>> generator; private readonly CancellationToken token; private Optional<T> current; public T Current { get { if (!(current.TryGet(out T value, out bool isNull) | isNull)) throw new InvalidOperationException(); return value; } } internal Enumerator(Func<CancellationToken, ValueTask<Optional<T>>> generator, CancellationToken token) { this.generator = generator; this.token = token; } [AsyncStateMachine(typeof(AsyncGenerator<>.Enumerator.<MoveNextAsync>d__6))] public ValueTask<bool> MoveNextAsync() { <MoveNextAsync>d__6 stateMachine = default(<MoveNextAsync>d__6); stateMachine.<>t__builder = AsyncValueTaskMethodBuilder<bool>.Create(); stateMachine.<>4__this = this; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } ValueTask IAsyncDisposable.DisposeAsync() { current = default(Optional<T>); return default(ValueTask); } } private readonly Func<CancellationToken, ValueTask<Optional<T>>> generator; internal AsyncGenerator(Func<CancellationToken, ValueTask<Optional<T>>> generator) { this.generator = generator; } [System.Runtime.CompilerServices.NullableContext(1)] public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken token = default(CancellationToken)) { return new Enumerator(generator, token); } } private sealed class AsyncNotNullEnumerable<T> : IAsyncEnumerable<T> where T : class { private sealed class Enumerator : IAsyncEnumerator<T>, IAsyncDisposable { private readonly IAsyncEnumerator<T> enumerator; private T current; public T Current { get { T val = current; if (val == null) throw new InvalidOperationException(); return val; } } internal Enumerator(IAsyncEnumerable<T> enumerable, CancellationToken token) { enumerator = enumerable.GetAsyncEnumerator(token); } [AsyncStateMachine(typeof(AsyncNotNullEnumerable<>.Enumerator.<MoveNextAsync>d__5))] public ValueTask<bool> MoveNextAsync() { <MoveNextAsync>d__5 stateMachine = default(<MoveNextAsync>d__5); stateMachine.<>t__builder = AsyncValueTaskMethodBuilder<bool>.Create(); stateMachine.<>4__this = this; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } public ValueTask DisposeAsync() { current = null; return enumerator.DisposeAsync(); } } private readonly IAsyncEnumerable<T> enumerable; internal AsyncNotNullEnumerable(IAsyncEnumerable<T> enumerable) { this.enumerable = enumerable; } public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken token) { return new Enumerator(enumerable, token); } } [StructLayout(LayoutKind.Auto)] [System.Runtime.CompilerServices.NullableContext(0)] public readonly struct ConsumingEnumerable<[System.Runtime.CompilerServices.Nullable(2)] T> : IEnumerable<T>, IEnumerable { [StructLayout(LayoutKind.Auto)] public struct Enumerator : IEnumerator<T>, IEnumerator, IDisposable { private readonly IProducerConsumerCollection<T> collection; private T current; [System.Runtime.CompilerServices.Nullable(1)] public T Current { [IsReadOnly] [System.Runtime.CompilerServices.NullableContext(1)] get { return current; } } [System.Runtime.CompilerServices.Nullable(2)] object IEnumerator.Current { [IsReadOnly] get { return Current; } } internal Enumerator(IProducerConsumerCollection<T> collection) { this.collection = collection; current = default(T); } public bool MoveNext() { return collection?.TryTake(out current) ?? false; } [IsReadOnly] void IEnumerator.Reset() { throw new NotSupportedException(); } void IDisposable.Dispose() { this = default(Enumerator); } } private readonly IProducerConsumerCollection<T> collection; internal ConsumingEnumerable(IProducerConsumerCollection<T> collection) { this.collection = collection; } public Enumerator GetEnumerator() { return new Enumerator(collection); } IEnumerator<T> IEnumerable<T>.GetEnumerator() { return GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } [DebuggerDisplay("Count = 0")] private sealed class EmptyEnumerator<T> : IEnumerator<T>, IEnumerator, IDisposable, IAsyncEnumerator<T>, IAsyncDisposable, IAsyncEnumerable<T> { internal static readonly EmptyEnumerator<T> Instance = new EmptyEnumerator<T>(); [DebuggerBrowsable(DebuggerBrowsableState.Never)] public T Current { get { throw new InvalidOperationException(); } } [DebuggerBrowsable(DebuggerBrowsableState.Never)] object IEnumerator.Current { get { return Current; } } private EmptyEnumerator() { } bool IEnumerator.MoveNext() { return false; } ValueTask<bool> IAsyncEnumerator<T>.MoveNextAsync() { return new ValueTask<bool>(false); } IAsyncEnumerator<T> IAsyncEnumerable<T>.GetAsyncEnumerator(CancellationToken cancellationToken) { return this; } void IEnumerator.Reset() { } void IDisposable.Dispose() { } ValueTask IAsyncDisposable.DisposeAsync() { return ValueTask.CompletedTask; } } [StructLayout(LayoutKind.Auto)] [System.Runtime.CompilerServices.NullableContext(0)] public readonly struct Generator<[System.Runtime.CompilerServices.Nullable(2)] T> { [StructLayout(LayoutKind.Auto)] [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public struct Enumerator { private readonly Func<Optional<T>> generator; private Optional<T> current; public T Current { [IsReadOnly] get { if (!(current.TryGet(out T value, out bool isNull) | isNull)) throw new InvalidOperationException(); return value; } } internal Enumerator(Func<Optional<T>> generator) { this.generator = generator; current = Optional<T>.None; } public bool MoveNext() { return !((generator == null) ? default(Optional<T>) : (current = generator())).IsUndefined; } } private readonly Func<Optional<T>> generator; internal Generator(Func<Optional<T>> generator) { this.generator = generator; } public Enumerator GetEnumerator() { return new Enumerator(generator); } } [StructLayout(LayoutKind.Auto)] [System.Runtime.CompilerServices.NullableContext(0)] public struct LimitedEnumerator<[System.Runtime.CompilerServices.Nullable(2)] T> : IEnumerator<T>, IEnumerator, IDisposable { private readonly IEnumerator<T> enumerator; private readonly bool disposeEnumerator; private int count; [System.Runtime.CompilerServices.Nullable(1)] public T Current { [IsReadOnly] [System.Runtime.CompilerServices.NullableContext(1)] get { return enumerator.Current; } } [System.Runtime.CompilerServices.Nullable(2)] object IEnumerator.Current { [IsReadOnly] get { return Current; } } internal LimitedEnumerator(IEnumerator<T> enumerator, int limit, bool leaveOpen) { this.enumerator = enumerator; disposeEnumerator = !leaveOpen; count = limit; } public bool MoveNext() { if (count-- > 0) return enumerator.MoveNext(); return false; } [IsReadOnly] public void Reset() { enumerator?.Reset(); } public void Dispose() { if (disposeEnumerator) enumerator?.Dispose(); this = default(LimitedEnumerator<T>); } } private sealed class NotNullEnumerable<T> : IEnumerable<T>, IEnumerable where T : class { private sealed class Enumerator : Disposable, IEnumerator<T>, IEnumerator, IDisposable { private readonly IEnumerator<T> enumerator; private T current; public T Current { get { T val = current; if (val == null) throw new InvalidOperationException(); return val; } } object IEnumerator.Current { get { return Current; } } internal Enumerator(IEnumerable<T> enumerable) { enumerator = enumerable.GetEnumerator(); } public bool MoveNext() { while (enumerator.MoveNext()) { T val = enumerator.Current; if (val != null) { current = val; return true; } } return false; } public void Reset() { enumerator.Reset(); } protected override void Dispose(bool disposing) { if (disposing) { current = null; enumerator.Dispose(); } base.Dispose(disposing); } } private readonly IEnumerable<T> enumerable; internal NotNullEnumerable(IEnumerable<T> enumerable) { this.enumerable = enumerable; } public IEnumerator<T> GetEnumerator() { return new Enumerator(enumerable); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } [AsyncStateMachine(typeof(<ForEachAsync>d__0<>))] public static ValueTask ForEachAsync<[System.Runtime.CompilerServices.Nullable(2)] T>(this IAsyncEnumerable<T> collection, Action<T> action, CancellationToken token = default(CancellationToken)) { <ForEachAsync>d__0<T> stateMachine = default(<ForEachAsync>d__0<T>); stateMachine.<>t__builder = AsyncValueTaskMethodBuilder.Create(); stateMachine.collection = collection; stateMachine.action = action; stateMachine.token = token; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [AsyncStateMachine(typeof(<ForEachAsync>d__1<>))] public static ValueTask ForEachAsync<[System.Runtime.CompilerServices.Nullable(2)] T>(this IAsyncEnumerable<T> collection, Func<T, CancellationToken, ValueTask> action, CancellationToken token = default(CancellationToken)) { <ForEachAsync>d__1<T> stateMachine = default(<ForEachAsync>d__1<T>); stateMachine.<>t__builder = AsyncValueTaskMethodBuilder.Create(); stateMachine.collection = collection; stateMachine.action = action; stateMachine.token = token; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [System.Runtime.CompilerServices.NullableContext(0)] [AsyncStateMachine(typeof(<FirstOrNullAsync>d__2<>))] public static ValueTask<T?> FirstOrNullAsync<T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] this IAsyncEnumerable<T> seq, CancellationToken token = default(CancellationToken)) where T : struct { <FirstOrNullAsync>d__2<T> stateMachine = default(<FirstOrNullAsync>d__2<T>); stateMachine.<>t__builder = AsyncValueTaskMethodBuilder<T?>.Create(); stateMachine.seq = seq; stateMachine.token = token; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [System.Runtime.CompilerServices.NullableContext(0)] [AsyncStateMachine(typeof(<LastOrNullAsync>d__3<>))] public static ValueTask<T?> LastOrNullAsync<T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] this IAsyncEnumerable<T> seq, CancellationToken token = default(CancellationToken)) where T : struct { <LastOrNullAsync>d__3<T> stateMachine = default(<LastOrNullAsync>d__3<T>); stateMachine.<>t__builder = AsyncValueTaskMethodBuilder<T?>.Create(); stateMachine.seq = seq; stateMachine.token = token; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [Obsolete("Use FirstOrNoneAsync() extension method instead")] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 0, 1 })] public static ValueTask<Optional<T>> FirstOrEmptyAsync<[System.Runtime.CompilerServices.Nullable(2)] T>(this IAsyncEnumerable<T> seq, CancellationToken token = default(CancellationToken)) { return seq.FirstOrNoneAsync(token); } [AsyncStateMachine(typeof(<FirstOrNoneAsync>d__5<>))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 0, 1 })] public static ValueTask<Optional<T>> FirstOrNoneAsync<[System.Runtime.CompilerServices.Nullable(2)] T>(this IAsyncEnumerable<T> seq, CancellationToken token = default(CancellationToken)) { <FirstOrNoneAsync>d__5<T> stateMachine = default(<FirstOrNoneAsync>d__5<T>); stateMachine.<>t__builder = AsyncValueTaskMethodBuilder<Optional<T>>.Create(); stateMachine.seq = seq; stateMachine.token = token; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [AsyncStateMachine(typeof(<LastOrNoneAsync>d__6<>))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 0, 1 })] public static ValueTask<Optional<T>> LastOrNoneAsync<[System.Runtime.CompilerServices.Nullable(2)] T>(this IAsyncEnumerable<T> seq, CancellationToken token = default(CancellationToken)) { <LastOrNoneAsync>d__6<T> stateMachine = default(<LastOrNoneAsync>d__6<T>); stateMachine.<>t__builder = AsyncValueTaskMethodBuilder<Optional<T>>.Create(); stateMachine.seq = seq; stateMachine.token = token; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [Obsolete("Use FirstOrNoneAsync() extension method instead")] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 0, 1 })] public static ValueTask<Optional<T>> FirstOrEmptyAsync<[System.Runtime.CompilerServices.Nullable(2)] T>(this IAsyncEnumerable<T> seq, Predicate<T> filter, CancellationToken token = default(CancellationToken)) { return seq.FirstOrNoneAsync(filter, default(CancellationToken)); } [AsyncStateMachine(typeof(<FirstOrNoneAsync>d__8<>))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 0, 1 })] public static ValueTask<Optional<T>> FirstOrNoneAsync<[System.Runtime.CompilerServices.Nullable(2)] T>(this IAsyncEnumerable<T> seq, Predicate<T> filter, CancellationToken token = default(CancellationToken)) { <FirstOrNoneAsync>d__8<T> stateMachine = default(<FirstOrNoneAsync>d__8<T>); stateMachine.<>t__builder = AsyncValueTaskMethodBuilder<Optional<T>>.Create(); stateMachine.seq = seq; stateMachine.filter = filter; stateMachine.token = token; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [System.Runtime.CompilerServices.NullableContext(0)] [AsyncStateMachine(typeof(<SkipAsync>d__9<>))] public static ValueTask<bool> SkipAsync<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(1)] this IAsyncEnumerator<T> enumerator, int count) { <SkipAsync>d__9<T> stateMachine = default(<SkipAsync>d__9<T>); stateMachine.<>t__builder = AsyncValueTaskMethodBuilder<bool>.Create(); stateMachine.enumerator = enumerator; stateMachine.count = count; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [AsyncStateMachine(typeof(<ElementAtAsync>d__10<>))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 0, 1 })] public static ValueTask<Optional<T>> ElementAtAsync<[System.Runtime.CompilerServices.Nullable(2)] T>(this IAsyncEnumerable<T> collection, int index, CancellationToken token = default(CancellationToken)) { <ElementAtAsync>d__10<T> stateMachine = default(<ElementAtAsync>d__10<T>); stateMachine.<>t__builder = AsyncValueTaskMethodBuilder<Optional<T>>.Create(); stateMachine.collection = collection; stateMachine.index = index; stateMachine.token = token; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } public static IAsyncEnumerable<T> SkipNulls<T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] this IAsyncEnumerable<T> collection) where T : class { return new AsyncNotNullEnumerable<T>(collection); } [AsyncStateMachine(typeof(<ToArrayAsync>d__12<>))] public static Task<T[]> ToArrayAsync<[System.Runtime.CompilerServices.Nullable(2)] T>(this IAsyncEnumerable<T> collection, int initialCapacity = 10, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] MemoryAllocator<T> allocator = null, CancellationToken token = default(CancellationToken)) { <ToArrayAsync>d__12<T> stateMachine = default(<ToArrayAsync>d__12<T>); stateMachine.<>t__builder = AsyncTaskMethodBuilder<T[]>.Create(); stateMachine.collection = collection; stateMachine.initialCapacity = initialCapacity; stateMachine.allocator = allocator; stateMachine.token = token; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } public static IAsyncEnumerable<T> ToAsyncEnumerable<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerable<T> enumerable) { if (enumerable == null) throw new ArgumentNullException("enumerable"); return new AsyncEnumerable<T>(enumerable); } public static IAsyncEnumerator<T> GetAsyncEnumerator<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerable<T> enumerable, CancellationToken token = default(CancellationToken)) { if (enumerable == null) throw new ArgumentNullException("enumerable"); return new AsyncEnumerable<T>.Enumerator(enumerable, token); } public static IAsyncEnumerable<T> GetEmptyAsyncEnumerable<[System.Runtime.CompilerServices.Nullable(2)] T>() { return EmptyEnumerator<T>.Instance; } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static AsyncGenerator<T> ToAsyncEnumerable<T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 0, 1 })] this Func<CancellationToken, ValueTask<Optional<T>>> generator) { if (generator == null) throw new ArgumentNullException("generator"); return new AsyncGenerator<T>(generator); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static MemoryOwner<T> Copy<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerable<T> enumerable, int sizeHint = 0, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] MemoryAllocator<T> allocator = null) { ArgumentNullException.ThrowIfNull((object)enumerable, "enumerable"); if (sizeHint < 0) throw new ArgumentOutOfRangeException("sizeHint"); List<T> list = enumerable as List<T>; if (list != null) return CollectionsMarshal.AsSpan(list).Copy(allocator); T[] array = enumerable as T[]; if (array != null) return Span.Copy(array, allocator); string text = enumerable as string; if (text != null) return Intrinsics.ReinterpretCast<MemoryOwner<char>, MemoryOwner<T>>(text.AsSpan().Copy(Unsafe.As<MemoryAllocator<char>>(allocator))); if (!(enumerable is ArraySegment<T>)) { ICollection<T> collection = enumerable as ICollection<T>; if (collection != null) return (collection.Count == 0) ? default(MemoryOwner<T>) : ((allocator == null) ? <Copy>g__CopyCollection|20_0(collection) : <Copy>g__CopySlow|20_1(collection, collection.Count, allocator)); IReadOnlyCollection<T> readOnlyCollection = enumerable as IReadOnlyCollection<T>; if (readOnlyCollection != null) return (readOnlyCollection.Count == 0) ? default(MemoryOwner<T>) : <Copy>g__CopySlow|20_1(enumerable, readOnlyCollection.Count, allocator); return <Copy>g__CopySlow|20_1(enumerable, <Copy>g__GetSize|20_2(enumerable, sizeHint), allocator); } ArraySegment<T> segment = (ArraySegment<T>)enumerable; return segment.AsSpan().Copy(allocator); } [AsyncStateMachine(typeof(<CopyAsync>d__21<>))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1 })] public static Task<MemoryOwner<T>> CopyAsync<[System.Runtime.CompilerServices.Nullable(2)] T>(this IAsyncEnumerable<T> enumerable, int sizeHint = 0, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] MemoryAllocator<T> allocator = null, CancellationToken token = default(CancellationToken)) { <CopyAsync>d__21<T> stateMachine = default(<CopyAsync>d__21<T>); stateMachine.<>t__builder = AsyncTaskMethodBuilder<MemoryOwner<T>>.Create(); stateMachine.enumerable = enumerable; stateMachine.sizeHint = sizeHint; stateMachine.allocator = allocator; stateMachine.token = token; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static ConsumingEnumerable<T> GetConsumer<[System.Runtime.CompilerServices.Nullable(2)] T>(this IProducerConsumerCollection<T> collection) { if (collection == null) throw new ArgumentNullException("collection"); return new ConsumingEnumerable<T>(collection); } public static int SequenceHashCode<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerable<T> sequence, bool salted = true) { int num = sequence.Aggregate(-910176598, (int hash, T obj) => hash * -1521134295 + ((obj != null) ? EqualityComparer<T>.Default.GetHashCode(obj) : 0)); if (!salted) return num; return num * -1521134295 + RandomExtensions.BitwiseHashSalt; } internal static bool SequenceEqual<T>(IEnumerable<T> first, IEnumerable<T> second) { if (first != null) { if (second != null) return first.SequenceEqual(second); return false; } return second == null; } public static void ForEach<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerable<T> collection, Action<T> action) { List<T> list = collection as List<T>; if (list == null) { T[] array = collection as T[]; if (array == null) { if (collection is ArraySegment<T>) { ArraySegment<T> segment = (ArraySegment<T>)collection; Span.ForEach(segment.AsSpan(), action); } else { LinkedList<T> linkedList = collection as LinkedList<T>; if (linkedList == null) { string text = collection as string; if (text != null) Span.ForEach(text.AsSpan(), Unsafe.As<Action<char>>(action)); else <ForEach>g__ForEachSlow|26_0(collection, action); } else <ForEach>g__ForEachNode|26_1(linkedList, action); } } else Array.ForEach(array, action); } else Span.ForEach(CollectionsMarshal.AsSpan(list), action); } [AsyncStateMachine(typeof(<ForEachAsync>d__27<>))] public static ValueTask ForEachAsync<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerable<T> collection, Func<T, CancellationToken, ValueTask> action, CancellationToken token = default(CancellationToken)) { <ForEachAsync>d__27<T> stateMachine = default(<ForEachAsync>d__27<T>); stateMachine.<>t__builder = AsyncValueTaskMethodBuilder.Create(); stateMachine.collection = collection; stateMachine.action = action; stateMachine.token = token; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [System.Runtime.CompilerServices.NullableContext(0)] public static T? FirstOrNull<T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] this IEnumerable<T> seq) where T : struct { Optional<T> value = seq.FirstOrNone(); return ref value.OrNull(); } [System.Runtime.CompilerServices.NullableContext(0)] public static T? LastOrNull<T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] this IEnumerable<T> seq) where T : struct { Optional<T> value = seq.LastOrNone(); return ref value.OrNull(); } [Obsolete("Use FirstOrNone() extension method instead")] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Optional<T> FirstOrEmpty<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerable<T> seq) { return seq.FirstOrNone(); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Optional<T> FirstOrNone<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerable<T> seq) { List<T> list = seq as List<T>; if (list != null) return Span.FirstOrNone<T>(CollectionsMarshal.AsSpan(list)); T[] array = seq as T[]; if (array != null) return Span.FirstOrNone<T>(array); string text = seq as string; if (text != null) return Intrinsics.ReinterpretCast<Optional<char>, Optional<T>>(text.AsSpan().FirstOrNone()); IList<T> list2 = seq as IList<T>; if (list2 != null) return (((ICollection<T>)list2).Count > 0) ? ((Optional<T>)list2[0]) : Optional<T>.None; IReadOnlyList<T> readOnlyList = seq as IReadOnlyList<T>; if (readOnlyList != null) return (((IReadOnlyCollection<T>)readOnlyList).Count > 0) ? ((Optional<T>)readOnlyList[0]) : Optional<T>.None; return <FirstOrNone>g__FirstOrNoneSlow|31_0(seq); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Optional<T> LastOrNone<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerable<T> seq) { List<T> list = seq as List<T>; if (list != null) return Span.LastOrNone<T>(CollectionsMarshal.AsSpan(list)); T[] array = seq as T[]; if (array != null) return Span.LastOrNone<T>(array); string text = seq as string; if (text != null) return Intrinsics.ReinterpretCast<Optional<char>, Optional<T>>(text.AsSpan().LastOrNone()); IList<T> list2 = seq as IList<T>; if (list2 != null) return (((ICollection<T>)list2).Count > 0) ? ((Optional<T>)list2[((ICollection<T>)list2).Count - 1]) : Optional<T>.None; IReadOnlyList<T> readOnlyList = seq as IReadOnlyList<T>; if (readOnlyList != null) return (((IReadOnlyCollection<T>)readOnlyList).Count > 0) ? ((Optional<T>)readOnlyList[((IReadOnlyCollection<T>)readOnlyList).Count - 1]) : Optional<T>.None; return <LastOrNone>g__LastOrNoneSlow|32_0(seq); } [Obsolete("Use FirstOrNone() extension method instead")] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Optional<T> FirstOrEmpty<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerable<T> seq, Predicate<T> filter) { return seq.FirstOrNone(filter); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Optional<T> FirstOrNone<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerable<T> seq, Predicate<T> filter) { List<T> list = seq as List<T>; if (list != null) return Span.FirstOrNone(CollectionsMarshal.AsSpan(list), filter); T[] array = seq as T[]; if (array != null) return Span.FirstOrNone(array, filter); string text = seq as string; if (text != null) return Intrinsics.ReinterpretCast<Optional<char>, Optional<T>>(text.AsSpan().FirstOrNone(Unsafe.As<Predicate<char>>(filter))); LinkedList<T> linkedList = seq as LinkedList<T>; if (linkedList != null) return <FirstOrNone>g__FindInLinkedList|34_0(linkedList, filter); return <FirstOrNone>g__FirstOrNoneSlow|34_1(seq, filter); } public static bool Skip<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerator<T> enumerator, int count) { while (count > 0) { if (!enumerator.MoveNext()) return false; count--; } return true; } [System.Runtime.CompilerServices.NullableContext(0)] public static bool Skip<TEnumerator, [System.Runtime.CompilerServices.Nullable(2)] T>(ref TEnumerator enumerator, int count) where TEnumerator : struct, IEnumerator<T> { while (count > 0) { if (!enumerator.MoveNext()) return false; count--; } return true; } public static bool ElementAt<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerable<T> collection, int index, [MaybeNullWhen(false)] out T element) { List<T> list = collection as List<T>; if (list != null) return Span.ElementAt(CollectionsMarshal.AsSpan(list), index, out element); T[] array = collection as T[]; if (array != null) return Span.ElementAt(array, index, out element); LinkedList<T> linkedList = collection as LinkedList<T>; if (linkedList != null) return <ElementAt>g__NodeValueAt|37_0(linkedList, index, out element); IList<T> list2 = collection as IList<T>; if (list2 != null) return <ElementAt>g__ListElementAt|37_2(list2, index, out element); IReadOnlyList<T> readOnlyList = collection as IReadOnlyList<T>; if (readOnlyList != null) return <ElementAt>g__ReadOnlyListElementAt|37_3(readOnlyList, index, out element); return <ElementAt>g__ElementAtSlow|37_1(collection, index, out element); } public static IEnumerable<T> SkipNulls<T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] this IEnumerable<T> collection) where T : class { return new NotNullEnumerable<T>(collection); } public static string ToString<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerable<T> collection, string delimiter, string ifEmpty = "") { string text = string.Join(delimiter, collection); if (text == null || text.Length <= 0) return ifEmpty; return text; } public static IEnumerable<T> Singleton<[System.Runtime.CompilerServices.Nullable(2)] T>(T item) { return List.Singleton(item); } public static IEnumerable<T> Prepend<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerable<T> collection, params T[] items) { return items.Concat(collection); } public static IEnumerable<T> Append<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerable<T> collection, params T[] items) { return collection.Concat(items); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static LimitedEnumerator<T> Limit<[System.Runtime.CompilerServices.Nullable(2)] T>(this IEnumerator<T> enumerator, int count, bool leaveOpen = false) { return new LimitedEnumerator<T>(enumerator, count, leaveOpen); } public static IEnumerator<T> ToEnumerator<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ReadOnlyMemory<T> memory) { if (!memory.IsEmpty) { if (!MemoryMarshal.TryGetArray(memory, out ArraySegment<T> segment)) return <ToEnumerator>g__ToEnumeratorSlow|44_0(memory); return segment.GetEnumerator(); } return GetEmptyEnumerator<T>(); } public static IEnumerator<T> ToEnumerator<[System.Runtime.CompilerServices.Nullable(2)] T>([In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ref ReadOnlySequence<T> sequence) { if (!sequence.IsEmpty) { if (!sequence.IsSingleSegment) return <ToEnumerator>g__ToEnumeratorSlow|45_0(sequence.GetEnumerator()); return ToEnumerator(sequence.First); } return GetEmptyEnumerator<T>(); } public static IEnumerator<T> GetEmptyEnumerator<[System.Runtime.CompilerServices.Nullable(2)] T>() { return EmptyEnumerator<T>.Instance; } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Generator<T> ToEnumerable<T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1 })] this Func<Optional<T>> generator) { if (generator == null) throw new ArgumentNullException("generator"); return new Generator<T>(generator); } } }