-
-
Notifications
You must be signed in to change notification settings - Fork 35
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
Refactor Scalar Operations #132
Changes from 1 commit
771995c
aa606f9
1be0bdb
1500cb7
630e1de
f59fe84
430b49b
bb88e84
84ca9f9
0382caa
17d05a5
7e6d63c
3115e9a
7477f13
080744d
3f45162
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ Create a collection from a callable. | |
$collection = Collection::fromCallable($callback); | ||
|
||
fromFile | ||
~~~~~~~~~~~~ | ||
~~~~~~~~ | ||
|
||
Create a collection from a file. | ||
|
||
|
@@ -145,7 +145,32 @@ Another example | |
Methods (operations) | ||
-------------------- | ||
|
||
.. note:: Operations always returns a new collection object, with the exception of ``all``, ``count``, ``current``, ``key``. | ||
Background | ||
~~~~~~~~~~ | ||
|
||
Operations are pure functions which can be used to manipulate an iterator, either directly | ||
or through the ``Collection`` object. | ||
|
||
.. literalinclude:: code/operations/background.php | ||
:language: php | ||
|
||
When used separately, operations typically return a PHP `Generator`_ or an `Iterator`_. | ||
When used as a ``Collection`` method, operations fall into three main categories based on the return type: | ||
|
||
1. Operations that return a ``scalar`` value. Currently, this includes: | ||
``Contains``, ``Every``, ``Falsy``, ``Has``, ``Key``, ``Match`` (or ``MatchOne``), ``Nullsy``, ``Truthy``. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the explanation is overall really great and I wouldn't change a thing, except for the classification points here. What I propose is to list the operation types as such:
(be carefull Collection::key() might return anything, not only a scalar, but anything) Also, what do you think about:
(about that page, it's very sad we cannot generate the documentation automatically from the classes themselves!) Let me know what you think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @drupol thank you for the feedback, I've done some changes to the numbered list to incorporate your suggestions, with a couple tweaks. Let me know what you think of this new classification :). Regarding updating the signatures of the operations in the docs -> it's something that I thought about today as well 😄 . I'll do it in a future PR for all of them There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks all good now :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @drupol great, this is all ready on my side 😁 |
||
|
||
2. Operations that return a new ``Collection`` object - this includes the majority of operations | ||
|
||
3. Operations that return a ``Collection`` of ``Collection`` objects. Currently, this includes: ``Partition``, ``Span``. | ||
|
||
A couple other operations do not fall neatly in any of the above categories: ``all``, ``current``, ``get``. | ||
These methods will retrieve items of the collection/iterable directly. | ||
|
||
.. note:: Earlier versions of the package had most operations returning a new ``Collection`` object. | ||
This was changed based on convenience and ease of use; typical usage of operations which return `scalar` values | ||
would involve immediately retrieving the value inside, whereas for most other operations further transformations | ||
are likely to be applied. | ||
|
||
all | ||
~~~ | ||
|
@@ -2342,7 +2367,6 @@ Signature: ``Collection::zip(iterable ...$iterables);`` | |
->limit(100) | ||
->unwrap(); // [0, 1, 2, 3 ... 196, 197, 198, 199] | ||
|
||
.. _Doctrine Collections: https://github.com/doctrine/collections | ||
.. _Allable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Allable.php | ||
.. _Appendable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Appendable.php | ||
.. _Applyable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Applyable.php | ||
|
@@ -2357,9 +2381,7 @@ Signature: ``Collection::zip(iterable ...$iterables);`` | |
.. _Compactable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Compactable.php | ||
.. _Coalesceable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Coalesceable.php | ||
.. _Containsable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Containsable.php | ||
.. _Countable: https://www.php.net/manual/en/class.countable.php | ||
.. _Currentable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Currentable.php | ||
.. _Criteria: https://www.doctrine-project.org/projects/doctrine-collections/en/1.6/index.html#matching | ||
.. _Cycleable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Cycleable.php | ||
.. _Diffable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Diffable.php | ||
.. _Diffkeysable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Diffkeysable.php | ||
|
@@ -2453,7 +2475,13 @@ Signature: ``Collection::zip(iterable ...$iterables);`` | |
.. _Wordsable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Wordsable.php | ||
.. _Wrapable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Wrapable.php | ||
.. _Zipable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Zipable.php | ||
|
||
.. _array_flip(): https://php.net/array_flip | ||
.. _Countable: https://www.php.net/manual/en/class.countable.php | ||
.. _Criteria: https://www.doctrine-project.org/projects/doctrine-collections/en/1.6/index.html#matching | ||
.. _Doctrine Collections: https://github.com/doctrine/collections | ||
.. _Generator: https://www.php.net/manual/en/language.generators.overview.php | ||
.. _Iterator: https://www.php.net/manual/en/class.iterator.php | ||
.. _symfony/var-dumper: https://packagist.org/packages/symfony/var-dumper | ||
.. _Traversable: https://www.php.net/manual/en/class.traversable.php | ||
.. _TypedIterator: https://github.com/loophp/collection/blob/master/src/Iterator/TypedIterator.php | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/** | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App; | ||
|
||
use ArrayIterator; | ||
use loophp\collection\Collection; | ||
use loophp\collection\Operation\Filter; | ||
|
||
include __DIR__ . '/../../../../vendor/autoload.php'; | ||
|
||
$input = [1, 2, 3, 4]; | ||
$even = static fn (int $value): bool => $value % 2 === 0; | ||
|
||
// Standalone usage | ||
$filtered = Filter::of()($even)(new ArrayIterator($input)); | ||
|
||
print_r(iterator_to_array($filtered)); // [2, 4] | ||
|
||
// Usage via Collection object | ||
$filtered = Collection::fromIterable($input)->filter($even); | ||
|
||
print_r($filtered->all()); // [2, 4] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did a new count and this is up over 100 now 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent :-)