From c58505b855a631c4d5eff2adf3407364d6c89407 Mon Sep 17 00:00:00 2001 From: Valentin Udaltsov Date: Mon, 2 Sep 2024 05:42:09 +0300 Subject: [PATCH] Improve --- composer.json | 3 --- src/DataStructure/Internal/ArrayMap.php | 33 +++++++++++++++++++++++++ src/DataStructure/Map.php | 14 +++++------ src/DataStructure/MutableMap.php | 8 +++--- tests/DataStructure/MapTestCase.php | 4 +-- 5 files changed, 46 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index 9fb380ed..77ba0a94 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/DataStructure/Internal/ArrayMap.php b/src/DataStructure/Internal/ArrayMap.php index 73c6d2d9..9de13ca2 100644 --- a/src/DataStructure/Internal/ArrayMap.php +++ b/src/DataStructure/Internal/ArrayMap.php @@ -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 $kv + */ + static fn(mixed $accumulator, KVPair $kv): mixed => $operation($accumulator, $kv->value), + $initial, + ); + } + /** * @template I * @template R @@ -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( diff --git a/src/DataStructure/Map.php b/src/DataStructure/Map.php index 35029d18..05aa0006 100644 --- a/src/DataStructure/Map.php +++ b/src/DataStructure/Map.php @@ -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)) { @@ -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)) { @@ -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)) { @@ -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)) { @@ -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( /** @@ -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, @@ -362,7 +362,7 @@ abstract public function filterKV(callable $predicate): static; * @param callable(V): NV $mapper * @return static */ - final public function map(callable $mapper): static + public function map(callable $mapper): static { return $this->mapKV( /** @param V $value */ diff --git a/src/DataStructure/MutableMap.php b/src/DataStructure/MutableMap.php index 6ca5efa9..eb2d0cc3 100644 --- a/src/DataStructure/MutableMap.php +++ b/src/DataStructure/MutableMap.php @@ -108,7 +108,7 @@ public function with(mixed $key, mixed $value): static * @param KVPair ...$kvPairs * @return static */ - final public function withPairs(KVPair ...$kvPairs): static + public function withPairs(KVPair ...$kvPairs): static { if ($kvPairs === []) { return $this; @@ -140,7 +140,7 @@ protected function doWithAll(iterable $values): static * @no-named-arguments * @return static */ - final public function without(mixed ...$keys): static + public function without(mixed ...$keys): static { if ($keys === []) { return $this; @@ -239,7 +239,7 @@ public function filterKV(callable $predicate): static * @param callable(K, V): NK $mapper * @return static */ - final public function mapKeyKV(callable $mapper): static + public function mapKeyKV(callable $mapper): static { /** @var static */ $map = new static(); @@ -255,7 +255,7 @@ final public function mapKeyKV(callable $mapper): static /** * @return static */ - final public function flip(): static + public function flip(): static { $map = new static(); diff --git a/tests/DataStructure/MapTestCase.php b/tests/DataStructure/MapTestCase.php index f5d7424a..7d817dc2 100644 --- a/tests/DataStructure/MapTestCase.php +++ b/tests/DataStructure/MapTestCase.php @@ -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