-
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 add missing documentation update changelog add identity documentation add Sequence::toIdentity() add Identity monad
- Loading branch information
Showing
9 changed files
with
266 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# `Identity` | ||
|
||
This is the simplest monad there is. It's a simple wrapper around a value to allow chaining calls on this value. | ||
|
||
Let's say you have a string you want to camelize, here's how you'd do it: | ||
|
||
=== "Identity" | ||
```php | ||
$value = Identity::of('some value to camelize') | ||
->map(fn($string) => \explode(' ', $string)) | ||
->map(fn($parts) => \array_map( | ||
\ucfirst(...), | ||
$parts, | ||
)) | ||
->map(fn($parts) => \implode('', $parts)) | ||
->map(\lcfirst(...)) | ||
->unwrap(); | ||
|
||
echo $value; // outputs "someValueToCamelize" | ||
``` | ||
|
||
=== "Imperative" | ||
```php | ||
$string = 'some value to camelize'; | ||
$parts = \explode(' ', $string); | ||
$parts = \array_map( | ||
\ucfirst(...), | ||
$parts, | ||
); | ||
$string = \implode('', $parts); | ||
$value = \lcfirst($string); | ||
|
||
echo $value; // outputs "someValueToCamelize" | ||
``` | ||
|
||
=== "Pyramid of doom" | ||
```php | ||
$value = \lcfirst( | ||
\implode( | ||
'', | ||
\array_map( | ||
\ucfirst(...), | ||
\explode( | ||
' ', | ||
'some value to camelize', | ||
), | ||
), | ||
), | ||
); | ||
|
||
echo $value; // outputs "someValueToCamelize" | ||
``` | ||
|
||
!!! abstract "" | ||
In the end this monad does not provide any behaviour, it's a different way to write and read your code. |
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,74 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
use Innmind\Immutable\Identity; | ||
use Innmind\BlackBox\Set; | ||
|
||
return static function() { | ||
yield proof( | ||
'Identity::unwrap()', | ||
given(Set\Type::any()), | ||
static fn($assert, $value) => $assert->same( | ||
$value, | ||
Identity::of($value)->unwrap(), | ||
), | ||
); | ||
|
||
yield proof( | ||
'Identity::map()', | ||
given( | ||
Set\Type::any(), | ||
Set\Type::any(), | ||
), | ||
static fn($assert, $initial, $expected) => $assert->same( | ||
$expected, | ||
Identity::of($initial) | ||
->map(static function($value) use ($assert, $initial, $expected) { | ||
$assert->same($initial, $value); | ||
|
||
return $expected; | ||
}) | ||
->unwrap(), | ||
), | ||
); | ||
|
||
yield proof( | ||
'Identity::flatMap()', | ||
given( | ||
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); | ||
|
||
return Identity::of($expected); | ||
}) | ||
->unwrap(), | ||
), | ||
); | ||
|
||
yield proof( | ||
'Identity::map() and ::flatMap() interchangeability', | ||
given( | ||
Set\Type::any(), | ||
Set\Type::any(), | ||
Set\Type::any(), | ||
), | ||
static fn($assert, $initial, $intermediate, $expected) => $assert->same( | ||
Identity::of($initial) | ||
->flatMap(static fn() => Identity::of($intermediate)) | ||
->map(static fn() => $expected) | ||
->unwrap(), | ||
Identity::of($initial) | ||
->flatMap( | ||
static fn() => Identity::of($intermediate)->map( | ||
static fn() => $expected, | ||
), | ||
) | ||
->unwrap(), | ||
), | ||
); | ||
}; |
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,18 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
use Innmind\Immutable\Sequence; | ||
|
||
return static function() { | ||
yield test( | ||
'Sequence::toIdentity()', | ||
static function($assert) { | ||
$sequence = Sequence::of(); | ||
|
||
$assert->same( | ||
$sequence, | ||
$sequence->toIdentity()->unwrap(), | ||
); | ||
}, | ||
); | ||
}; |
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,69 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Innmind\Immutable; | ||
|
||
/** | ||
* @psalm-immutable | ||
* @template T | ||
*/ | ||
final class Identity | ||
{ | ||
/** @var T */ | ||
private mixed $value; | ||
|
||
/** | ||
* @param T $value | ||
*/ | ||
private function __construct(mixed $value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
/** | ||
* @psalm-pure | ||
* @template A | ||
* | ||
* @param A $value | ||
* | ||
* @return self<A> | ||
*/ | ||
public static function of(mixed $value): self | ||
{ | ||
return new self($value); | ||
} | ||
|
||
/** | ||
* @template U | ||
* | ||
* @param callable(T): U $map | ||
* | ||
* @return self<U> | ||
*/ | ||
public function map(callable $map): self | ||
{ | ||
/** @psalm-suppress ImpureFunctionCall */ | ||
return new self($map($this->value)); | ||
} | ||
|
||
/** | ||
* @template U | ||
* | ||
* @param callable(T): self<U> $map | ||
* | ||
* @return self<U> | ||
*/ | ||
public function flatMap(callable $map): self | ||
{ | ||
/** @psalm-suppress ImpureFunctionCall */ | ||
return $map($this->value); | ||
} | ||
|
||
/** | ||
* @return T | ||
*/ | ||
public function unwrap(): mixed | ||
{ | ||
return $this->value; | ||
} | ||
} |
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