Skip to content

Commit

Permalink
Merge pull request #34 from Flumm3ry/feature/add-else-with-value
Browse files Browse the repository at this point in the history
Else with value without callback
  • Loading branch information
khanage authored Dec 3, 2023
2 parents 57426a3 + bb07869 commit e124619
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Bearded.Monads.Tests/OptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,26 @@ public void ElseWithNoneReturnsElse()
Assert.Equal(666, def);
}

[Fact]
public void ElseValueWithSomeReturnsSome()
{
var option = Option.Return(42);

var def = option.Else(666);

Assert.Equal(42, def);
}

[Fact]
public void ElseValueWithNoneReturnsValue()
{
var option = Option<int>.None;

var def = option.Else(666);

Assert.Equal(666, def);
}

[Fact]
public void SelectManyFlattensOption()
{
Expand Down
6 changes: 6 additions & 0 deletions Bearded.Monads/Option.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public static Option<A> Return(A a)

public abstract A Else(Func<A> callbackForNone);

public abstract A Else(A valueForNone);

public abstract Option<CastTarget> Cast<CastTarget>() where CastTarget : A;

public abstract bool Equals(A other);
Expand Down Expand Up @@ -89,6 +91,8 @@ public Some(A force)

public override A Else(Func<A> callbackForNone) => force;

public override A Else(A valueForNone) => force;

public override Option<CastTarget> Cast<CastTarget>()
{
if (force is CastTarget)
Expand Down Expand Up @@ -133,6 +137,8 @@ public override void Do(Action<A> valueCallback, Action nullCallback)

public override A Else(Func<A> callbackForNone) => callbackForNone();

public override A Else(A valueForNone) => valueForNone;

public override Option<CastTarget> Cast<CastTarget>()
{
return Option<CastTarget>.None;
Expand Down

0 comments on commit e124619

Please sign in to comment.