-
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.
* develop: specify next release fix deferred/lazy Sequence::last() defer the values for ->get(), ->first(), ->last(), ->indexOf() and ->find() on a lazy/deferred Sequence add Maybe::defer() and Either::defer() add shortcut method Str::maybe()
- Loading branch information
Showing
16 changed files
with
507 additions
and
92 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
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,79 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Innmind\Immutable\Either; | ||
|
||
use Innmind\Immutable\{ | ||
Either, | ||
Maybe, | ||
}; | ||
|
||
/** | ||
* @template L1 | ||
* @template R1 | ||
* @implements Implementation<L1, R1> | ||
* @psalm-immutable | ||
* @internal | ||
*/ | ||
final class Defer implements Implementation | ||
{ | ||
/** @var callable(): Either<L1, R1> */ | ||
private $deferred; | ||
/** @var ?Either<L1, R1> */ | ||
private ?Either $value = null; | ||
|
||
/** | ||
* @param callable(): Either<L1, R1> $deferred | ||
*/ | ||
public function __construct($deferred) | ||
{ | ||
$this->deferred = $deferred; | ||
} | ||
|
||
public function map(callable $map): self | ||
{ | ||
return new self(fn() => $this->unwrap()->map($map)); | ||
} | ||
|
||
public function flatMap(callable $map): Either | ||
{ | ||
return Either::defer(fn() => $this->unwrap()->flatMap($map)); | ||
} | ||
|
||
public function leftMap(callable $map): self | ||
{ | ||
return new self(fn() => $this->unwrap()->leftMap($map)); | ||
} | ||
|
||
public function match(callable $right, callable $left) | ||
{ | ||
return $this->unwrap()->match($right, $left); | ||
} | ||
|
||
public function otherwise(callable $otherwise): Either | ||
{ | ||
return Either::defer(fn() => $this->unwrap()->otherwise($otherwise)); | ||
} | ||
|
||
public function filter(callable $predicate, callable $otherwise): Implementation | ||
{ | ||
return new self(fn() => $this->unwrap()->filter($predicate, $otherwise)); | ||
} | ||
|
||
public function maybe(): Maybe | ||
{ | ||
return Maybe::defer(fn() => $this->unwrap()->maybe()); | ||
} | ||
|
||
/** | ||
* @return Either<L1, R1> | ||
*/ | ||
private function unwrap(): Either | ||
{ | ||
/** | ||
* @psalm-suppress InaccessibleProperty | ||
* @psalm-suppress ImpureFunctionCall | ||
*/ | ||
return $this->value ??= ($this->deferred)(); | ||
} | ||
} |
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,73 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Innmind\Immutable\Maybe; | ||
|
||
use Innmind\Immutable\{ | ||
Maybe, | ||
Either, | ||
}; | ||
|
||
/** | ||
* @template V | ||
* @implements Implementation<V> | ||
* @psalm-immutable | ||
* @internal | ||
*/ | ||
final class Defer implements Implementation | ||
{ | ||
/** @var callable(): Maybe<V> */ | ||
private $deferred; | ||
/** @var ?Maybe<V> */ | ||
private ?Maybe $value = null; | ||
|
||
/** | ||
* @param callable(): Maybe<V> $deferred | ||
*/ | ||
public function __construct(callable $deferred) | ||
{ | ||
$this->deferred = $deferred; | ||
} | ||
|
||
public function map(callable $map): self | ||
{ | ||
return new self(fn() => $this->unwrap()->map($map)); | ||
} | ||
|
||
public function flatMap(callable $map): Maybe | ||
{ | ||
return Maybe::defer(fn() => $this->unwrap()->flatMap($map)); | ||
} | ||
|
||
public function match(callable $just, callable $nothing) | ||
{ | ||
return $this->unwrap()->match($just, $nothing); | ||
} | ||
|
||
public function otherwise(callable $otherwise): Maybe | ||
{ | ||
return Maybe::defer(fn() => $this->unwrap()->otherwise($otherwise)); | ||
} | ||
|
||
public function filter(callable $predicate): Implementation | ||
{ | ||
return new self(fn() => $this->unwrap()->filter($predicate)); | ||
} | ||
|
||
public function either(): Either | ||
{ | ||
return Either::defer(fn() => $this->unwrap()->either()); | ||
} | ||
|
||
/** | ||
* @return Maybe<V> | ||
*/ | ||
private function unwrap(): Maybe | ||
{ | ||
/** | ||
* @psalm-suppress InaccessibleProperty | ||
* @psalm-suppress ImpureFunctionCall | ||
*/ | ||
return $this->value ??= ($this->deferred)(); | ||
} | ||
} |
Oops, something went wrong.