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

Refactor Scalar Operations #132

Merged
merged 16 commits into from
Jul 11, 2021
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ This library has been inspired by:
needed are standard PHP variables like `int`, `string`, `callable` or
`iterator`.
It allows users to use those operations individually, at their own will, to
build up something custom. Currently, more than [**80 operations**][28] are
build up something custom. Currently, more than [**100 operations**][28] are
Copy link
Collaborator Author

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 🎉

Copy link
Collaborator

Choose a reason for hiding this comment

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

Excellent :-)

available in this library. This library is an example of what you can do
with all those small bricks, but nothing prevents users from using an operation
on its own as well.
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Features
the arguments needed are standard PHP variables like ``int``,
``string``, ``callable`` or ``iterator``. It allows users to use
those operations individually, at their own will, to build up
something custom. Currently, more than :ref:`80 operations <api>`
something custom. Currently, more than :ref:`100 operations <api>`
are available in this library. This library is an example of what
you can do with all those small bricks, but nothing prevents users
from using an operation on its own as well.
Expand Down
38 changes: 33 additions & 5 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Create a collection from a callable.
$collection = Collection::fromCallable($callback);

fromFile
~~~~~~~~~~~~
~~~~~~~~

Create a collection from a file.

Expand Down Expand Up @@ -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``.
Copy link
Collaborator

@drupol drupol Jul 11, 2021

Choose a reason for hiding this comment

The 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:

  1. Operations which returns a boolean: contains, every, falsy, has... etc etc
  2. Operations which returns a Collection of Collection: span, partition
  3. Operation which returns an array: all
  4. Operation which returns a Collection: all the rest.

(be carefull Collection::key() might return anything, not only a scalar, but anything)

Also, what do you think about:

  • Updating the signatures of all the operations in the API page and add their return type ?
    or
  • Updating all operations and add a note about the return type ?

(about that page, it's very sad we cannot generate the documentation automatically from the classes themselves!)

Let me know what you think.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks all good now :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
~~~
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions docs/pages/code/operations/background.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]
2 changes: 1 addition & 1 deletion docs/pages/installation.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Installation
============

The easiest way to install it is through Composer_
The easiest way to install it is through Composer_:

.. code-block:: bash

Expand Down
2 changes: 1 addition & 1 deletion docs/pages/requirements.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Requirements
PHP
---

PHP greater than 7.4 is required, including 8.0
PHP greater than 7.4 is required, including 8.0.

Dependencies
------------
Expand Down