Skip to content

Commit

Permalink
Merge pull request #565 from marcinjahn/maybe-bind-allocation-free
Browse files Browse the repository at this point in the history
Add allocation-free Maybe.Bind overload
  • Loading branch information
vkhorikov authored Sep 28, 2024
2 parents c009233 + e7e03e5 commit 9599a06
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 54 deletions.
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);
}
}
}

0 comments on commit 9599a06

Please sign in to comment.