Skip to content

Commit

Permalink
Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Sep 2, 2024
1 parent 25edebb commit c58505b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
3 changes: 0 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@
"symfony/polyfill-php84": "^1.30"
},
"require-dev": {
"ext-ds": "^1",
"bamarni/composer-bin-plugin": "^1.8.2",
"doctrine/collections": "^2.2",
"dragon-code/benchmark": "^2.6 || ^3",
"ergebnis/composer-normalize": "^2.43.0",
"friendsofphp/php-cs-fixer": "^3.62.0",
"illuminate/collections": "^11.21",
"infection/infection": "^0.29.6",
"mikey179/vfsstream": "^1.6.11",
"phpstan/phpstan": "^1.11.9",
Expand Down
33 changes: 33 additions & 0 deletions src/DataStructure/Internal/ArrayMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,26 @@ public function reduceKV(callable $operation): mixed
);
}

/**
* @template I
* @template R
* @param I $initial
* @param callable(I|R, V): R $operation
* @return I|R
*/
public function fold(mixed $initial, callable $operation): mixed
{
return array_reduce(
$this->kvPairs,
/**
* @param I|R $accumulator
* @param KVPair<K,V> $kv
*/
static fn(mixed $accumulator, KVPair $kv): mixed => $operation($accumulator, $kv->value),
$initial,
);
}

/**
* @template I
* @template R
Expand All @@ -161,11 +181,24 @@ public function foldKV(mixed $initial, callable $operation): mixed
);
}

public function filter(callable $predicate): static
{
return new self(array_filter($this->kvPairs, static fn(KVPair $kv): bool => $predicate($kv->value)));
}

public function filterKV(callable $predicate): static
{
return new self(array_filter($this->kvPairs, static fn(KVPair $kv): bool => $predicate($kv->key, $kv->value)));
}

public function map(callable $mapper): static
{
return new self(array_map(
static fn(KVPair $kv): KVPair => $kv->withValue($mapper($kv->value)),
$this->kvPairs,
));
}

public function mapKV(callable $mapper): static
{
return new self(array_map(
Expand Down
14 changes: 7 additions & 7 deletions src/DataStructure/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function findFirstKV(callable $predicate): ?KVPair
/**
* @param callable(V): bool $predicate
*/
final public function any(callable $predicate): bool
public function any(callable $predicate): bool
{
foreach ($this->getIterator() as $value) {
if ($predicate($value)) {
Expand All @@ -222,7 +222,7 @@ final public function any(callable $predicate): bool
/**
* @param callable(K, V): bool $predicate
*/
final public function anyKV(callable $predicate): bool
public function anyKV(callable $predicate): bool
{
foreach ($this->getIterator() as $key => $value) {
if ($predicate($key, $value)) {
Expand All @@ -236,7 +236,7 @@ final public function anyKV(callable $predicate): bool
/**
* @param callable(V): bool $predicate
*/
final public function all(callable $predicate): bool
public function all(callable $predicate): bool
{
foreach ($this->getIterator() as $value) {
if (!$predicate($value)) {
Expand All @@ -250,7 +250,7 @@ final public function all(callable $predicate): bool
/**
* @param callable(K, V): bool $predicate
*/
final public function allKV(callable $predicate): bool
public function allKV(callable $predicate): bool
{
foreach ($this->getIterator() as $key => $value) {
if (!$predicate($key, $value)) {
Expand All @@ -266,7 +266,7 @@ final public function allKV(callable $predicate): bool
* @param callable(V|R, V): R $operation
* @return V|R
*/
final public function reduce(callable $operation): mixed
public function reduce(callable $operation): mixed
{
return $this->reduceKV(
/**
Expand Down Expand Up @@ -311,7 +311,7 @@ public function reduceKV(callable $operation): mixed
* @param callable(I|R, V): R $operation
* @return I|R
*/
final public function fold(mixed $initial, callable $operation): mixed
public function fold(mixed $initial, callable $operation): mixed
{
return $this->foldKV(
$initial,
Expand Down Expand Up @@ -362,7 +362,7 @@ abstract public function filterKV(callable $predicate): static;
* @param callable(V): NV $mapper
* @return static<K, NV>
*/
final public function map(callable $mapper): static
public function map(callable $mapper): static
{
return $this->mapKV(
/** @param V $value */
Expand Down
8 changes: 4 additions & 4 deletions src/DataStructure/MutableMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function with(mixed $key, mixed $value): static
* @param KVPair<NK, NV> ...$kvPairs
* @return static<K|NK, V|NV>
*/
final public function withPairs(KVPair ...$kvPairs): static
public function withPairs(KVPair ...$kvPairs): static
{
if ($kvPairs === []) {
return $this;
Expand Down Expand Up @@ -140,7 +140,7 @@ protected function doWithAll(iterable $values): static
* @no-named-arguments
* @return static<K, V>
*/
final public function without(mixed ...$keys): static
public function without(mixed ...$keys): static
{
if ($keys === []) {
return $this;
Expand Down Expand Up @@ -239,7 +239,7 @@ public function filterKV(callable $predicate): static
* @param callable(K, V): NK $mapper
* @return static<NK, V>
*/
final public function mapKeyKV(callable $mapper): static
public function mapKeyKV(callable $mapper): static
{
/** @var static<NK, V> */
$map = new static();
Expand All @@ -255,7 +255,7 @@ final public function mapKeyKV(callable $mapper): static
/**
* @return static<V, K>
*/
final public function flip(): static
public function flip(): static
{
$map = new static();

Expand Down
4 changes: 2 additions & 2 deletions tests/DataStructure/MapTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ abstract class MapTestCase extends TestCase
final protected static function assertMapEquals(array $expected, Map $map): void
{
self::assertSame($expected, $map->toArray());
// check map internal keys are same
self::assertEquals(static::createMap($expected), $map);
// check map hashes are correct
self::assertTrue($map->without(...array_keys($expected))->isEmpty());
}

final public function testWithReturnsNewMapWithAddedElement(): void
Expand Down

0 comments on commit c58505b

Please sign in to comment.