Skip to content

Commit 6b2fc4e

Browse files
committed
Merge branch 'develop'
* develop: specify release number build a Maybe out of an Either
2 parents 51ca383 + f768be9 commit 6b2fc4e

File tree

7 files changed

+80
-3
lines changed

7 files changed

+80
-3
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 4.3.0 - 2022-07-02
4+
5+
### Added
6+
7+
- `Innmind\Immutable\Either::maybe()`
8+
39
## 4.2.0 - 2022-03-26
410

511
### Added

docs/EITHER.md

+15
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,18 @@ This is similar to the `->map()` function but will be applied on the left value
138138
$either = identify($request)
139139
->leftMap(fn(Error $error) => new ErrorResponse($error));
140140
```
141+
142+
## `->maybe()`
143+
144+
This returns a [`Maybe`](MAYBE.md) containing the right value, in case of a left value it returns a `Maybe` with nothing inside.
145+
146+
```php
147+
Either::right('something')->maybe()->match(
148+
static fn($value) => $value,
149+
static fn() => null,
150+
); // returns 'something'
151+
Either::left('something')->maybe()->match(
152+
static fn($value) => $value,
153+
static fn() => null,
154+
); // returns null
155+
```

src/Either.php

+8
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,12 @@ public function filter(callable $predicate, callable $otherwise): self
130130
{
131131
return new self($this->either->filter($predicate, $otherwise));
132132
}
133+
134+
/**
135+
* @return Maybe<R>
136+
*/
137+
public function maybe(): Maybe
138+
{
139+
return $this->either->maybe();
140+
}
133141
}

src/Either/Implementation.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
namespace Innmind\Immutable\Either;
55

6-
use Innmind\Immutable\Either;
6+
use Innmind\Immutable\{
7+
Either,
8+
Maybe,
9+
};
710

811
/**
912
* @template L
@@ -70,4 +73,9 @@ public function otherwise(callable $otherwise): Either;
7073
* @return self<L|A, R>
7174
*/
7275
public function filter(callable $predicate, callable $otherwise): self;
76+
77+
/**
78+
* @return Maybe<R>
79+
*/
80+
public function maybe(): Maybe;
7381
}

src/Either/Left.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
namespace Innmind\Immutable\Either;
55

6-
use Innmind\Immutable\Either;
6+
use Innmind\Immutable\{
7+
Either,
8+
Maybe,
9+
};
710

811
/**
912
* @template L1
@@ -62,4 +65,9 @@ public function filter(callable $predicate, callable $otherwise): self
6265
{
6366
return $this;
6467
}
68+
69+
public function maybe(): Maybe
70+
{
71+
return Maybe::nothing();
72+
}
6573
}

src/Either/Right.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
namespace Innmind\Immutable\Either;
55

6-
use Innmind\Immutable\Either;
6+
use Innmind\Immutable\{
7+
Either,
8+
Maybe,
9+
};
710

811
/**
912
* @template L1
@@ -66,4 +69,9 @@ public function filter(callable $predicate, callable $otherwise): Implementation
6669

6770
return new Left($otherwise());
6871
}
72+
73+
public function maybe(): Maybe
74+
{
75+
return Maybe::just($this->value);
76+
}
6977
}

tests/EitherTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,28 @@ public function testLeftValueIsLeftMapped()
328328
);
329329
});
330330
}
331+
332+
public function testMaybe()
333+
{
334+
$this
335+
->forAll(
336+
Set\AnyType::any(),
337+
Set\AnyType::any(),
338+
)
339+
->then(function($left, $right) {
340+
$this->assertSame(
341+
$right,
342+
Either::right($right)->maybe()->match(
343+
static fn($value) => $value,
344+
static fn() => null,
345+
),
346+
);
347+
$this->assertNull(
348+
Either::left($left)->maybe()->match(
349+
static fn($value) => $value,
350+
static fn() => null,
351+
),
352+
);
353+
});
354+
}
331355
}

0 commit comments

Comments
 (0)