Skip to content

Commit 58f516e

Browse files
committed
[Functional] toArrayList cast for Option.
1 parent 804f11b commit 58f516e

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/Fp/Functional/Option/Option.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Fp\Functional\Option;
66

77
use Error;
8+
use Fp\Collections\ArrayList;
89
use Fp\Functional\Either\Either;
910
use Fp\Functional\Either\Left;
1011
use Fp\Functional\Either\Right;
@@ -663,4 +664,36 @@ public static function condLazy(bool $condition, callable $some): Option
663664
? self::some($some())
664665
: self::none();
665666
}
667+
668+
/**
669+
* Convert optional value to ArrayList collection
670+
* via transformation function $callback.
671+
*
672+
* ```php
673+
* >>> Option::some(1)
674+
* ->toArrayList(ArrayList::singleton(...))
675+
* ->toArray();
676+
* => [1]
677+
*
678+
* >>> Option::some([1])
679+
* ->toArrayList(ArrayList::collect(...))
680+
* ->toArray();
681+
* => [1]
682+
*
683+
* >>> Option::none()
684+
* ->toArrayList(ArrayList::collect(...))
685+
* ->toArray();
686+
* => []
687+
* ```
688+
*
689+
* @template TOutValue
690+
* @param callable(A): ArrayList<TOutValue> $callback
691+
* @return ArrayList<TOutValue>
692+
*/
693+
public function toArrayList(callable $callback): ArrayList
694+
{
695+
return $this->isSome()
696+
? $callback($this->value)
697+
: new ArrayList([]);
698+
}
666699
}

tests/Runtime/Classes/Option/OptionTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Error;
88
use Exception;
9+
use Fp\Collections\ArrayList;
910
use Fp\Functional\Either\Left;
1011
use Fp\Functional\Either\Right;
1112
use Fp\Functional\Option\None;
@@ -197,4 +198,22 @@ public function testFlatTap(): void
197198
$this->assertEquals(1, Option::some(1)->flatTap(fn($e) => Option::some(2))->get());
198199
$this->assertNull(Option::some(1)->flatTap(fn($e) => Option::none())->get());
199200
}
201+
202+
public function testToArrayList(): void
203+
{
204+
$this->assertEquals(
205+
[1],
206+
Option::some(1)->toArrayList(fn(int $val) => ArrayList::singleton($val))->toArray()
207+
);
208+
209+
$this->assertEquals(
210+
[1],
211+
Option::some([1])->toArrayList(fn(array $val) => ArrayList::collect($val))->toArray()
212+
);
213+
214+
$this->assertEquals(
215+
[],
216+
Option::none()->toArrayList(fn(array $val) => ArrayList::collect($val))->toArray()
217+
);
218+
}
200219
}

tests/Static/Classes/Option/OptionStaticTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests\Static\Classes\Option;
66

7+
use Fp\Collections\ArrayList;
78
use Fp\Functional\Option\Option;
89

910
final class OptionStaticTest
@@ -44,4 +45,13 @@ public function testFlatMap(): ?string
4445
->flatMap(fn(int $v) => Option::fromNullable((string) $v))
4546
->get();
4647
}
48+
49+
/**
50+
* @return ArrayList<1>
51+
*/
52+
public function testToArrayList(): ArrayList
53+
{
54+
return Option::fromNullable(1)
55+
->toArrayList(fn(int $v) => ArrayList::singleton($v));
56+
}
4757
}

0 commit comments

Comments
 (0)