Skip to content

Commit

Permalink
Update documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jan 6, 2020
1 parent 675f4ab commit ff360a6
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ This library is framework agnostic and can be integrated in any PHP project, in
Requirements <pages/requirements>
Installation <pages/installation>
Usage <pages/usage>
Examples <pages/examples>
API <pages/api>
Tests <pages/tests>
Contributing <pages/contributing>
Development <pages/development>
2 changes: 1 addition & 1 deletion docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ walk
zip
---

.. _Appendable: https://github.com/drupol/collection/blob/master/src/Contract/Appendable.php
.. _Appendable: https://github.com/loophp/collection/blob/master/src/Contract/Appendable.php
4 changes: 0 additions & 4 deletions docs/pages/development.rst

This file was deleted.

258 changes: 258 additions & 0 deletions docs/pages/examples.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
Examples
========

Approximate the number e
------------------------

.. code-block:: bash
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use loophp\collection\Collection;
$multiplication = static function ($value1, $value2) {
return $value1 * $value2;
};
$addition = static function ($value1, $value2) {
return $value1 + $value2;
};
$fact = static function (int $number) use ($multiplication) {
return Collection::range(1, $number + 1)
->reduce(
$multiplication,
1
);
};
$e = static function (int $value) use ($fact): float {
return $value / $fact($value);
};
$number_e_approximation = Collection::times(INF, $e)
->until(static function (float $value): bool {return $value < 10 ** -12;})
->reduce($addition);
var_dump($number_e_approximation); // 2.718281828459
Approximate the number Pi
-------------------------

.. code-block:: php
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use loophp\collection\Collection;
$iterations = 100000;
$monteCarloMethod = static function ($in = 0) {
$randomNumber1 = mt_rand() / mt_getrandmax();
$randomNumber2 = mt_rand() / mt_getrandmax();
if (1 >= (($randomNumber1 ** 2) + ($randomNumber2 ** 2))) {
++$in;
}
return $in;
};
$pi_approximation = Collection::iterate($monteCarloMethod)
->limit($iterations)
->tail()
->map(
static function ($value) use ($iterations) {
return 4 * $value / $iterations;
}
)
->first();
var_dump($pi_approximation); // 3.1416764444444
Find Prime numbers
------------------

.. code-block:: php
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use loophp\collection\Collection;
use function in_array;
use const INF;
/**
* Get the divisor of a given number.
*
* @param float $num
* The number.
* @param int $start
* The start.
*
* @return \Traversable
* The divisors of the number.
*/
function factors(float $num, int $start = 1): Traversable
{
if (0 === $num % $start) {
yield $start => $start;
yield $num / $start => $num / $start;
}
if (ceil(sqrt($num)) >= $start) {
yield from factors($num, $start + 1);
}
}
/**
* Check if a number is a multiple of 2.
*
* @param $value
* The number.
*
* @return bool
* Whether or not the number is a multiple of 2.
*/
$notMultipleOf2 = static function ($value): bool {
return 0 !== $value % 2;
};
/**
* Check if a number is a multiple of 3.
*
* @param $value
* The number.
*
* @return bool
* Whether or not the number is a multiple of 3.
*/
$notMultipleOf3 = static function ($value): bool {
$sumIntegers = static function ($value): float {
return array_reduce(
mb_str_split((string) $value),
static function ($carry, $value) {
return $value + $carry;
},
0
);
};
$sum = $sumIntegers($value);
while (10 < $sum) {
$sum = $sumIntegers($sum);
}
return 0 !== $sum % 3;
};
/**
* Check if a number is a multiple of 5.
*
* @param $value
* The number.
*
* @return bool
* Whether or not the number is a multiple of 5.
*/
$notMultipleOf5 = static function ($value): bool {
return !in_array(mb_substr((string) $value, -1), ['0', '5'], true);
};
/**
* Check if a number is a multiple of 7.
*
* @param $value
* The number.
*
* @return bool
* Whether or not the number is a multiple of 7.
*/
$notMultipleOf7 = static function ($value): bool {
$number = $value;
while (14 <= $number) {
$lastDigit = mb_substr((string) $number, -1);
if ('0' === $lastDigit) {
return true;
}
$number = (int) abs((int) mb_substr((string) $number, 0, -1) - 2 * (int) $lastDigit);
}
return !(0 === $number || 7 === $number);
};
/**
* Check if a number is a multiple of 11.
*
* @param $value
* The number.
*
* @return bool
* Whether or not the number is a multiple of 11.
*/
$notMultipleOf11 = static function ($value): bool {
$number = $value;
while (11 < $number) {
$lastDigit = mb_substr((string) $number, -1);
if ('0' === $lastDigit) {
return true;
}
$number = (int) abs((int) mb_substr((string) $number, 0, -1) - (int) $lastDigit);
}
return !(0 === $number || 11 === $number);
};
/**
* Check if a number have more than 2 divisors.
*
* @param $value
* The number.
*
* @return bool
* Whether or not the number has more than 2 divisors.
*/
$valueHavingMoreThan2Divisors = static function ($value): bool {
$i = 0;
foreach (factors($value) as $factor) {
if (2 < $i++) {
return false;
}
}
return true;
};
$primes = Collection::range(9, INF, 2) // Count from 10 to infinity
->filter($notMultipleOf2) // Filter out multiples of 2
->filter($notMultipleOf3) // Filter out multiples of 3
->filter($notMultipleOf5) // Filter out multiples of 5
->filter($notMultipleOf7) // Filter out multiples of 7
->filter($notMultipleOf11) // Filter out multiples of 11
->filter($valueHavingMoreThan2Divisors) // Filter out remaining values having more than 2 divisors.
->prepend(2, 3, 5, 7) // Add back digits that were removed
->normalize() // Re-index the keys
->limit(100); // Take the 100 first prime numbers.
print_r($primes->all());
2 changes: 1 addition & 1 deletion docs/pages/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ The easiest way to install it is through Composer_

.. code-block:: bash
composer require drupol/collection
composer require loophp/collection
.. _Composer: https://getcomposer.org
2 changes: 1 addition & 1 deletion docs/pages/tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ will check your code
.. _PSR-12: https://www.php-fig.org/psr/psr-12/
.. _drupol/php-conventions: https://github.com/drupol/php-conventions
.. _Github Actions: https://github.com/drupol/collection/actions
.. _Github Actions: https://github.com/loophp/collection/actions
.. _PHPSpec: http://www.phpspec.net/
.. _PHPInfection: https://github.com/infection/infection
.. _Grumphp: https://github.com/phpro/grumphp
4 changes: 2 additions & 2 deletions docs/pages/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Usage
include 'vendor/autoload.php';
use drupol\collection\Collection;
use loophp\collection\Collection;
// More examples...
$collection = Collection::with(['A', 'B', 'C', 'D', 'E']);
Expand Down Expand Up @@ -85,7 +85,7 @@ Usage
// See: https://www.php.net/manual/en/function.array-flip.php
// Example:
// $dedupArray = array_flip(array_flip(['a', 'b', 'c', 'd', 'a'])); // ['a', 'b', 'c', 'd']
// However, in drupol/collection it doesn't behave as such.
// However, in loophp/collection it doesn't behave as such.
// As this library is based on PHP Generators, it's able to return multiple times the same key when iterating.
// You end up with the following result when issuing twice the ::flip() operation.
Collection::with(['a', 'b', 'c', 'd', 'a'])
Expand Down

0 comments on commit ff360a6

Please sign in to comment.