Skip to content

Commit

Permalink
Merge pull request #904 from dotnet/more_csharp8
Browse files Browse the repository at this point in the history
Use some more C# 8.0 features for async Ix
  • Loading branch information
bartdesmet authored May 24, 2019
2 parents f417a7c + c23993c commit 3529def
Show file tree
Hide file tree
Showing 35 changed files with 911 additions and 947 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this
private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource>(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
{
#if USE_ASYNC_ITERATOR
if (comparer == null)
{
comparer = EqualityComparer<TSource>.Default;
}
comparer ??= EqualityComparer<TSource>.Default;

return AsyncEnumerable.Create(Core);

Expand Down Expand Up @@ -135,10 +132,7 @@ async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
{
#if USE_ASYNC_ITERATOR
if (comparer == null)
{
comparer = EqualityComparer<TKey>.Default;
}
comparer ??= EqualityComparer<TKey>.Default;

return AsyncEnumerable.Create(Core);

Expand Down Expand Up @@ -180,10 +174,7 @@ async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
{
#if USE_ASYNC_ITERATOR
if (comparer == null)
{
comparer = EqualityComparer<TKey>.Default;
}
comparer ??= EqualityComparer<TKey>.Default;

return AsyncEnumerable.Create(Core);

Expand Down Expand Up @@ -226,10 +217,7 @@ async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
{
#if USE_ASYNC_ITERATOR
if (comparer == null)
{
comparer = EqualityComparer<TKey>.Default;
}
comparer ??= EqualityComparer<TKey>.Default;

return AsyncEnumerable.Create(Core);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public static ValueTask<bool> IsEmptyAsync<TSource>(this IAsyncEnumerable<TSourc

return Core(source, cancellationToken);

static async ValueTask<bool> Core(IAsyncEnumerable<TSource> _source, CancellationToken _cancellationToken)
static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
{
await using (var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false))
await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
{
return !await e.MoveNextAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ public static ValueTask<TSource> MaxAsync<TSource>(this IAsyncEnumerable<TSource

return Core(source, comparer, cancellationToken);

static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> _source, IComparer<TSource> _comparer, CancellationToken _cancellationToken)
static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
{
if (_comparer == null)
{
_comparer = Comparer<TSource>.Default;
}
comparer ??= Comparer<TSource>.Default;

await using (var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false))
await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
{
if (!await e.MoveNextAsync())
throw Error.NoElements();
Expand All @@ -35,7 +32,7 @@ static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> _source, ICompare
{
var cur = e.Current;

if (_comparer.Compare(cur, max) > 0)
if (comparer.Compare(cur, max) > 0)
{
max = cur;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,22 @@ public static ValueTask<IList<TSource>> MaxByAsync<TSource, TKey>(this IAsyncEnu

private static ValueTask<IList<TSource>> MaxByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
{
if (comparer == null)
{
comparer = Comparer<TKey>.Default;
}
comparer ??= Comparer<TKey>.Default;

return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue), cancellationToken);
}

private static ValueTask<IList<TSource>> MaxByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
{
if (comparer == null)
{
comparer = Comparer<TKey>.Default;
}
comparer ??= Comparer<TKey>.Default;

return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue), cancellationToken);
}

#if !NO_DEEP_CANCELLATION
private static ValueTask<IList<TSource>> MaxByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
{
if (comparer == null)
{
comparer = Comparer<TKey>.Default;
}
comparer ??= Comparer<TKey>.Default;

return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue), cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ public static ValueTask<TSource> MinAsync<TSource>(this IAsyncEnumerable<TSource

return Core(source, comparer, cancellationToken);

static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> _source, IComparer<TSource> _comparer, CancellationToken _cancellationToken)
static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
{
if (_comparer == null)
{
_comparer = Comparer<TSource>.Default;
}
comparer ??= Comparer<TSource>.Default;

await using (var e = _source.GetConfiguredAsyncEnumerator(_cancellationToken, false))
await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
{
if (!await e.MoveNextAsync())
throw Error.NoElements();
Expand All @@ -35,7 +32,7 @@ static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> _source, ICompare
{
var cur = e.Current;

if (_comparer.Compare(cur, min) < 0)
if (comparer.Compare(cur, min) < 0)
{
min = cur;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,22 @@ public static ValueTask<IList<TSource>> MinByAsync<TSource, TKey>(this IAsyncEnu

private static ValueTask<IList<TSource>> MinByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
{
if (comparer == null)
{
comparer = Comparer<TKey>.Default;
}
comparer ??= Comparer<TKey>.Default;

return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
}

private static ValueTask<IList<TSource>> MinByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
{
if (comparer == null)
{
comparer = Comparer<TKey>.Default;
}
comparer ??= Comparer<TKey>.Default;

return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
}

#if !NO_DEEP_CANCELLATION
private static ValueTask<IList<TSource>> MinByCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
{
if (comparer == null)
{
comparer = Comparer<TKey>.Default;
}
comparer ??= Comparer<TKey>.Default;

return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
}
Expand Down
4 changes: 4 additions & 0 deletions Ix.NET/Source/System.Linq.Async/System.Linq.Async.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@
</None>
</ItemGroup>

<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>

</Project>
Loading

0 comments on commit 3529def

Please sign in to comment.