Skip to content

Commit

Permalink
feat: Add Averages operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Feb 24, 2022
1 parent cf8f1ae commit dca5e69
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use loophp\collection\Operation\Associate;
use loophp\collection\Operation\AsyncMap;
use loophp\collection\Operation\AsyncMapN;
use loophp\collection\Operation\Averages;
use loophp\collection\Operation\Cache;
use loophp\collection\Operation\Chunk;
use loophp\collection\Operation\Coalesce;
Expand Down Expand Up @@ -208,6 +209,11 @@ public function asyncMapN(callable ...$callbacks): CollectionInterface
return new self((new AsyncMapN())()(...$callbacks), [$this]);
}

public function averages(): CollectionInterface
{
return new self((new Averages())(), [$this]);
}

public function cache(?CacheItemPoolInterface $cache = null): CollectionInterface
{
return new self((new Cache())()($cache ?? new ArrayAdapter()), [$this]);
Expand Down
3 changes: 3 additions & 0 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use loophp\collection\Contract\Operation\Associateable;
use loophp\collection\Contract\Operation\AsyncMapable;
use loophp\collection\Contract\Operation\AsyncMapNable;
use loophp\collection\Contract\Operation\Averagesable;
use loophp\collection\Contract\Operation\Cacheable;
use loophp\collection\Contract\Operation\Chunkable;
use loophp\collection\Contract\Operation\Coalesceable;
Expand Down Expand Up @@ -142,6 +143,7 @@
* @template-extends Associateable<TKey, T>
* @template-extends AsyncMapable<TKey, T>
* @template-extends AsyncMapNable<TKey, T>
* @template-extends Averagesable<TKey, T>
* @template-extends Cacheable<TKey, T>
* @template-extends Chunkable<TKey, T>
* @template-extends Coalesceable<TKey, T>
Expand Down Expand Up @@ -259,6 +261,7 @@ interface Collection extends
Associateable,
AsyncMapable,
AsyncMapNable,
Averagesable,
Cacheable,
Chunkable,
Coalesceable,
Expand Down
28 changes: 28 additions & 0 deletions src/Contract/Operation/Averagesable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace loophp\collection\Contract\Operation;

use loophp\collection\Contract\Collection;

/**
* @template TKey
* @template T
*/
interface Averagesable
{
/**
* Compute the averages.
*
* @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#averages
*
* @return Collection<TKey, float>
*/
public function averages(): Collection;
}
43 changes: 43 additions & 0 deletions src/Operation/Averages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace loophp\collection\Operation;

use Closure;

/**
* @immutable
*
* @template TKey
* @template T
*
* phpcs:disable Generic.Files.LineLength.TooLong
*/
final class Averages extends AbstractOperation
{
/**
* @return Closure(iterable<TKey, T>): Generator<int, float>
*/
public function __invoke(): Closure
{
return Pipe::of()(
(new Normalize())(),
(new Associate())()(static fn (int $i): int => $i + 1)(static fn ($i) => $i),
(new ScanLeft1())()(
/**
* @param float $acc
* @param float $value
*
* @return float
*/
static fn ($acc, $value, int $key) => ($acc * ($key - 1) + $value) / ($key)
)
);
}
}
1 change: 1 addition & 0 deletions tests/unit/CollectionGenericOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ final class CollectionGenericOperationTest extends TestCase
* @dataProvider associateOperationProvider
* @dataProvider asyncMapOperationProvider
* @dataProvider asyncMapNOperationProvider
* @dataProvider averagesOperationProvider
* @dataProvider cacheOperationProvider
* @dataProvider chunkOperationProvider
* @dataProvider coalesceOperationProvider
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/Traits/GenericCollectionProviders.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ static function (int $v): int {
];
}

public function averagesOperationProvider()
{
$operation = 'averages';
$input = range(0, 5);

yield [
$operation,
[],
$input,
[(int) 0, (float) .5, (float) 1.0, (float) 1.5, (float) 2.0, (float) 2.5],
];
}

public function cacheOperationProvider()
{
$operation = 'cache';
Expand Down

0 comments on commit dca5e69

Please sign in to comment.