fix: use correct signature on bind
This commit is contained in:
parent
c3598a61ed
commit
ef3a54eae7
45
Base.Tests/Structures/MaybeTests.cs
Normal file
45
Base.Tests/Structures/MaybeTests.cs
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
using FruityFoundation.Base.Structures;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace Base.Tests.Structures;
|
||||||
|
|
||||||
|
public class MaybeTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void TestBind_DoesNotBindEmptyValue()
|
||||||
|
{
|
||||||
|
var emptyMaybe = Maybe.Empty<int>();
|
||||||
|
|
||||||
|
var result = emptyMaybe.Bind(x => Maybe.Just(x + 1));
|
||||||
|
|
||||||
|
Assert.That(result, Is.InstanceOf<Maybe<int>>());
|
||||||
|
Assert.That(result.HasValue, Is.False);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestBind_BindsWhenHasValue()
|
||||||
|
{
|
||||||
|
var emptyMaybe = Maybe.Just<int>(1);
|
||||||
|
|
||||||
|
var result = emptyMaybe.Bind(x => Maybe.Just(x + 1));
|
||||||
|
|
||||||
|
Assert.That(result, Is.InstanceOf<Maybe<int>>());
|
||||||
|
Assert.That(result.HasValue, Is.True);
|
||||||
|
Assert.That(result.Value, Is.EqualTo(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestBind_BindsWhenHasValue_NewOutputType()
|
||||||
|
{
|
||||||
|
var maybe = Maybe.Just(25);
|
||||||
|
|
||||||
|
var result = maybe.Bind(x => x switch
|
||||||
|
{
|
||||||
|
25 => Maybe.Just("twenty-five"),
|
||||||
|
_ => Maybe.Empty<string>()
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.That(result, Is.InstanceOf<Maybe<string>>());
|
||||||
|
Assert.That(result.HasValue, Is.True);
|
||||||
|
}
|
||||||
|
}
|
|
@ -54,7 +54,7 @@ public readonly struct Maybe<T>
|
||||||
public Maybe<TOutput> Map<TOutput>(Func<T, TOutput> transformer) =>
|
public Maybe<TOutput> Map<TOutput>(Func<T, TOutput> transformer) =>
|
||||||
HasValue ? Maybe.Just(transformer(Value)) : Maybe.Empty<TOutput>();
|
HasValue ? Maybe.Just(transformer(Value)) : Maybe.Empty<TOutput>();
|
||||||
|
|
||||||
public Maybe<TOutput> Bind<TOutput>(Func<T, TOutput> binder) =>
|
public Maybe<TOutput> Bind<TOutput>(Func<T, Maybe<TOutput>> binder) =>
|
||||||
HasValue ? binder(Value) : Maybe.Empty<TOutput>();
|
HasValue ? binder(Value) : Maybe.Empty<TOutput>();
|
||||||
|
|
||||||
public Maybe<TOutput> Cast<TOutput>()
|
public Maybe<TOutput> Cast<TOutput>()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user