diff --git a/docs/index.rst b/docs/index.rst index 1605e8514..d45774768 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -57,7 +57,7 @@ This library is framework agnostic and can be integrated in any PHP project, in Requirements Installation Usage + Examples API Tests Contributing - Development diff --git a/docs/pages/api.rst b/docs/pages/api.rst index 802ec506e..5af51d79c 100644 --- a/docs/pages/api.rst +++ b/docs/pages/api.rst @@ -124,4 +124,4 @@ walk zip --- -.. _Appendable: https://github.com/drupol/collection/blob/master/src/Contract/Appendable.php \ No newline at end of file +.. _Appendable: https://github.com/loophp/collection/blob/master/src/Contract/Appendable.php \ No newline at end of file diff --git a/docs/pages/development.rst b/docs/pages/development.rst deleted file mode 100644 index ba67c43f5..000000000 --- a/docs/pages/development.rst +++ /dev/null @@ -1,4 +0,0 @@ -.. _development: - -Development -=========== diff --git a/docs/pages/examples.rst b/docs/pages/examples.rst new file mode 100644 index 000000000..ce760cae8 --- /dev/null +++ b/docs/pages/examples.rst @@ -0,0 +1,258 @@ +Examples +======== + +Approximate the number e +------------------------ + +.. code-block:: bash + + 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 + + = (($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 + + $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()); diff --git a/docs/pages/installation.rst b/docs/pages/installation.rst index f90b15924..8713cc1e5 100644 --- a/docs/pages/installation.rst +++ b/docs/pages/installation.rst @@ -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 \ No newline at end of file diff --git a/docs/pages/tests.rst b/docs/pages/tests.rst index 569e2644b..5e271f790 100644 --- a/docs/pages/tests.rst +++ b/docs/pages/tests.rst @@ -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 \ No newline at end of file diff --git a/docs/pages/usage.rst b/docs/pages/usage.rst index 17801490c..93d1acb4e 100644 --- a/docs/pages/usage.rst +++ b/docs/pages/usage.rst @@ -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']); @@ -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'])