Skip to content

Commit

Permalink
add withDefault to Maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosh committed Oct 16, 2021
1 parent 5593f82 commit ae0dab4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions spec/MaybeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@
expect($result)->toEqual(84);
});

it('uses the default value when extracting a Nothing', function () {
$maybe = Maybe::nothing();

expect($maybe->fromMaybe(42))->toBe(42);
});

it('return the inner value when extracting a Just', function () {
$maybe = Maybe::just(42);

expect($maybe->fromMaybe(37))->toBe(42);
});

it('maps a nothing to a nothing', function () {
$maybe = Maybe::nothing();

Expand Down
16 changes: 16 additions & 0 deletions src/Maybe.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ public function eval(
return $ifNothing;
}

/**
* @param A $a
* @return A
*/
public function fromMaybe($a)
{
return $this->eval(
$a,
/**
* @param A $a
* @return A
*/
fn($a) => $a
);
}

/**
* @template B
* @param Functor<MaybeBrand> $functor
Expand Down

0 comments on commit ae0dab4

Please sign in to comment.