Skip to content

Commit

Permalink
Add static method ::iterate().
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 1, 2019
1 parent 55a0e3d commit 3306602
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,36 @@ Collection::with(
->limit(10)
->all(); // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

// or

Collection::iterate(
static function($v) {
return [$v[1], $v[0] + $v[1]];
},
[1,1]
)
->limit(10)
->all(); // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

// Find the golden ratio with Fibonacci
$result = Collection::iterate(
static function($v) {
return [$v[1], $v[0] + $v[1]];
},
[1,1]
)
->map(static function($item) {return $item[0];})
->chunk(2)
->map(
static function(ArrayIterator $item) {
[$first, $second] = $item;

return $second / $first;
}
)
->limit(100)
->last(); // 1.6180339887499

// Use an existing Generator as input data.
$readFileLineByLine = static function (string $filepath): \Generator {
$fh = fopen($filepath, 'rb');
Expand Down Expand Up @@ -308,6 +338,7 @@ about the kind of parameters they require.
| Methods | Return type | Source |
| ------------- | --------------------- | -------------- |
| `empty` | new Collection object | [Collection.php](./src/Collection.php)
| `iterate` | new Collection object | [Collection.php](./src/Collection.php)
| `range` | new Collection object | [Collection.php](./src/Collection.php)
| `times` | new Collection object | [Collection.php](./src/Collection.php)
| `with` | new Collection object | [Collection.php](./src/Collection.php)
Expand Down
15 changes: 15 additions & 0 deletions spec/drupol/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,21 @@ public function it_can_intersperse(): void
->during('intersperse', ['foo', 1, -1]);
}

public function it_can_iterate(): void
{
$this
->beConstructedThrough('iterate', [static function ($item) {
return [$item[1], $item[0] + $item[1]];
}, [0, 1]]);

$this
->map(static function ($item) {
return $item[0];
})
->limit(10)
->shouldIterateAs([0, 1, 1, 2, 3, 5, 8, 13, 21, 34]);
}

public function it_can_keys(): void
{
$this
Expand Down
20 changes: 20 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,26 @@ public function intersperse($element, int $every = 1, int $startAt = 0): BaseInt
return new Collection($this->run(new Intersperse($element, $every, $startAt)));
}

/**
* {@inheritdoc}
*/
public static function iterate(callable $callback, $initial = null): CollectionInterface
{
return new Collection(
static function () use ($initial, $callback) {
$result = $initial;

yield $initial;

while (true) {
$result = $callback($result);

yield $result;
}
}
);
}

/**
* {@inheritdoc}
*/
Expand Down
16 changes: 16 additions & 0 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ interface Collection extends
*/
public static function empty(): Collection;

/**
* @param callable $callback
* @param mixed $initial
*
* @return \drupol\collection\Contract\Collection
*/
public static function iterate(callable $callback, $initial = null): Collection;

/**
* Create a new with a range of number.
*
Expand All @@ -69,4 +77,12 @@ public static function range(int $start = 0, $end = \INF, $step = 1): Collection
* @return \drupol\collection\Contract\Collection
*/
public static function times($number, callable $callback = null): Collection;

/**
* @param callable $callback
* @param mixed $initial
*
* @return \drupol\collection\Contract\Collection
*/
public static function iterate(callable $callback, $initial = null): Collection;
}

0 comments on commit 3306602

Please sign in to comment.