Skip to content

Commit

Permalink
disambiguate LazyIterator: renamed to LazyWorker because it does …
Browse files Browse the repository at this point in the history
…not extend Iterator
  • Loading branch information
camille-hdl committed Jun 4, 2020
1 parent fe2dd55 commit f829058
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/LazyIterator.php → src/LazyWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

/**
* Wraps an Iterator and orchestrates the Transducers
* Transducers can iteract with the LazyIterator through its
* Transducers can iteract with the LazyWorker through its
* public methods.
*
* This class can be instanciated by `LazyLists\pipe()`
* @see \LazyLists\pipe
*/
class LazyIterator
class LazyWorker
{
/**
* @var \Iterator
Expand Down
2 changes: 1 addition & 1 deletion src/Transducer/Each.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function computeNextResult($item)
{
$sideEffect = $this->sideEffect;
$sideEffect($item);
$this->iterator->yieldToNextTransducer($item);
$this->worker->yieldToNextTransducer($item);
}

public function getEmptyFinalResult()
Expand Down
4 changes: 2 additions & 2 deletions src/Transducer/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function computeNextResult($item)
{
$predicate = $this->predicate;
if ($predicate($item)) {
$this->iterator->yieldToNextTransducer($item);
$this->worker->yieldToNextTransducer($item);
} else {
$this->iterator->skipToNextLoop();
$this->worker->skipToNextLoop();
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Transducer/Flatten.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ public static function flattenItem(int $levels, $item): array
public function computeNextResult($item)
{
if (!\is_array($item)) {
$this->iterator->yieldToNextTransducer($item);
$this->worker->yieldToNextTransducer($item);
return;
}
if (isAssociativeArray($item)) {
$this->iterator->yieldToNextTransducer($item);
$this->worker->yieldToNextTransducer($item);
return;
}
$flattened = self::flattenItem($this->levels, $item);
$this->iterator->yieldToNextTransducerWithFutureValues($flattened);
$this->worker->yieldToNextTransducerWithFutureValues($flattened);
}

public function getEmptyFinalResult()
Expand Down
2 changes: 1 addition & 1 deletion src/Transducer/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(callable $procedure)
public function computeNextResult($item)
{
$procedure = $this->procedure;
$this->iterator->yieldToNextTransducer($procedure($item));
$this->worker->yieldToNextTransducer($procedure($item));
}

public function getEmptyFinalResult()
Expand Down
8 changes: 4 additions & 4 deletions src/Transducer/PureTransducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@

namespace LazyLists\Transducer;

use LazyLists\LazyIterator;
use LazyLists\LazyWorker;

/**
* Models a simple transducer that maintains no internal state.
* For example : Map
*/
abstract class PureTransducer
{
protected $iterator;
protected $worker;
public function __invoke($item)
{
$this->computeNextResult($item);
}

public function initialize(LazyIterator $iterator)
public function initialize(LazyWorker $worker)
{
$this->iterator = $iterator;
$this->worker = $worker;
}

abstract public function computeNextResult($item);
Expand Down
10 changes: 5 additions & 5 deletions src/Transducer/Reduce.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

namespace LazyLists\Transducer;

use LazyLists\LazyIterator;
use LazyLists\LazyWorker;

/**
*
*/
class Reduce implements TransducerInterface
{
protected $iterator;
protected $worker;
protected $accumulator;
protected $initialReduction;
protected $reduction;
Expand All @@ -40,9 +40,9 @@ public function __construct(
}

public function initialize(
LazyIterator $iterator
LazyWorker $worker
) {
$this->iterator = $iterator;
$this->worker = $worker;
}

public function getEmptyFinalResult()
Expand All @@ -54,7 +54,7 @@ public function computeNextResult($item)
{
$accumulator = $this->accumulator;
$this->reduction = $accumulator($this->reduction, $item);
$this->iterator->yieldToNextTransducer($this->reduction);
$this->worker->yieldToNextTransducer($this->reduction);
}

public function computeFinalResult($previousResult, $lastValue)
Expand Down
12 changes: 6 additions & 6 deletions src/Transducer/Take.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

namespace LazyLists\Transducer;

use LazyLists\LazyIterator;
use LazyLists\LazyWorker;

/**
*
*/
class Take implements TransducerInterface
{
protected $iterator;
protected $worker;
protected $taken;
protected $numberOfItems;
public function __invoke($item)
Expand All @@ -36,10 +36,10 @@ public function __construct(
}

public function initialize(
LazyIterator $iterator
LazyWorker $worker
) {
$this->taken = [];
$this->iterator = $iterator;
$this->worker = $worker;
}

public function getEmptyFinalResult()
Expand All @@ -50,10 +50,10 @@ public function getEmptyFinalResult()
public function computeNextResult($item)
{
if (\count($this->taken) >= $this->numberOfItems - 1) {
$this->iterator->completeEarly();
$this->worker->completeEarly();
}
$this->taken[] = $item;
$this->iterator->yieldToNextTransducer($item);
$this->worker->yieldToNextTransducer($item);
}

public function computeFinalResult($previousResult, $lastValue)
Expand Down
17 changes: 7 additions & 10 deletions src/Transducer/TransducerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,27 @@

namespace LazyLists\Transducer;

use LazyLists\LazyIterator;
use LazyLists\LazyWorker;

/**
*
*/
interface TransducerInterface
{
/**
* This function should make use of:
* * $iterator->yieldToNextTransducer
* * $iterator->yieldToNextTransducerWithFutureValues
* * $iterator->skipToNextLoop
* * $iterator->completeEarly
* * $worker->yieldToNextTransducer
* * $worker->yieldToNextTransducerWithFutureValues
* * $worker->skipToNextLoop
* * $worker->completeEarly
*
* @param mixed $item
* @return void
*/
public function __invoke($item);

/**
* @param LazyIterator $iterator
* @param LazyWorker $worker
* @return void
*/
public function initialize(LazyIterator $iterator);
public function initialize(LazyWorker $worker);

public function computeNextResult($item);

Expand Down
6 changes: 3 additions & 3 deletions src/Transducer/Until.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public function computeNextResult($item)
{
$condition = $this->condition;
if ($condition($item)) {
$this->iterator->completeEarly();
$this->iterator->skipToNextLoop();
$this->worker->completeEarly();
$this->worker->skipToNextLoop();
} else {
$this->iterator->yieldToNextTransducer($item);
$this->worker->yieldToNextTransducer($item);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/pipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function pipe(...$transducers)
{
InvalidArgumentException::assertTransducers($transducers, __FUNCTION__);
return function ($subject) use ($transducers) {
$lazyIterator = new LazyIterator($subject, $transducers);
return $lazyIterator();
$LazyWorker = new LazyWorker($subject, $transducers);
return $LazyWorker();
};
}

0 comments on commit f829058

Please sign in to comment.