fix: use correct signature on bind

This commit is contained in:
Kyle Ratti 2023-12-17 13:47:16 -05:00
parent c3598a61ed
commit ef3a54eae7
No known key found for this signature in database
GPG Key ID: 4D429B6287C68DD9
2 changed files with 46 additions and 1 deletions

View 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);
}
}

View File

@ -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>()