Skip to content

Commit

Permalink
Rename ops from XTask to XAsync, fix spelling mistakes, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
akarnokd committed Oct 25, 2018
1 parent 81ea4c4 commit 85615e1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 38 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ finally
## End-consumers

- `Consume` - consume the async sequence via a awaitable push interface of `IAsyncConsumer`
- `FirstTask` - get the very first value of the async sequence
- `FirstAsync` - get the very first value of the async sequence
- `ForEach` - invoke callbacks for each item and for the terminal signals
- `LastTask` - get the very last value of the sequence
- `SingleTask` - get the only value of the sequence or signal error
- `LastAsync` - get the very last value of the sequence
- `SingleAsync` - get the only value of the sequence or signal error
- `ToArrayAsync` - get all items as an array
- `ToEnumerable` - convert the `IAsyncEnumerable` into a blocking `IEnumerable`
- `ToListAsync` - get all items in an IList
- `ToObservable` - convert the `IAsyncEnumerable` into an `IObservable`

## Push-pull bridges
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,66 @@

namespace async_enumerable_dotnet_test
{
public class FirstLastSingleTaskTest
public class FirstLastSingleAsyncTest
{
[Fact]
public async void First()
{
Assert.Equal(1, await AsyncEnumerable.Range(1, 5).FirstTask());
Assert.Equal(1, await AsyncEnumerable.Range(1, 5).FirstAsync());
}

[Fact]
public async void First_Empty()
{
await Assert.ThrowsAsync<IndexOutOfRangeException>(() => AsyncEnumerable.Range(1, 0).FirstTask().AsTask());
await Assert.ThrowsAsync<IndexOutOfRangeException>(() => AsyncEnumerable.Range(1, 0).FirstAsync().AsTask());
}

[Fact]
public async void First_Or_Default()
{
Assert.Equal(10, await AsyncEnumerable.Range(1, 0).FirstTask(10));
Assert.Equal(10, await AsyncEnumerable.Range(1, 0).FirstAsync(10));
}

[Fact]
public async void Last()
{
Assert.Equal(5, await AsyncEnumerable.Range(1, 5).LastTask());
Assert.Equal(5, await AsyncEnumerable.Range(1, 5).LastAsync());
}

[Fact]
public async void Last_Empty()
{
await Assert.ThrowsAsync<IndexOutOfRangeException>(() => AsyncEnumerable.Range(1, 0).LastTask().AsTask());
await Assert.ThrowsAsync<IndexOutOfRangeException>(() => AsyncEnumerable.Range(1, 0).LastAsync().AsTask());
}

[Fact]
public async void Last_Or_Default()
{
Assert.Equal(10, await AsyncEnumerable.Range(1, 0).LastTask(10));
Assert.Equal(10, await AsyncEnumerable.Range(1, 0).LastAsync(10));
}

[Fact]
public async void Single()
{
Assert.Equal(0, await AsyncEnumerable.Just(0).SingleTask());
Assert.Equal(0, await AsyncEnumerable.Just(0).SingleAsync());
}

[Fact]
public async void Single_Empty()
{
await Assert.ThrowsAsync<IndexOutOfRangeException>(() => AsyncEnumerable.Empty<int>().SingleTask().AsTask());
await Assert.ThrowsAsync<IndexOutOfRangeException>(() => AsyncEnumerable.Empty<int>().SingleAsync().AsTask());
}

[Fact]
public async void Single_Or_Default()
{
Assert.Equal(10, await AsyncEnumerable.Empty<int>().SingleTask(10));
Assert.Equal(10, await AsyncEnumerable.Empty<int>().SingleAsync(10));
}

[Fact]
public async void Single_Too_Many()
{
await Assert.ThrowsAsync<IndexOutOfRangeException>(() => AsyncEnumerable.Range(1, 5).SingleTask().AsTask());
await Assert.ThrowsAsync<IndexOutOfRangeException>(() => AsyncEnumerable.Range(1, 5).SingleAsync().AsTask());
}
}
}
46 changes: 23 additions & 23 deletions async-enumerable-dotnet/AsyncEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public static IAsyncEnumerable<int> Range(int start, int count)
}

/// <summary>
/// Transforms each souce item into another item via a function.
/// Transforms each source item into another item via a function.
/// </summary>
/// <typeparam name="T">The element type of the source.</typeparam>
/// <typeparam name="R">The result type.</typeparam>
Expand All @@ -233,7 +233,7 @@ public static IAsyncEnumerable<int> Range(int start, int count)
}

