Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using static Funcky.Discard;

namespace Funcky.Async.Test.Extensions.AsyncEnumerableExtensions;

public sealed class WhereSelectTest
Expand All @@ -10,6 +12,16 @@ public async Task WhereSelectRetainsOnlySomeValues()
Assert.Equal(expectedSquares, squares);
}

[Fact]
public async Task WhereSelectReceivesTheSourceElementsIndex()
{
const int count = 6;
var expectedSequence = Enumerable.Range(0, count: count);
var units = AsyncSequence.Cycle(__).Take(count);
var indexes = units.WhereSelect((_, index) => Option.Some(index));
Assert.Equal(expectedSequence, await indexes.ToListAsync());
}

private static Option<int> SquareEvenNumbers(int n)
=> n % 2 == 0
? n * n
Expand Down
32 changes: 26 additions & 6 deletions Funcky.Async/Extensions/AsyncEnumerableExtensions/WhereSelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Funcky.Extensions;
public static partial class AsyncEnumerableExtensions
{
/// <summary>
/// Filters out all the empty values from an <c>IEnumerable&lt;Option&lt;T&gt;&gt;</c> and therefore returns an <see cref="IEnumerable{T}"/>.
/// Filters out all the empty values from an <see cref="IAsyncEnumerable{T}"><![CDATA[IAsyncEnumerable<Option<T>>]]></see> and therefore returns an <see cref="IAsyncEnumerable{T}"/>.
/// </summary>
[Pure]
public static IAsyncEnumerable<TSource> WhereSelect<TSource>(this IAsyncEnumerable<Option<TSource>> source)
Expand All @@ -19,21 +19,41 @@ public static IAsyncEnumerable<TResult> WhereSelect<TSource, TResult>(this IAsyn
where TResult : notnull
=> source.Select(selector).SelectMany(ToAsyncEnumerable);

/// <inheritdoc cref="WhereSelect{TSource,TOutput}"/>
/// <summary>
/// Projects and filters an <see cref="IAsyncEnumerable{T}"/> at the same time.
/// This is done by filtering out any empty <see cref="Option{T}"/> values returned by the <paramref name="selector"/>.
/// The index of each source element is used in the projected form of that element.
/// </summary>
[Pure]
public static IAsyncEnumerable<TResult> WhereSelect<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, int, Option<TResult>> selector)
where TResult : notnull
=> source.Select(selector).SelectMany(ToAsyncEnumerable);

/// <inheritdoc cref="WhereSelect{TSource,TResult}(IAsyncEnumerable{TSource},Func{TSource,Option{TResult}})"/>
[Pure]
public static IAsyncEnumerable<TResult> WhereSelectAwait<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<Option<TResult>>> selector)
where TResult : notnull
=> source.SelectAwait(selector).SelectMany(ToAsyncEnumerable);

/// <inheritdoc cref="WhereSelect{TSource,TOutput}"/>
/// <inheritdoc cref="WhereSelect{TSource,TResult}(IAsyncEnumerable{TSource},Func{TSource,int,Option{TResult}})"/>
[Pure]
public static IAsyncEnumerable<TResult> WhereSelectAwait<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, int, ValueTask<Option<TResult>>> selector)
where TResult : notnull
=> source.SelectAwait(selector).SelectMany(ToAsyncEnumerable);

/// <inheritdoc cref="WhereSelect{TSource,TResult}(IAsyncEnumerable{TSource},Func{TSource,Option{TResult}})"/>
[Pure]
public static IAsyncEnumerable<TResult> WhereSelectAwaitWithCancellation<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<Option<TResult>>> selector)
where TResult : notnull
=> source.SelectAwaitWithCancellation(selector).SelectMany(ToAsyncEnumerable);

/// <inheritdoc cref="WhereSelect{TSource,TResult}(IAsyncEnumerable{TSource},Func{TSource,int,Option{TResult}})"/>
[Pure]
public static IAsyncEnumerable<TResult> WhereSelectAwaitWithCancellation<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, ValueTask<Option<TResult>>> selector)
where TResult : notnull
=> source.SelectAwaitWithCancellation(selector).SelectMany(ToAsyncEnumerable);

