Skip to content

Commit

Permalink
Updated more Methods to use private static methods instead of closures.
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobSilasBentley committed Apr 29, 2024
1 parent 955a6fa commit 2241256
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 30 deletions.
28 changes: 21 additions & 7 deletions MoreLinq/Cartesian.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ static partial class MoreEnumerable
if (second == null) throw new ArgumentNullException(nameof(second));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));

return _(); IEnumerable<TResult> _()
return _(first, second, resultSelector);

static IEnumerable<TResult> _(IEnumerable<T1> first, IEnumerable<T2> second, Func<T1, T2, TResult> resultSelector)
{
IEnumerable<T2> secondMemo;

Expand Down Expand Up @@ -123,7 +125,9 @@ static partial class MoreEnumerable
if (third == null) throw new ArgumentNullException(nameof(third));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));

return _(); IEnumerable<TResult> _()
return _(first, second, third, resultSelector);

Check warning on line 128 in MoreLinq/Cartesian.g.cs

View check run for this annotation

Codecov / codecov/patch

MoreLinq/Cartesian.g.cs#L128

Added line #L128 was not covered by tests

static IEnumerable<TResult> _(IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, Func<T1, T2, T3, TResult> resultSelector)
{
IEnumerable<T2> secondMemo;
IEnumerable<T3> thirdMemo;
Expand Down Expand Up @@ -185,7 +189,9 @@ static partial class MoreEnumerable
if (fourth == null) throw new ArgumentNullException(nameof(fourth));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));

return _(); IEnumerable<TResult> _()
return _(first, second, third, fourth, resultSelector);

static IEnumerable<TResult> _(IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, IEnumerable<T4> fourth, Func<T1, T2, T3, T4, TResult> resultSelector)
{
IEnumerable<T2> secondMemo;
IEnumerable<T3> thirdMemo;
Expand Down Expand Up @@ -255,7 +261,9 @@ static partial class MoreEnumerable
if (fifth == null) throw new ArgumentNullException(nameof(fifth));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));

return _(); IEnumerable<TResult> _()
return _(first, second, third, fourth, fifth, resultSelector);

Check warning on line 264 in MoreLinq/Cartesian.g.cs

View check run for this annotation

Codecov / codecov/patch

MoreLinq/Cartesian.g.cs#L264

Added line #L264 was not covered by tests

static IEnumerable<TResult> _(IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, IEnumerable<T4> fourth, IEnumerable<T5> fifth, Func<T1, T2, T3, T4, T5, TResult> resultSelector)
{
IEnumerable<T2> secondMemo;
IEnumerable<T3> thirdMemo;
Expand Down Expand Up @@ -333,7 +341,9 @@ static partial class MoreEnumerable
if (sixth == null) throw new ArgumentNullException(nameof(sixth));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));

return _(); IEnumerable<TResult> _()
return _(first, second, third, fourth, fifth, sixth, resultSelector);

Check warning on line 344 in MoreLinq/Cartesian.g.cs

View check run for this annotation

Codecov / codecov/patch

MoreLinq/Cartesian.g.cs#L344

Added line #L344 was not covered by tests

static IEnumerable<TResult> _(IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, IEnumerable<T4> fourth, IEnumerable<T5> fifth, IEnumerable<T6> sixth, Func<T1, T2, T3, T4, T5, T6, TResult> resultSelector)
{
IEnumerable<T2> secondMemo;
IEnumerable<T3> thirdMemo;
Expand Down Expand Up @@ -419,7 +429,9 @@ static partial class MoreEnumerable
if (seventh == null) throw new ArgumentNullException(nameof(seventh));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));

return _(); IEnumerable<TResult> _()
return _(first, second, third, fourth, fifth, sixth, seventh, resultSelector);

Check warning on line 432 in MoreLinq/Cartesian.g.cs

View check run for this annotation

Codecov / codecov/patch

MoreLinq/Cartesian.g.cs#L432

Added line #L432 was not covered by tests

static IEnumerable<TResult> _(IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, IEnumerable<T4> fourth, IEnumerable<T5> fifth, IEnumerable<T6> sixth, IEnumerable<T7> seventh, Func<T1, T2, T3, T4, T5, T6, T7, TResult> resultSelector)
{
IEnumerable<T2> secondMemo;
IEnumerable<T3> thirdMemo;
Expand Down Expand Up @@ -513,7 +525,9 @@ static partial class MoreEnumerable
if (eighth == null) throw new ArgumentNullException(nameof(eighth));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));

return _(); IEnumerable<TResult> _()
return _(first, second, third, fourth, fifth, sixth, seventh, eighth, resultSelector);

Check warning on line 528 in MoreLinq/Cartesian.g.cs

View check run for this annotation

Codecov / codecov/patch

MoreLinq/Cartesian.g.cs#L528

Added line #L528 was not covered by tests

static IEnumerable<TResult> _(IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, IEnumerable<T4> fourth, IEnumerable<T5> fifth, IEnumerable<T6> sixth, IEnumerable<T7> seventh, IEnumerable<T8> eighth, Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult> resultSelector)
{
IEnumerable<T2> secondMemo;
IEnumerable<T3> thirdMemo;
Expand Down
4 changes: 3 additions & 1 deletion MoreLinq/Cartesian.g.tt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ Func<<#= string.Join(", ", from x in o.Arguments select "T" + x.Number) #>, TRes
<# } #>
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));