/// <summary>
/// Transforms each souce item into another item via an asynchronous function.
/// Transforms each source item into another item via an asynchronous function.
/// </summary>
/// <typeparam name="T">The element type of the source.</typeparam>
/// <typeparam name="R">The result type.</typeparam>
Expand Down Expand Up @@ -333,7 +333,7 @@ public static IAsyncEnumerable<T> Just<T>(T item)
/// Skips the first given number of items of the source async sequence.
/// </summary>
/// <typeparam name="T">The element type.</typeparam>
/// <param name="source">The source asnyc sequence.</param>
/// <param name="source">The source async sequence.</param>
/// <param name="n">The number of items to skip.</param>
/// <returns>The new IAsyncEnumerable instance.</returns>
public static IAsyncEnumerable<T> Skip<T>(this IAsyncEnumerable<T> source, long n)
Expand All @@ -355,7 +355,7 @@ public static IAsyncEnumerable<T> Empty<T>()
/// Returns a shared instance of an async sequence that never produces any items or terminates.
/// </summary>
/// <typeparam name="T">The target element type.</typeparam>
/// <returns>The shared non-signalling IAsyncEnumerable instance.</returns>
/// <returns>The shared non-signaling IAsyncEnumerable instance.</returns>
/// <remarks>
/// Note that the async sequence API doesn't really support a never emitting source because
/// such source never completes its MoveNextAsync and thus DisposeAsync can't be called.
Expand Down Expand Up @@ -387,9 +387,9 @@ public static ValueTask ForEach<T>(this IAsyncEnumerable<T> source, Action<T> on
/// <typeparam name="T">The element type.</typeparam>
/// <param name="source">The source async sequence to get the first item of.</param>
/// <returns>The task that completes with the first item of the sequence or fails.</returns>
public static ValueTask<T> FirstTask<T>(this IAsyncEnumerable<T> source)
public static ValueTask<T> FirstAsync<T>(this IAsyncEnumerable<T> source)
{
return impl.FirstLastSingleTask.First(source, default, false);
return impl.FirstLastSingleAsync.First(source, default, false);
}

/// <summary>
Expand All @@ -400,9 +400,9 @@ public static ValueTask<T> FirstTask<T>(this IAsyncEnumerable<T> source)
/// <param name="source">The source async sequence to get the first item of.</param>
/// <param name="defaultItem">The item to return if the source is empty.</param>
/// <returns>The task that completes with the first/default item of the sequence or fails.</returns>
public static ValueTask<T> FirstTask<T>(this IAsyncEnumerable<T> source, T defaultItem)
public static ValueTask<T> FirstAsync<T>(this IAsyncEnumerable<T> source, T defaultItem)
{
return impl.FirstLastSingleTask.First(source, defaultItem, true);
return impl.FirstLastSingleAsync.First(source, defaultItem, true);
}

/// <summary>
Expand All @@ -412,9 +412,9 @@ public static ValueTask<T> FirstTask<T>(this IAsyncEnumerable<T> source, T defau
/// <typeparam name="T">The element type.</typeparam>
/// <param name="source">The source async sequence to get the last item of.</param>
/// <returns>The task that completes with the last item of the sequence or fails.</returns>
public static ValueTask<T> LastTask<T>(this IAsyncEnumerable<T> source)
public static ValueTask<T> LastAsync<T>(this IAsyncEnumerable<T> source)
{
return impl.FirstLastSingleTask.Last(source, default, false);
return impl.FirstLastSingleAsync.Last(source, default, false);
}

/// <summary>
Expand All @@ -425,9 +425,9 @@ public static ValueTask<T> LastTask<T>(this IAsyncEnumerable<T> source)
/// <param name="source">The source async sequence to get the last item of.</param>
/// <param name="defaultItem">The item to return if the source is empty.</param>
/// <returns>The task that completes with the last/default item of the sequence or fails.</returns>
public static ValueTask<T> LastTask<T>(this IAsyncEnumerable<T> source, T defaultItem)
public static ValueTask<T> LastAsync<T>(this IAsyncEnumerable<T> source, T defaultItem)
{
return impl.FirstLastSingleTask.Last(source, defaultItem, true);
return impl.FirstLastSingleAsync.Last(source, defaultItem, true);
}

/// <summary>
Expand All @@ -437,9 +437,9 @@ public static ValueTask<T> LastTask<T>(this IAsyncEnumerable<T> source, T defaul
/// <typeparam name="T">The element type.</typeparam>
/// <param name="source">The source async sequence to get the only item of.</param>
/// <returns>The task that completes with the only item of the sequence or fails.</returns>
public static ValueTask<T> SingleTask<T>(this IAsyncEnumerable<T> source)
public static ValueTask<T> SingleAsync<T>(this IAsyncEnumerable<T> source)
{
return impl.FirstLastSingleTask.Single(source, default, false);
return impl.FirstLastSingleAsync.Single(source, default, false);
}

/// <summary>
Expand All @@ -450,9 +450,9 @@ public static ValueTask<T> SingleTask<T>(this IAsyncEnumerable<T> source)
/// <param name="source">The source async sequence to get the only item of.</param>
/// <param name="defaultItem">The item to return if the source is empty.</param>
/// <returns>The task that completes with the only/default item of the sequence or fails.</returns>
public static ValueTask<T> SingleTask<T>(this IAsyncEnumerable<T> source, T defaultItem)
public static ValueTask<T> SingleAsync<T>(this IAsyncEnumerable<T> source, T defaultItem)
{
return impl.FirstLastSingleTask.Single(source, defaultItem, true);
return impl.FirstLastSingleAsync.Single(source, defaultItem, true);
}

/// <summary>
Expand Down Expand Up @@ -507,7 +507,7 @@ public static IAsyncEnumerable<T> Reduce<T>(this IAsyncEnumerable<T> source, Fun
/// <typeparam name="T">The element type of the source async sequence.</typeparam>
/// <typeparam name="R">The element type of the inner sequences and the result items.</typeparam>
/// <param name="source">The source async sequence to be mapped.</param>
/// <param name="mapper">The function recieving the source item and should return an inner async sequence to relay elements of.</param>
/// <param name="mapper">The function receiving the source item and should return an inner async sequence to relay elements of.</param>
/// <returns>The new IAsyncEnumerable instance.</returns>
public static IAsyncEnumerable<R> ConcatMap<T, R>(this IAsyncEnumerable<T> source, Func<T, IAsyncEnumerable<R>> mapper)
{
Expand All @@ -520,7 +520,7 @@ public static IAsyncEnumerable<T> Reduce<T>(this IAsyncEnumerable<T> source, Fun
/// <typeparam name="T">The element type of the source async sequence.</typeparam>
/// <typeparam name="R">The element type of the inner sequences and the result items.</typeparam>
/// <param name="source">The source async sequence to be mapped.</param>
/// <param name="mapper">The function recieving the source item and should return an inner enumerable sequence to relay elements of.</param>
/// <param name="mapper">The function receiving the source item and should return an inner enumerable sequence to relay elements of.</param>
/// <returns>The new IAsyncEnumerable instance.</returns>
public static IAsyncEnumerable<R> ConcatMap<T, R>(this IAsyncEnumerable<T> source, Func<T, IEnumerable<R>> mapper)
{
Expand Down Expand Up @@ -658,7 +658,7 @@ public static IAsyncEnumerable<long> Interval(TimeSpan period)
}

/// <summary>
/// Produces an ever increasing number after an itial delay, then periodically.
/// Produces an ever increasing number after an initial delay, then periodically.
/// </summary>
/// <param name="initialDelay">The initial delay before the first item emitted.</param>
/// <param name="period">The initial and in-between time period for when to signal the next value.</param>
Expand Down Expand Up @@ -847,7 +847,7 @@ public static IAsyncEnumerable<T> TakeLast<T>(this IAsyncEnumerable<T> source, i
}

/// <summary>
/// Collect the specified number of source items, non-overlappingly, into Lists and emit those lists.
/// Collect the specified number of source items, in a non-overlapping fashion, into Lists and emit those lists.
/// </summary>
/// <typeparam name="T">The element type.</typeparam>
/// <param name="source">The source async sequence to batch up.</param>
Expand All @@ -859,7 +859,7 @@ public static IAsyncEnumerable<IList<T>> Buffer<T>(this IAsyncEnumerable<T> sour
}

/// <summary>
/// Collect the specified number of source items, non-overlappingly, into custom ICollections generated on demand and emit those lists.
/// Collect the specified number of source items, in a non-overlapping fashion, into custom ICollections generated on demand and emit those lists.
/// </summary>
/// <typeparam name="T">The element type.</typeparam>
/// <typeparam name="C">The custom collection type.</typeparam>
Expand All @@ -873,7 +873,7 @@ public static IAsyncEnumerable<IList<T>> Buffer<T>(this IAsyncEnumerable<T> sour
}

/// <summary>
/// Collect the specified number of source items, possibly overlappingly, into Lists and emit those lists.
/// Collect the specified number of source items, possibly in an overlapping fashion, into Lists and emit those lists.
/// </summary>
/// <typeparam name="T">The element type.</typeparam>
/// <param name="source">The source async sequence to batch up.</param>
Expand All @@ -886,7 +886,7 @@ public static IAsyncEnumerable<IList<T>> Buffer<T>(this IAsyncEnumerable<T> sour
}

/// <summary>
/// Collect the specified number of source items, possibly overlappingly, into custom ICollections and emit those lists.
/// Collect the specified number of source items, possibly in an overlapping fashion, into custom ICollections and emit those lists.
/// </summary>
/// <typeparam name="T">The element type.</typeparam>
/// <typeparam name="C">The custom collection type.</typeparam>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace async_enumerable_dotnet.impl
{
internal static class FirstLastSingleTask
internal static class FirstLastSingleAsync
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static async ValueTask<T> First<T>(IAsyncEnumerable<T> source, T defaultItem, bool hasDefault)
{
var enumerator = source.GetAsyncEnumerator();
Expand All @@ -31,6 +33,7 @@ internal static async ValueTask<T> First<T>(IAsyncEnumerable<T> source, T defaul
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static async ValueTask<T> Last<T>(IAsyncEnumerable<T> source, T defaultItem, bool hasDefault)
{
var hasLast = false;
Expand Down Expand Up @@ -62,6 +65,7 @@ internal static async ValueTask<T> Last<T>(IAsyncEnumerable<T> source, T default
throw new IndexOutOfRangeException("The source async sequence is empty");
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static async ValueTask<T> Single<T>(IAsyncEnumerable<T> source, T defaultItem, bool hasDefault)
{
var enumerator = source.GetAsyncEnumerator();
Expand Down

0 comments on commit 85615e1

Please sign in to comment.