-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
9b3252f
commit fe2dd55
Showing
6 changed files
with
171 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ | |
"src/reduce.php", | ||
"src/take.php", | ||
"src/flatten.php", | ||
"src/each.php", | ||
"src/until.php" | ||
] | ||
}, | ||
|
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,51 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the camille-hdl/lazy-lists library | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Camille Hodoul <camille.hodoul@gmail.com> | ||
* @license http://opensource.org/licenses/MIT MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LazyLists\Transducer; | ||
|
||
class Each extends PureTransducer implements TransducerInterface | ||
{ | ||
/** | ||
* Applies $sideEffect to each $item, but does not change the $item itself | ||
* | ||
* @var callable | ||
*/ | ||
protected $sideEffect; | ||
|
||
public function __construct(callable $sideEffect) | ||
{ | ||
$this->sideEffect = $sideEffect; | ||
} | ||
|
||
public function computeNextResult($item) | ||
{ | ||
$sideEffect = $this->sideEffect; | ||
$sideEffect($item); | ||
$this->iterator->yieldToNextTransducer($item); | ||
} | ||
|
||
public function getEmptyFinalResult() | ||
{ | ||
return []; | ||
} | ||
|
||
public function computeFinalResult($previousResult, $lastValue) | ||
{ | ||
if (\is_null($previousResult)) { | ||
return []; | ||
} | ||
$previousResult[] = $lastValue; | ||
return $previousResult; | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the camille-hdl/lazy-lists library | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Camille Hodoul <camille.hodoul@gmail.com> | ||
* @license http://opensource.org/licenses/MIT MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LazyLists; | ||
|
||
use LazyLists\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* Calls `$sideEffect` on each element in `$list`. | ||
* If $list is omitted, returns a Transducer to be used with `pipe()` instead. | ||
* | ||
* @param callable $sideEffect | ||
* @param array|\Traversable|null $list | ||
* @see \LazyLists\Transducer\Map | ||
* @return mixed void or \LazyLists\Transducer\Map | ||
*/ | ||
function each(callable $sideEffect, $list = null) | ||
{ | ||
if (\is_null($list)) { | ||
return new \LazyLists\Transducer\Each($sideEffect); | ||
} | ||
|
||
InvalidArgumentException::assertCollection($list, __FUNCTION__, 2); | ||
foreach ($list as $key => $item) { | ||
$sideEffect($item, $key); | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LazyLists\Test; | ||
|
||
use function LazyLists\each; | ||
|
||
class EachTest extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
|
||
$list = ['value', 'value']; | ||
$c = 0; | ||
$fn = function ($v) use (&$c) { | ||
$c++; | ||
}; | ||
each($fn, $list); | ||
$this->assertEquals(2, $c); | ||
} | ||
|
||
public function testPassNoCollection() | ||
{ | ||
$this->expectException(\Exception::class); | ||
each(function () { | ||
return; | ||
}, 'invalidCollection'); | ||
} | ||
|
||
public function testPassNonCallable() | ||
{ | ||
$this->expectException(\TypeError::class); | ||
each('notACallable', []); | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LazyLists\Test\Transducer; | ||
|
||
use LazyLists\Test\TestCase; | ||
|
||
use function LazyLists\each; | ||
use function LazyLists\filter; | ||
use function LazyLists\pipe; | ||
|
||
class EachTest extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$log = []; | ||
$logValue = each(function ($v) use (&$log) { | ||
$log[] = $v; | ||
}); | ||
$pipe2 = pipe($logValue); | ||
$this->assertSame([1, 2, 3], $pipe2([1, 2, 3])); | ||
} | ||
} |