From 3bb8caa8139d834eb0407280a7951d054ce1a0bf Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Mon, 31 Aug 2020 18:58:35 +0200 Subject: [PATCH] Update documentation. --- docs/pages/usage.rst | 86 ++++++++++++++------------------------------ 1 file changed, 27 insertions(+), 59 deletions(-) diff --git a/docs/pages/usage.rst b/docs/pages/usage.rst index cb26185ff..2f66c3335 100644 --- a/docs/pages/usage.rst +++ b/docs/pages/usage.rst @@ -572,97 +572,65 @@ Prime numbers declare(strict_types=1); - include 'vendor/autoload.php'; + include __DIR__ . '/vendor/autoload.php'; use loophp\collection\Collection; - function llist($init, callable $succ): Generator - { - for ($v = $init; ; $v = $succ($v)) { - yield $v; - } - } - - function not(callable $test): Closure + function primesGenerator(Iterator $iterator): Generator { - return static function (...$arguments) use ($test): bool { - return !$test(...$arguments); - }; - }; + yield $primeNumber = $iterator->current(); - function multipleOf(int $a): Closure - { - return static function (int $b) use ($a): bool { - return 0 === ($b % $a); - }; - }; + $iterator = new \CallbackFilterIterator( + $iterator, + fn(int $a): bool => $a % $primeNumber !== 0 + ); - function filter(Generator $generator, callable $filter): Generator - { - for (; true === $generator->valid(); $generator->next()) { - $n = $generator->current(); + $iterator->next(); - if (true === $filter($n)) { - yield $n; - } - } + return $iterator->valid() ? + yield from primesGenerator($iterator): + null; } - function headTail(Generator $generator): array + function integerGenerator(int $init = 1, callable $succ): Generator { - $head = $generator->current(); - - $generator->next(); + yield $init; - return [$head, $generator]; + return yield from integerGenerator($succ($init), $succ); } - function primesGeneratorBuilder(Generator $n): Generator - { - [$primeNumber, $tail] = headTail($n); - - yield $primeNumber; - - return yield from primesGeneratorBuilder( - filter( - $tail, - not(multipleOf($primeNumber)) - ) - ); - }; + $primes = primesGenerator(integerGenerator(2, fn(int $n): int => $n + 1)); $limit = 1000000; // Create a lazy collection of Prime numbers from 2 to infinity. $lazyPrimeNumbersCollection = Collection::fromIterable( - primesGeneratorBuilder( - llist(2, static fn ($n) => $n + 1) + primesGenerator( + integerGenerator(2, static fn ($n) => $n + 1) ) ); // Print out the first 1 million of prime numbers. - $lazyPrimeNumbersCollection - ->limit($limit) - ->apply(static fn ($value) => var_dump($value)) - ->all(); + foreach ($lazyPrimeNumbersCollection->limit($limit) as $prime) { + var_dump($prime); + } // Create a lazy collection of Prime numbers from 2 to infinity. $lazyPrimeNumbersCollection = Collection::fromIterable( - primesGeneratorBuilder( - llist(2, static fn ($n) => $n + 1) + primesGenerator( + integerGenerator(2, static fn ($n) => $n + 1) ) ); // Find out the Twin Prime numbers by filtering out unwanted values. $lazyTwinPrimeNumbersCollection = Collection::fromIterable($lazyPrimeNumbersCollection) ->zip($lazyPrimeNumbersCollection->tail()) - ->filter(static fn (array $chunk) => 2 === $chunk[1] - $chunk[0]); + ->filter(static fn (array $chunk): bool => 2 === $chunk[1] - $chunk[0]); + + foreach ($lazyTwinPrimeNumbersCollection->limit($limit) as $prime) { + var_dump($prime); + } - // Print out the first 1 million Twin Prime numbers. - $lazyTwinPrimeNumbersCollection - ->limit($limit) - ->apply(static fn ($value) => var_dump($value)) - ->all(); Text analysis ~~~~~~~~~~~~~