Skip to content

Commit

Permalink
Update documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 1, 2020
1 parent 94c010a commit 3bb8caa
Showing 1 changed file with 27 additions and 59 deletions.
86 changes: 27 additions & 59 deletions docs/pages/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~
Expand Down

0 comments on commit 3bb8caa

Please sign in to comment.