Skip to content

Commit

Permalink
docs: Add more examples snippets.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed May 24, 2022
1 parent cf7890a commit a1873a7
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 18 deletions.
22 changes: 22 additions & 0 deletions docs/pages/code/goldenNumber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

include __DIR__ . '/../../../vendor/autoload.php';

use loophp\collection\Collection;

// Golden ratio: https://en.wikipedia.org/wiki/Golden_ratio
$goldenNumberGenerator = static fn ($a = 0): array => [($a + 1) ** .5];

$goldenNumber = Collection::unfold($goldenNumberGenerator)
->limit(10)
->unwrap()
->last();

var_dump($goldenNumber->current()); // 1.6180165422314876
79 changes: 79 additions & 0 deletions docs/pages/code/ngrams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

include __DIR__ . '/../../../vendor/autoload.php';

use loophp\collection\Collection;

// NGram: https://en.wikipedia.org/wiki/N-gram
$input = 'Hello world';

$ngrams = Collection::fromString($input)
->window(2)
->drop(2);

print_r($ngrams->all());
/*
[
0 =>
[
0 => 'H',
1 => 'e',
2 => 'l',
],
1 =>
[
0 => 'e',
1 => 'l',
2 => 'l',
],
2 =>
[
0 => 'l',
1 => 'l',
2 => 'o',
],
3 =>
[
0 => 'l',
1 => 'o',
2 => ' ',
],
4 =>
[
0 => 'o',
1 => ' ',
2 => 'w',
],
5 =>
[
0 => ' ',
1 => 'w',
2 => 'o',
],
6 =>
[
0 => 'w',
1 => 'o',
2 => 'r',
],
7 =>
[
0 => 'o',
1 => 'r',
2 => 'l',
],
8 =>
[
0 => 'r',
1 => 'l',
2 => 'd',
],
];
*/
50 changes: 32 additions & 18 deletions docs/pages/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ Usage

Find here some working examples.

.. tip:: Read the section on :ref:`Working with keys and values` to better understand
the differences between working with Collection compared to normal PHP arrays.
.. tip:: Read the section on :ref:`Working with keys and values` to better
understand the differences between working with Collection compared to
normal PHP arrays.
The same principles apply to all :ref:`API methods <Methods (operations)>`,
giving you great power to manipulate various types of data if used correctly.
giving you great power to manipulate various types of data if used
correctly.

Simple
-------
Expand All @@ -29,8 +31,8 @@ be of any type!

.. code-block:: php
// This following example is perfectly valid, despite that having array for keys
// in a regular PHP arrays is impossible.
// This following example is perfectly valid, despite that having array for
// keys in a regular PHP arrays is impossible.
$input = static function () {
yield ['a'] => 'a';
yield ['b'] => 'b';
Expand All @@ -54,14 +56,14 @@ result. We can see that some data is missing, why?
->all(false); // [5 => 'e', 4 => 'd', 3 => 'c']
The reason that the frequency analysis for letters 'a' and 'b' is missing
is because when you call the method ``Collection::all()`` with a *false* parameter,
the collection converts the lazy collection into a regular PHP array,
and PHP doesn't allow having multiple time the same key; thus, it overrides
is because when you call the method ``Collection::all()`` with a *false*
parameter, the collection converts the lazy collection into a regular PHP array,
and PHP doesn't allow having multiple time the same key; thus, it overrides
the previous data and there will be missing information in the resulting array.

In order to prevent this, by default the ``all`` operation will also apply
``normalize``, re-indexing and re-ordering the keys. However, this might not always
be the desired outcome, like in this instance (see examples below).
``normalize``, re-indexing and re-ordering the keys. However, this might not
always be the desired outcome, like in this instance (see examples below).

Other ways to circumvent this PHP array limitation:

Expand All @@ -76,15 +78,15 @@ It's up to you to decide which approach to take based on your use case.
Serialization
~~~~~~~~~~~~~

The collection object implements the `JsonSerializable`_ interface, thus allowing
for JSON serialization using the built-in PHP function ``json_encode`` or a
custom serializer like the `Symfony Serializer`_.
The collection object implements the `JsonSerializable`_ interface, thus
allowing for JSON serialization using the built-in PHP function ``json_encode``
or a custom serializer like the `Symfony Serializer`_.

.. tip:: By default the collection is not normalized when serializing, which allows
its usage as an associative array. However, when it is used as a list and
there are missing keys, the ``normalize`` operation should be applied before
serialization; not doing so will likely not result in the desired outcome, as
shown in the example below.
.. tip:: By default the collection is not normalized when serializing, which
allows its usage as an associative array. However, when it is used as a
list and there are missing keys, the ``normalize`` operation should be
applied before serialization; not doing so will likely not result in the
desired outcome, as shown in the example below.

.. literalinclude:: code/serialization.php
:language: php
Expand Down Expand Up @@ -136,6 +138,12 @@ Approximate the number Pi
.. literalinclude:: code/monte-carlo.php
:language: php

Approximate the golden ratio
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. literalinclude:: code/goldenNumber.php
:language: php

Fibonacci sequence
~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -190,6 +198,12 @@ Lazy json parsing
.. literalinclude:: code/lazy-json-parsing.php
:language: php

Calcultate N-Grams
~~~~~~~~~~~~~~~~~~

.. literalinclude:: code/ngrams.php
:language: php

.. _article: https://not-a-number.io/2019/php-composition-and-inheritance/
.. _JsonSerializable: https://www.php.net/manual/en/class.jsonserializable.php
.. _Symfony Serializer: https://symfony.com/doc/current/components/serializer.html

0 comments on commit a1873a7

Please sign in to comment.