Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add allocation-free Maybe.Bind overload #565

Merged
merged 1 commit into from
Sep 28, 2024
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
136 changes: 97 additions & 39 deletions CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,97 @@
using FluentAssertions;
using Xunit;

namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions
{
public class BindTests : MaybeTestBase
{
[Fact]
public void Bind_returns_no_value_if_initial_maybe_is_null()
{
Maybe<T> maybe = null;

var maybe2 = maybe.Bind(ExpectAndReturnMaybe(null, T.Value));

maybe2.HasValue.Should().BeFalse();
}

[Fact]
public void Bind_returns_no_value_if_selector_returns_null()
{
Maybe<T> maybe = T.Value;

var maybe2 = maybe.Bind(ExpectAndReturn(T.Value, Maybe<T>.None));

maybe2.HasValue.Should().BeFalse();
}

[Fact]
public void Bind_returns_value_if_selector_returns_value()
{
Maybe<T> maybe = T.Value;

var maybe2 = maybe.Bind(ExpectAndReturnMaybe<T>(T.Value, T.Value));

maybe2.HasValue.Should().BeTrue();
maybe2.Value.Should().Be(T.Value);
}
}
}
using FluentAssertions;
using Xunit;

namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions
{
public class BindTests : MaybeTestBase
{
[Fact]
public void Bind_returns_no_value_if_initial_maybe_is_null()
{
Maybe<T> maybe = null;

var maybe2 = maybe.Bind(ExpectAndReturnMaybe(null, T.Value));

maybe2.HasValue.Should().BeFalse();
}

[Fact]
public void Bind_returns_no_value_if_selector_returns_null()
{
Maybe<T> maybe = T.Value;

var maybe2 = maybe.Bind(ExpectAndReturn(T.Value, Maybe<T>.None));

maybe2.HasValue.Should().BeFalse();
}

[Fact]
public void Bind_returns_value_if_selector_returns_value()
{
Maybe<T> maybe = T.Value;

var maybe2 = maybe.Bind(ExpectAndReturnMaybe<T>(T.Value, T.Value));

maybe2.HasValue.Should().BeTrue();
maybe2.Value.Should().Be(T.Value);
}

[Fact]
public void Bind_provides_context_to_selector()
{
Maybe<T> maybe = null;
var context = 5;

var maybe2 = maybe.Bind(
(value, ctx) =>
{
ctx.Should().Be(context);
return Maybe.From(value);
},
context
);

maybe2.HasValue.Should().BeFalse();
}

[Fact]
public void Bind_with_context_returns_no_value_if_initial_maybe_is_null()
{
Maybe<T> maybe = null;

var maybe2 = maybe.Bind(
(value, _) => ExpectAndReturnMaybe(null, T.Value)(value),
context: 5
);

maybe2.HasValue.Should().BeFalse();
}

[Fact]
public void Bind_with_context_returns_no_value_if_selector_returns_null()
{
Maybe<T> maybe = T.Value;

var maybe2 = maybe.Bind(
(value, _) => ExpectAndReturn(T.Value, Maybe<T>.None)(value),
context: 5
);

maybe2.HasValue.Should().BeFalse();
}

[Fact]
public void Bind_with_context_returns_value_if_selector_returns_value()
{
Maybe<T> maybe = T.Value;

var maybe2 = maybe.Bind(
(value, _) => ExpectAndReturnMaybe<T>(T.Value, T.Value)(value),
5
);

maybe2.HasValue.Should().BeTrue();
maybe2.Value.Should().Be(T.Value);
}
}
}
42 changes: 27 additions & 15 deletions CSharpFunctionalExtensions/Maybe/Extensions/Bind.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
using System;

namespace CSharpFunctionalExtensions
{
public static partial class MaybeExtensions
{
public static Maybe<K> Bind<T, K>(in this Maybe<T> maybe, Func<T, Maybe<K>> selector)
{
if (maybe.HasNoValue)
return Maybe<K>.None;

return selector(maybe.GetValueOrThrow());
}
}
}
using System;

namespace CSharpFunctionalExtensions
{
public static partial class MaybeExtensions
{
public static Maybe<K> Bind<T, K>(in this Maybe<T> maybe, Func<T, Maybe<K>> selector)
{
if (maybe.HasNoValue)
return Maybe<K>.None;

return selector(maybe.GetValueOrThrow());
}

public static Maybe<K> Bind<T, K, TContext>(
in this Maybe<T> maybe,
Func<T, TContext, Maybe<K>> selector,
TContext context
)
{
if (maybe.HasNoValue)
return Maybe<K>.None;

return selector(maybe.GetValueOrThrow(), context);
}
}
}