return _(); IEnumerable<TResult> _()
return _(<#= string.Join(", ", from x in o.Arguments select x.Ordinal) #>, resultSelector);

static IEnumerable<TResult> _(<#= string.Join(", ", from x in o.Arguments select "IEnumerable<T" + x.Number + "> " + x.Ordinal) #>, Func<<#= string.Join(", ", from x in o.Arguments select "T" + x.Number) #>, TResult> resultSelector)
{
<# foreach (var arg in o.Arguments.Skip(1)) { #>
IEnumerable<T<#= arg.Number #>> <#= arg.Ordinal #>Memo;
Expand Down
4 changes: 2 additions & 2 deletions MoreLinq/Experimental/Async/Merge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ partial class ExperimentalEnumerable
if (sources is null) throw new ArgumentNullException(nameof(sources));
if (maxConcurrent <= 0) throw new ArgumentOutOfRangeException(nameof(maxConcurrent));

return Async();
return Async(sources, maxConcurrent);

async IAsyncEnumerable<T> Async([EnumeratorCancellation]CancellationToken cancellationToken = default)
static async IAsyncEnumerable<T> Async(IEnumerable<IAsyncEnumerable<T>> sources, int maxConcurrent, [EnumeratorCancellation]CancellationToken cancellationToken = default)
{
using var thisCancellationTokenSource = new CancellationTokenSource();

Expand Down
13 changes: 11 additions & 2 deletions MoreLinq/Experimental/Await.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,20 @@ select e.Task.IsFaulted

return
AwaitQuery.Create(
options => Impl(options.MaxConcurrency,
options => Impl(source,
evaluator,
resultSelector,
options.MaxConcurrency,
options.Scheduler ?? TaskScheduler.Default,
options.PreserveOrder));

IEnumerable<TResult> Impl(int? maxConcurrency, TaskScheduler scheduler, bool ordered)
static IEnumerable<TResult> Impl(
IEnumerable<T> source,
Func<T, CancellationToken, Task<TTaskResult>> evaluator,
Func<T, Task<TTaskResult>, TResult> resultSelector,
int? maxConcurrency,
TaskScheduler scheduler,
bool ordered)
{
// A separate task will enumerate the source and launch tasks.
// It will post all progress as notices to the collection below.
Expand Down
7 changes: 6 additions & 1 deletion MoreLinq/Experimental/Batch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ static partial class ExperimentalEnumerable
if (bucketProjectionSelector == null) throw new ArgumentNullException(nameof(bucketProjectionSelector));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));

return _(); IEnumerable<TResult> _()
return _(source, size, pool, bucketProjectionSelector, resultSelector);

static IEnumerable<TResult> _(
IEnumerable<TSource> source, int size, ArrayPool<TSource> pool,
Func<ICurrentBuffer<TSource>, IEnumerable<TBucket>> bucketProjectionSelector,
Func<IEnumerable<TBucket>, TResult> resultSelector)
{
using var batch = source.Batch(size, pool);
var bucket = bucketProjectionSelector(batch.CurrentBuffer);
Expand Down
34 changes: 18 additions & 16 deletions MoreLinq/Experimental/Memoize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,53 +96,55 @@ public IEnumerator<T> GetEnumerator()
}
}

return _(); IEnumerator<T> _()
return _(this);

static IEnumerator<T> _(MemoizedEnumerable<T> memoized)
{
var index = 0;

while (true)
{
T current;
lock (this.locker)
lock (memoized.locker)
{
if (this.cache == null) // Cache disposed during iteration?
if (memoized.cache == null) // Cache disposed during iteration?
throw new ObjectDisposedException(nameof(MemoizedEnumerable<T>));

if (index >= this.cache.Count)
if (index >= memoized.cache.Count)
{
if (index == this.errorIndex)
Assume.NotNull(this.error).Throw();
if (index == memoized.errorIndex)
Assume.NotNull(memoized.error).Throw();

if (this.sourceEnumerator == null)
if (memoized.sourceEnumerator == null)
break;

bool moved;
try
{
moved = this.sourceEnumerator.MoveNext();
moved = memoized.sourceEnumerator.MoveNext();
}
catch (Exception ex)
{
this.error = ExceptionDispatchInfo.Capture(ex);
this.errorIndex = index;
this.sourceEnumerator.Dispose();
this.sourceEnumerator = null;
memoized.error = ExceptionDispatchInfo.Capture(ex);
memoized.errorIndex = index;
memoized.sourceEnumerator.Dispose();
memoized.sourceEnumerator = null;
throw;
}

if (moved)
{
this.cache.Add(this.sourceEnumerator.Current);
memoized.cache.Add(memoized.sourceEnumerator.Current);
}
else
{
this.sourceEnumerator.Dispose();
this.sourceEnumerator = null;
memoized.sourceEnumerator.Dispose();
memoized.sourceEnumerator = null;
break;
}
}

current = this.cache[index];
current = memoized.cache[index];
}

yield return current;
Expand Down
4 changes: 3 additions & 1 deletion MoreLinq/Subsets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ public static IEnumerable<IList<T>> Subsets<T>(this IEnumerable<T> sequence, int
// preconditions. This however, needs to be carefully considered - and perhaps
// may change after further thought and review.

return _(); IEnumerable<IList<T>> _()
return _(sequence, subsetSize);

static IEnumerable<IList<T>> _(IEnumerable<T> sequence, int subsetSize)
{
foreach (var subset in Subsets(sequence.ToList(), subsetSize))
yield return subset;
Expand Down

0 comments on commit 2241256

Please sign in to comment.