Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show partition failure #147

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use loophp\collection\Contract\Operation;
use loophp\collection\Iterator\ClosureIterator;
use loophp\collection\Operation\AbstractOperation;
use loophp\collection\Operation\Pipe;
use OutOfBoundsException;
use PhpSpec\Exception\Example\FailureException;
use PhpSpec\Exception\Example\MatcherException;
Expand Down Expand Up @@ -60,6 +61,22 @@ public function it_can_all(): void
->shouldIterateAs(['a' => 3, 'b' => 2]);
}

public function it_fails_to_work_with_generator(): void
{
$gen = static fn (): Generator => yield from [1, 2, 3];

$this->beConstructedThrough('fromIterable', [$gen()]);
$this->shouldHaveCount(3);
$this->shouldIterateAs([1, 2, 3]);
}
Comment on lines +64 to +71
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like the issue is here, when we instantiate the collection from a generator, which is what also happens in partition


public function it_works_with_iterator(): void
{
$this->beConstructedThrough('fromIterable', [new ArrayIterator([1, 2, 3])]);
$this->shouldHaveCount(3);
$this->shouldIterateAs([1, 2, 3]);
}

public function it_can_append(): void
{
$generator = static function (): Generator {
Expand Down Expand Up @@ -2264,7 +2281,7 @@ public function it_can_partition(): void

$subject = $this::fromIterable($input)->partition($isGreaterThan(5));
$subject->shouldHaveCount(2);
$subject->first()->shouldBeAnInstanceOf(CollectionInterface::class);
$subject->first()->current()->shouldBeAnInstanceOf(CollectionInterface::class);
$subject->last()->shouldBeAnInstanceOf(CollectionInterface::class);

$this::fromIterable($input)
Expand Down Expand Up @@ -3868,6 +3885,31 @@ public function it_is_initializable(): void
$this->shouldHaveType(Collection::class);
}

public function it_reproduces_partition_issue(): void
{
$collection = $this::fromIterable([1, 2, 3, 4, 5]);

[$even, $odd] = $collection->partition(static fn (int $value): bool => $value % 2 === 0)->all();

$even->shouldHaveCount(2);
$even->shouldIterateAs([1 => 2, 3 => 4]);
}

public function it_reproduces_partition_issue_using_first_last(): void
{
$collection = $this::fromIterable([1, 2, 3, 4, 5]);

$partitioned = $collection->partition(static fn (int $value): bool => $value % 2 === 0);
$even = $partitioned->first()->current();

$even->shouldHaveCount(2);
$even->shouldIterateAs([1 => 2, 3 => 4]);

$odd = $partitioned->last()->current();
$odd->shouldHaveCount(3);
$odd->shouldIterateAs([0 => 1, 2 => 3, 4 => 5]);
}

public function let(): void
{
$this->beConstructedThrough('empty');
Expand Down
3 changes: 2 additions & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Closure;
use Doctrine\Common\Collections\Criteria;
use Generator;
use Iterator;
use loophp\collection\Contract\Collection as CollectionInterface;
use loophp\collection\Contract\Operation;
Expand Down Expand Up @@ -670,7 +671,7 @@ public function partition(callable ...$callbacks): CollectionInterface
return new self(
Pipe::of()(
Partition::of()(...$callbacks),
Map::of()(static fn (Iterator $iterator): CollectionInterface => self::fromIterable($iterator))
Map::of()(static fn (Generator $generator): CollectionInterface => self::fromIterable(iterator_to_array($generator)))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with this refactor it works looks like

),
$this->getIterator()
);
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Partition.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __invoke(): Closure
*
* @return Generator<int, Iterator<TKey, T>>
*/
static function (Iterator $iterator) use ($callbacks): Iterator {
static function (Iterator $iterator) use ($callbacks): Generator {
/** @var Iterator<TKey, T> $filter */
$filter = Filter::of()(...$callbacks)($iterator);
/** @var Iterator<TKey, T> $reject */
Expand Down