Skip to content

Commit

Permalink
Add a new example.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jun 23, 2020
1 parent 1a70029 commit df73058
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions docs/pages/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,50 @@ Find Prime numbers
->limit(100); // Take the 100 first prime numbers.
print_r($primes->all());
Text analysis
-------------
.. code-block:: php
<?php
declare(strict_types=1);
include __DIR__ . '/vendor/autoload.php';
use loophp\collection\Collection;
use loophp\collection\Contract\Operation;
use loophp\collection\Operation\AbstractOperation;
$data = file_get_contents('http://loripsum.net/api');
$textFrequencyAnalysis = new class() extends AbstractOperation implements Operation {
public function __invoke(): Closure
{
return static function (iterable $collection): Generator {
$storage = [];
foreach ($collection as $value) {
$storage += [$value => 0];
++$storage[$value];
}
yield from $storage;
};
}
};
$result = Collection::with($data)
->map(static function (string $letter):string {return strtolower($letter); })
->filter(
static function ($item, $key): bool {
return (bool) preg_match('/^[a-zA-Z]+$/', $item);
}
)
->run($textFrequencyAnalysis)
->sort()
->all();
print_r($result);

0 comments on commit df73058

Please sign in to comment.