private static IAsyncEnumerable<TItem> ToAsyncEnumerable<TItem>(Option<TItem> option)
where TItem : notnull
=> option.Match(
none: AsyncEnumerable.Empty<TItem>,
some: AsyncSequence.Return);
=> option.ToAsyncEnumerable();
}
3 changes: 3 additions & 0 deletions Funcky.Async/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
#nullable enable
static Funcky.Extensions.AsyncEnumerableExtensions.Traverse<TSource, T>(this System.Collections.Generic.IAsyncEnumerable<TSource>! source, System.Func<TSource, System.Lazy<T>!>! selector) -> System.Lazy<System.Collections.Generic.IAsyncEnumerable<T>!>!
static Funcky.Extensions.AsyncEnumerableExtensions.WhereSelect<TSource, TResult>(this System.Collections.Generic.IAsyncEnumerable<TSource>! source, System.Func<TSource, int, Funcky.Monads.Option<TResult>>! selector) -> System.Collections.Generic.IAsyncEnumerable<TResult>!
static Funcky.Extensions.AsyncEnumerableExtensions.WhereSelectAwait<TSource, TResult>(this System.Collections.Generic.IAsyncEnumerable<TSource>! source, System.Func<TSource, int, System.Threading.Tasks.ValueTask<Funcky.Monads.Option<TResult>>>! selector) -> System.Collections.Generic.IAsyncEnumerable<TResult>!
static Funcky.Extensions.AsyncEnumerableExtensions.WhereSelectAwaitWithCancellation<TSource, TResult>(this System.Collections.Generic.IAsyncEnumerable<TSource>! source, System.Func<TSource, int, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask<Funcky.Monads.Option<TResult>>>! selector) -> System.Collections.Generic.IAsyncEnumerable<TResult>!
11 changes: 11 additions & 0 deletions Funcky.Test/Extensions/EnumerableExtensions/WhereSelectTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Funcky.Test.TestUtils;
using static Funcky.Discard;

namespace Funcky.Test.Extensions.EnumerableExtensions;

Expand All @@ -21,6 +22,16 @@ public void WhereSelectFiltersEmptySelectorValues()
Assert.Equal(expectedResult, result);
}

[Fact]
public void WhereSelectReceivesTheSourceElementsIndex()
{
const int count = 6;
var expectedSequence = Enumerable.Range(0, count: count);
var units = Sequence.Cycle(__).Take(count);
var indexes = units.WhereSelect((_, index) => Option.Some(index));
Assert.Equal(expectedSequence, indexes);
}

[Fact]
public void WhereSelectFiltersEmptyFromSequence()
{
Expand Down
8 changes: 7 additions & 1 deletion Funcky/Extensions/EnumerableExtensions/WhereSelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Funcky.Extensions;
public static partial class EnumerableExtensions
{
/// <summary>
/// Filters out all the empty values from an <c>IEnumerable&lt;Option&lt;T&gt;&gt;</c> and therefore returns an <see cref="IEnumerable{T}"/>.
/// Filters out all the empty values from an <see cref="IEnumerable{T}"><![CDATA[IEnumerable<Option<T>>]]></see> and therefore returns an <see cref="IEnumerable{T}"/>.
/// </summary>
[Pure]
public static IEnumerable<TSource> WhereSelect<TSource>(this IEnumerable<Option<TSource>> source)
Expand All @@ -18,4 +18,10 @@ public static IEnumerable<TSource> WhereSelect<TSource>(this IEnumerable<Option<
public static IEnumerable<TResult> WhereSelect<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, Option<TResult>> selector)
where TResult : notnull
=> source.SelectMany(input => selector(input).ToEnumerable());

/// <inheritdoc cref="WhereSelect{TSource,TResult}(IEnumerable{TSource},Func{TSource,Option{TResult}})"/>
[Pure]
public static IEnumerable<TResult> WhereSelect<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, Option<TResult>> selector)
where TResult : notnull
=> source.SelectMany((input, index) => selector(input, index).ToEnumerable());
}
1 change: 1 addition & 0 deletions Funcky/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Funcky.Monads.IEither
Funcky.Monads.IOption
static Funcky.Extensions.DictionaryExtensions.RemoveOrNone<TKey, TValue>(this System.Collections.Generic.IDictionary<TKey, TValue>! dictionary, TKey key) -> Funcky.Monads.Option<TValue>
static Funcky.Extensions.EnumerableExtensions.Traverse<TSource, T>(this System.Collections.Generic.IEnumerable<TSource>! source, System.Func<TSource, System.Lazy<T>!>! selector) -> System.Lazy<System.Collections.Generic.IEnumerable<T>!>!
static Funcky.Extensions.EnumerableExtensions.WhereSelect<TSource, TResult>(this System.Collections.Generic.IEnumerable<TSource>! source, System.Func<TSource, int, Funcky.Monads.Option<TResult>>! selector) -> System.Collections.Generic.IEnumerable<TResult>!
static Funcky.Extensions.FuncExtensions.Apply<T1, T2, T3, T4, T5, TResult>(this System.Func<T1, T2, T3, T4, T5, TResult>! func, Funcky.Unit p1, Funcky.Unit p2, Funcky.Unit p3, Funcky.Unit p4, T5 p5) -> System.Func<T1, T2, T3, T4, TResult>!
static Funcky.Extensions.FuncExtensions.Apply<T1, T2, T3, T4, T5, TResult>(this System.Func<T1, T2, T3, T4, T5, TResult>! func, Funcky.Unit p1, Funcky.Unit p2, Funcky.Unit p3, T4 p4, Funcky.Unit p5) -> System.Func<T1, T2, T3, T5, TResult>!
static Funcky.Extensions.FuncExtensions.Apply<T1, T2, T3, T4, T5, TResult>(this System.Func<T1, T2, T3, T4, T5, TResult>! func, Funcky.Unit p1, Funcky.Unit p2, Funcky.Unit p3, T4 p4, T5 p5) -> System.Func<T1, T2, T3, TResult>!
Expand Down