Skip to content

Commit

Permalink
add proof on lazy/deferred Identity::flatMap()
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Jun 27, 2024
1 parent c971ccc commit a0d8d00
Showing 1 changed file with 55 additions and 9 deletions.
64 changes: 55 additions & 9 deletions proofs/identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,62 @@
Set\Type::any(),
Set\Type::any(),
),
static fn($assert, $initial, $expected) => $assert->same(
$expected,
Identity::of($initial)
->flatMap(static function($value) use ($assert, $initial, $expected) {
$assert->same($initial, $value);
static function($assert, $initial, $expected) {
$assert->same(
$expected,
Identity::of($initial)
->flatMap(static function($value) use ($assert, $initial, $expected) {
$assert->same($initial, $value);

return Identity::of($expected);
})
->unwrap(),
),
return Identity::of($expected);
})
->unwrap(),
);

$loaded = 0;
$identity = Identity::defer(static function() use (&$loaded, $initial) {
$loaded++;

return $initial;
})->flatMap(static function($value) use ($assert, $initial, $expected) {
$assert->same($initial, $value);

return Identity::of($expected);
});
$assert->same(0, $loaded);
$assert->same(
$expected,
$identity->unwrap(),
);
$assert->same(1, $loaded);
$assert->same(
$expected,
$identity->unwrap(),
);
$assert->same(1, $loaded);

$loaded = 0;
$identity = Identity::lazy(static function() use (&$loaded, $initial) {
$loaded++;

return $initial;
})->flatMap(static function($value) use ($assert, $initial, $expected) {
$assert->same($initial, $value);

return Identity::of($expected);
});
$assert->same(0, $loaded);
$assert->same(
$expected,
$identity->unwrap(),
);
$assert->same(1, $loaded);
$assert->same(
$expected,
$identity->unwrap(),
);
$assert->same(2, $loaded);
},
);

yield proof(
Expand Down

0 comments on commit a0d8d00

Please sign in to comment.