-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #565 from marcinjahn/maybe-bind-allocation-free
Add allocation-free Maybe.Bind overload
- Loading branch information
Showing
2 changed files
with
124 additions
and
54 deletions.
There are no files selected for viewing
136 changes: 97 additions & 39 deletions
136
CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |