Skip to content

Commit

Permalink
each
Browse files Browse the repository at this point in the history
  • Loading branch information
camille-hdl committed Jun 4, 2020
1 parent 9b3252f commit fe2dd55
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"src/reduce.php",
"src/take.php",
"src/flatten.php",
"src/each.php",
"src/until.php"
]
},
Expand Down
51 changes: 51 additions & 0 deletions src/Transducer/Each.php
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 <[email protected]>
* @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;
}
}
38 changes: 38 additions & 0 deletions src/each.php
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 <[email protected]>
* @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);
}
}
36 changes: 36 additions & 0 deletions tests/EachTest.php
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', []);
}
}
21 changes: 21 additions & 0 deletions tests/PipeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use function LazyLists\flatten;
use function LazyLists\reduce;
use function LazyLists\take;
use function LazyLists\each;
use function LazyLists\until;

/**
* "integration" tests
Expand Down Expand Up @@ -195,4 +197,23 @@ public function testRealUseCase()
$this->assertEquals(7, $sup_count);
$this->assertEquals(3, $sum_count);
}

public function testFlattenEachUntil()
{
$list = [
[1, 2],
[3, 4]
];
$c = 0;
$pipe = pipe(
flatten(1),
each(function ($v) use (&$c) {
$c = $c + $v;
}),
until(function ($v) use (&$c) {
return $c > 6;
})
);
$this->assertSame([1, 2, 3], $pipe($list));
}
}
24 changes: 24 additions & 0 deletions tests/Transducer/EachTest.php
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]));
}
}

0 comments on commit fe2dd55

Please sign in to comment.