-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from Innmind/lazy-identity
Add possibility to nest lazy `Sequence`s through a lazy `Identity`
- Loading branch information
Showing
13 changed files
with
444 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Innmind\Immutable\Identity; | ||
|
||
use Innmind\Immutable\{ | ||
Identity, | ||
Sequence, | ||
}; | ||
|
||
/** | ||
* @psalm-immutable | ||
* @template T | ||
* @implements Implementation<T> | ||
*/ | ||
final class Defer implements Implementation | ||
{ | ||
/** @var callable(): T */ | ||
private $value; | ||
private bool $loaded = false; | ||
/** @var ?T */ | ||
private mixed $computed = null; | ||
|
||
/** | ||
* @param callable(): T $value | ||
*/ | ||
public function __construct(callable $value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
public function map(callable $map): self | ||
{ | ||
/** @psalm-suppress ImpureFunctionCall */ | ||
return new self(fn() => $map($this->unwrap())); | ||
} | ||
|
||
public function flatMap(callable $map): Identity | ||
{ | ||
/** @psalm-suppress ImpureFunctionCall */ | ||
return Identity::lazy(fn() => $map($this->unwrap())->unwrap()); | ||
} | ||
|
||
public function toSequence(): Sequence | ||
{ | ||
/** @psalm-suppress ImpureFunctionCall */ | ||
return Sequence::defer((fn() => yield $this->unwrap())()); | ||
} | ||
|
||
public function unwrap(): mixed | ||
{ | ||
if ($this->loaded) { | ||
/** @var T */ | ||
return $this->computed; | ||
} | ||
|
||
/** | ||
* @psalm-suppress InaccessibleProperty | ||
* @psalm-suppress ImpureFunctionCall | ||
*/ | ||
$this->computed = ($this->value)(); | ||
/** @psalm-suppress InaccessibleProperty */ | ||
$this->loaded = true; | ||
|
||
return $this->computed; | ||
} | ||
} |
Oops, something went wrong.