Skip to content

Commit

Permalink
Merge pull request #741 from polyadic/to-async-enumerable
Browse files Browse the repository at this point in the history
Add `ToAsyncEnumerable` extension for `Option`
  • Loading branch information
bash authored Aug 7, 2023
2 parents 3e59a6a + 961094c commit 0f4dcaf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Funcky.Async.Test.Monads.OptionAsyncExtensions;

public sealed class ToAsyncEnumerableTest
{
[Fact]
public async Task ReturnsEmptyEnumerableWhenOptionIsEmpty()
{
var option = Option<int>.None;
Assert.True(await option.ToAsyncEnumerable().NoneAsync());
}

[Fact]
public async Task ReturnsEnumerableWithOneElementWhenOptionHasValue()
{
const int value = 42;
var option = Option.Some(value);
Assert.Equal(value, await option.ToAsyncEnumerable().SingleAsync());
}
}
14 changes: 14 additions & 0 deletions Funcky.Async/Monads/Option/OptionExtensions.ToAsyncEnumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Funcky.Monads;

public static partial class OptionAsyncExtensions
{
/// <summary>
/// Returns an <see cref="IAsyncEnumerable{T}"/> that yields exactly one value when the option
/// has an item and nothing when the option is empty.
/// </summary>
public static IAsyncEnumerable<TItem> ToAsyncEnumerable<TItem>(this Option<TItem> option)
where TItem : notnull
=> option.Match(
none: AsyncEnumerable.Empty<TItem>,
some: AsyncSequence.Return);
}
1 change: 1 addition & 0 deletions Funcky.Async/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
static Funcky.Monads.OptionAsyncExtensions.ToAsyncEnumerable<TItem>(this Funcky.Monads.Option<TItem> option) -> System.Collections.Generic.IAsyncEnumerable<TItem>!

0 comments on commit 0f4dcaf

Please sign in to comment.