Skip to content

Commit

Permalink
refactor: Update Fold* operations.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: yes
  • Loading branch information
drupol committed Oct 7, 2020
1 parent ce14a0d commit f6828f0
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 24 deletions.
16 changes: 8 additions & 8 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ static function (string $carry, string $item): string {
},
''
)
->shouldReturn('ABC');
->shouldIterateAs([2 => 'ABC']);
}

public function it_can_fold_from_the_right(): void
Expand All @@ -816,7 +816,7 @@ static function (string $carry, string $item): string {
},
''
)
->shouldReturn('CBA');
->shouldIterateAs([0 => 'CBA']);
}

public function it_can_foldleft1(): void
Expand All @@ -827,11 +827,11 @@ public function it_can_foldleft1(): void

$this::fromIterable([64, 4, 2, 8])
->foldLeft1($callback)
->shouldReturn(1);
->shouldIterateAs([3 => 1]);

$this::fromIterable([12])
->foldLeft1($callback)
->shouldReturn(12);
->shouldIterateAs([0 => 12]);
}

public function it_can_foldright1(): void
Expand All @@ -842,11 +842,11 @@ public function it_can_foldright1(): void

$this::fromIterable([8, 12, 24, 4])
->foldRight1($callback)
->shouldReturn(4);
->shouldIterateAs([0 => 4]);

$this::fromIterable([12])
->foldRight1($callback)
->shouldReturn(12);
->shouldIterateAs([0 => 12]);
}

public function it_can_forget(): void
Expand Down Expand Up @@ -1744,7 +1744,7 @@ public function __invoke(): Closure
}
};

$map = new class() extends AbstractOperation implements Operation {
$castToInt = new class() extends AbstractOperation implements Operation {
public function __invoke(): Closure
{
return static function ($collection) {
Expand All @@ -1756,7 +1756,7 @@ public function __invoke(): Closure
};

$this::fromIterable(range(1, 5))
->run($square(), $sqrt(), $map())
->run($square(), $sqrt(), $castToInt())
->shouldIterateAs(range(1, 5));
}

Expand Down
16 changes: 8 additions & 8 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,24 +409,24 @@ public function flip(): CollectionInterface
return $this->run(Flip::of());
}

public function foldLeft(callable $callback, $initial = null)
public function foldLeft(callable $callback, $initial = null): CollectionInterface
{
return $this->run(FoldLeft::of()($callback)($initial))->getIterator()->current();
return $this->run(FoldLeft::of()($callback)($initial));
}

public function foldLeft1(callable $callback)
public function foldLeft1(callable $callback): CollectionInterface
{
return $this->run(FoldLeft1::of()($callback))->getIterator()->current();
return $this->run(FoldLeft1::of()($callback));
}

public function foldRight(callable $callback, $initial = null)
public function foldRight(callable $callback, $initial = null): CollectionInterface
{
return $this->run(Foldright::of()($callback)($initial))->getIterator()->current();
return $this->run(Foldright::of()($callback)($initial));
}

public function foldRight1(callable $callback)
public function foldRight1(callable $callback): CollectionInterface
{
return $this->run(FoldRight1::of()($callback))->getIterator()->current();
return $this->run(FoldRight1::of()($callback));
}

public function forget(...$keys): CollectionInterface
Expand Down
9 changes: 7 additions & 2 deletions src/Contract/Operation/FoldLeft1able.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace loophp\collection\Contract\Operation;

use Iterator;
use loophp\collection\Contract\Collection;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand All @@ -12,8 +15,10 @@
interface FoldLeft1able
{
/**
* @psalm-param callable(T, T, TKey, Iterator<TKey, T>): T $callback
*
* @return mixed
* @psalm-return T|null
* @psalm-return \loophp\collection\Contract\Collection<TKey, T|null>
*/
public function foldLeft1(callable $callback);
public function foldLeft1(callable $callback): Collection;
}
9 changes: 7 additions & 2 deletions src/Contract/Operation/FoldLeftable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace loophp\collection\Contract\Operation;

use Iterator;
use loophp\collection\Contract\Collection;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand All @@ -14,11 +17,13 @@ interface FoldLeftable
/**
* Fold the collection from the left to the right.
*
* @psalm-param callable(T, T, TKey, Iterator<TKey, T>): T $callback
*
* @param mixed $initial
* @psalm-param T|null $initial
*
* @return mixed
* @psalm-return T|null
* @psalm-return \loophp\collection\Contract\Collection<TKey, T|null>
*/
public function foldLeft(callable $callback, $initial = null);
public function foldLeft(callable $callback, $initial = null): Collection;
}
9 changes: 7 additions & 2 deletions src/Contract/Operation/FoldRight1able.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace loophp\collection\Contract\Operation;

use Iterator;
use loophp\collection\Contract\Collection;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand All @@ -12,8 +15,10 @@
interface FoldRight1able
{
/**
* @psalm-param callable(T, T, TKey, Iterator<TKey, T>): T $callback
*
* @return mixed
* @psalm-return T|null
* @psalm-return \loophp\collection\Contract\Collection<TKey, T|null>
*/
public function foldRight1(callable $callback);
public function foldRight1(callable $callback): Collection;
}
9 changes: 7 additions & 2 deletions src/Contract/Operation/FoldRightable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace loophp\collection\Contract\Operation;

use Iterator;
use loophp\collection\Contract\Collection;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand All @@ -14,11 +17,13 @@ interface FoldRightable
/**
* Fold the collection from the right to the left.
*
* @psalm-param callable(T, T, TKey, Iterator<TKey, T>): T $callback
*
* @param mixed $initial
* @psalm-param T|null $initial
*
* @return mixed
* @psalm-return T|null
* @psalm-return \loophp\collection\Contract\Collection<TKey, T|null>
*/
public function foldRight(callable $callback, $initial = null);
public function foldRight(callable $callback, $initial = null): Collection;
}

0 comments on commit f6828f0

Please sign in to comment.