Skip to content

Commit

Permalink
Update the ::iterate() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 2, 2019
1 parent 95dbce9 commit e677738
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Collection::iterate(
static function($v) {
return [$v[1], $v[0] + $v[1]];
},
[1,1]
1,1
)
->limit(10)
->all(); // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Expand All @@ -146,7 +146,7 @@ $result = Collection::iterate(
static function($v) {
return [$v[1], $v[0] + $v[1]];
},
[1,1]
1,1
)
->map(static function($item) {return $item[0];})
->chunk(2)
Expand Down
2 changes: 1 addition & 1 deletion spec/drupol/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ public function it_can_iterate(): void
$this
->beConstructedThrough('iterate', [static function ($item) {
return [$item[1], $item[0] + $item[1]];
}, [0, 1]]);
}, 0, 1]);

$this
->map(static function ($item) {
Expand Down
12 changes: 4 additions & 8 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,14 @@ public function intersperse($element, int $every = 1, int $startAt = 0): BaseInt
/**
* {@inheritdoc}
*/
public static function iterate(callable $callback, $initial = null): CollectionInterface
public static function iterate(callable $callback, ...$parameters): CollectionInterface
{
return new Collection(
static function () use ($initial, $callback) {
$result = $initial;

yield $initial;

static function () use ($parameters, $callback) {
while (true) {
$result = $callback($result);
yield $parameters;

yield $result;
$parameters = $callback($parameters);
}
}
);
Expand Down
4 changes: 2 additions & 2 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public static function empty(): Collection;

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

/**
* Create a new with a range of number.
Expand Down

0 comments on commit e677738

Please sign in to comment.