From e5a13d20dfa3ca0c38c04a0f4c5e3ceef923e2f2 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Thu, 26 Aug 2021 10:45:55 +0200 Subject: [PATCH 1/7] fix: Update `Initsable` interface. --- src/Contract/Operation/Initsable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Contract/Operation/Initsable.php b/src/Contract/Operation/Initsable.php index fc36b05b2..4e08253a0 100644 --- a/src/Contract/Operation/Initsable.php +++ b/src/Contract/Operation/Initsable.php @@ -22,7 +22,7 @@ interface Initsable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#inits * - * @return Collection + * @return Collection> */ public function inits(): Collection; } From 84bb811f82433ee8a2883f747015fc14ca7d51e4 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Thu, 26 Aug 2021 10:46:12 +0200 Subject: [PATCH 2/7] fix: Update `Inits` operation. --- src/Operation/Inits.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Operation/Inits.php b/src/Operation/Inits.php index b46c10b46..0f0d0fe6f 100644 --- a/src/Operation/Inits.php +++ b/src/Operation/Inits.php @@ -24,26 +24,26 @@ final class Inits extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator> + * @return Closure(Iterator): Generator> */ public function __invoke(): Closure { $scanLeftCallback = /** - * @param array $carry - * @param T $value - * @param TKey $key + * @param list $carry + * @param array{0: TKey, 1: T} $value * - * @return array + * @return list */ - static function (array $carry, $value, $key): array { - $carry[$key] = $value; + static function (array $carry, array $value): array { + $carry[] = $value; return $carry; }; - /** @var Closure(Iterator): Generator> $inits */ + /** @var Closure(Iterator): Generator> $inits */ $inits = Pipe::of()( + Pack::of(), ScanLeft::of()($scanLeftCallback)([]), Normalize::of() ); From 8d808c56f3f5e8890c4cb1382eef5cf615a5f423 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Thu, 26 Aug 2021 10:46:22 +0200 Subject: [PATCH 3/7] tests: Add more tests. --- spec/loophp/collection/CollectionSpec.php | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/spec/loophp/collection/CollectionSpec.php b/spec/loophp/collection/CollectionSpec.php index a288190d4..5aeffa3e8 100644 --- a/spec/loophp/collection/CollectionSpec.php +++ b/spec/loophp/collection/CollectionSpec.php @@ -1965,9 +1965,26 @@ public function it_can_inits(): void ->inits() ->shouldIterateAs([ [], - ['a'], - ['a', 'b'], - ['a', 'b', 'c'], + [[0, 'a']], + [[0, 'a'], [1, 'b']], + [[0, 'a'], [1, 'b'], [2, 'c']], + ]); + + $gen = static function (): Generator { + yield true => 'true'; + + yield false => 'false'; + + yield [] => 'array'; + }; + + $this::fromIterable($gen()) + ->inits() + ->shouldIterateAs([ + [], + [[true, 'true']], + [[true, 'true'], [false, 'false']], + [[true, 'true'], [false, 'false'], [[], 'array']], ]); } From 84c7d2a31a749486ed03968176ad8ba4431eb40a Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Thu, 26 Aug 2021 10:46:36 +0200 Subject: [PATCH 4/7] tests: Add SA tests. --- tests/static-analysis/inits.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 tests/static-analysis/inits.php diff --git a/tests/static-analysis/inits.php b/tests/static-analysis/inits.php new file mode 100644 index 000000000..c976f8668 --- /dev/null +++ b/tests/static-analysis/inits.php @@ -0,0 +1,33 @@ +> $collection + */ +function inits_checkListString(CollectionInterface $collection): void +{ +} + +/** + * @param CollectionInterface> $collection + */ +function inits_checkMapString(CollectionInterface $collection): void +{ +} + +$listString = range('a', 'c'); +$mapString = array_combine(range('a', 'c'), range('a', 'c')); + +inits_checkListString(Collection::fromIterable($listString)->inits()); +inits_checkMapString(Collection::fromIterable($mapString)->inits()); From 5fd011131a64575b312f4762c6860e8e886102cd Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Thu, 26 Aug 2021 10:54:10 +0200 Subject: [PATCH 5/7] docs: Update `Inits` operation documentation. --- docs/pages/api.rst | 6 ++---- docs/pages/code/operations/inits.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 docs/pages/code/operations/inits.php diff --git a/docs/pages/api.rst b/docs/pages/api.rst index 471a9968b..dfd43f05a 100644 --- a/docs/pages/api.rst +++ b/docs/pages/api.rst @@ -1233,10 +1233,8 @@ Interface: `Initsable`_ Signature: ``Collection::inits(): Collection;`` -.. code-block:: php - - Collection::fromIterable(range('a', 'c')) - ->inits(); // [[], ['a'], ['a', 'b'], ['a', 'b', 'c']] +.. literalinclude:: code/operations/inits.php + :language: php intersect ~~~~~~~~~ diff --git a/docs/pages/code/operations/inits.php b/docs/pages/code/operations/inits.php new file mode 100644 index 000000000..b74ecd6ab --- /dev/null +++ b/docs/pages/code/operations/inits.php @@ -0,0 +1,17 @@ +inits(); // [[], [[0, 'a']], [[0, 'a'], [1, 'b']], [[0, 'a'], [1, 'b'], [2, 'c']]] From 84b220d03fafd67aaf2fe63e750ee70141ea2f59 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sun, 29 Aug 2021 11:14:38 +0200 Subject: [PATCH 6/7] docs: Add more examples. --- docs/pages/code/operations/inits.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/pages/code/operations/inits.php b/docs/pages/code/operations/inits.php index b74ecd6ab..a541fa2ea 100644 --- a/docs/pages/code/operations/inits.php +++ b/docs/pages/code/operations/inits.php @@ -13,5 +13,16 @@ include __DIR__ . '/../../../../vendor/autoload.php'; +$input = range('a', 'c'); + Collection::fromIterable(range('a', 'c')) ->inits(); // [[], [[0, 'a']], [[0, 'a'], [1, 'b']], [[0, 'a'], [1, 'b'], [2, 'c']]] + +Collection::fromIterable(array_combine(range('A', 'C'), $input)) + ->inits(); // [[], [['A', 'a']], [['A', 'a'], ['B', 'b']], [['A', 'a'], ['B', 'b'], ['C', 'c']]] + +// To get only the values: +Collection::fromIterable($input) + ->inits() + ->map(static fn (array $data): array => array_column($data, 1)); + // [[], ['a'], ['a', 'b'], ['a', 'b', 'c']] From 16b62772152d463db6f91aacb135e9822e163706 Mon Sep 17 00:00:00 2001 From: AlexandruGG Date: Sat, 11 Sep 2021 11:45:12 +0100 Subject: [PATCH 7/7] Add `pluck` example --- docs/pages/code/operations/inits.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/pages/code/operations/inits.php b/docs/pages/code/operations/inits.php index a541fa2ea..c92bd9e72 100644 --- a/docs/pages/code/operations/inits.php +++ b/docs/pages/code/operations/inits.php @@ -22,7 +22,15 @@ ->inits(); // [[], [['A', 'a']], [['A', 'a'], ['B', 'b']], [['A', 'a'], ['B', 'b'], ['C', 'c']]] // To get only the values: + +// Using `map` + `array_column` Collection::fromIterable($input) ->inits() ->map(static fn (array $data): array => array_column($data, 1)); // [[], ['a'], ['a', 'b'], ['a', 'b', 'c']] + +// Using the `pluck` operation +$var = Collection::fromIterable($input) + ->inits() + ->pluck('*.1'); + // [[], ['a'], ['a', 'b'], ['a', 'b', 'c']]