-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
178 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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App; | ||
|
||
include __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use loophp\collection\Collection; | ||
|
||
$input = range('a', 'e'); | ||
|
||
$collection = Collection::fromIterable($input) | ||
->coalesce(); // [ 0 => 'a' ] | ||
|
||
$input = ['', null, 'foo', false, ...range('a', 'e')]; | ||
|
||
$collection = Collection::fromIterable($input) | ||
->coalesce(); // [ 2 => 'foo' ] |
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\loophp\collection\Operation; | ||
|
||
use ArrayIterator; | ||
use loophp\collection\Operation\Coalesce; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class CoalesceSpec extends ObjectBehavior | ||
{ | ||
public function it_can_coalesce() | ||
{ | ||
$input = range('a', 'e'); | ||
|
||
$iterator = new ArrayIterator($input); | ||
|
||
$this | ||
->__invoke()($iterator) | ||
->shouldHaveCount(1); | ||
|
||
$this | ||
->__invoke()($iterator) | ||
->shouldIterateAs([ | ||
0 => 'a', | ||
]); | ||
|
||
$input = ['', null, 'foo', false, ...range('a', 'e')]; | ||
|
||
$iterator = new ArrayIterator($input); | ||
|
||
$this | ||
->__invoke()($iterator) | ||
->shouldHaveCount(1); | ||
|
||
$this | ||
->__invoke()($iterator) | ||
->shouldIterateAs([ | ||
2 => 'foo', | ||
]); | ||
} | ||
|
||
public function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(Coalesce::class); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Contract\Operation; | ||
|
||
use loophp\collection\Contract\Collection; | ||
|
||
/** | ||
* @psalm-template TKey | ||
* @psalm-template TKey of array-key | ||
* @psalm-template T | ||
*/ | ||
interface Coalesceable | ||
{ | ||
/** | ||
* Return the first non-nullsy value of the collection. | ||
* | ||
* @psalm-return \loophp\collection\Collection<TKey, T> | ||
*/ | ||
public function coalesce(): Collection; | ||
} |
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Operation; | ||
|
||
use Closure; | ||
use Generator; | ||
|
||
/** | ||
* @psalm-template TKey | ||
* @psalm-template TKey of array-key | ||
* @psalm-template T | ||
*/ | ||
final class Coalesce extends AbstractOperation | ||
{ | ||
/** | ||
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T> | ||
*/ | ||
public function __invoke(): Closure | ||
{ | ||
/** @psalm-var Closure(Iterator<TKey, T>): Generator<TKey, T> $pipe */ | ||
$pipe = Pipe::of()( | ||
Compact::of()(), | ||
Head::of(), | ||
); | ||
|
||
// Point free style. | ||
return $pipe; | ||
} | ||
} |