Page MenuHomeDevCentral

D3214.diff
No OneTemporary

D3214.diff

diff --git a/omnitools/src/Collections/HashMap.php b/omnitools/src/Collections/HashMap.php
--- a/omnitools/src/Collections/HashMap.php
+++ b/omnitools/src/Collections/HashMap.php
@@ -232,6 +232,21 @@
return $newMap;
}
+ public function mapToVector (callable $callable) : Vector {
+ $argc = (new CallableElement($callable))->countArguments();
+
+ $vector = [];
+ foreach ($this->map as $key => $value) {
+ $vector[] = match($argc) {
+ 0 => throw new InvalidArgumentException(self::CB_ZERO_ARG),
+ 1 => $callable($value),
+ default => $callable($key, $value),
+ };
+ }
+
+ return new Vector($vector);
+ }
+
public function filterKeys (callable $callable) : self {
return new self(
array_filter($this->map, $callable, ARRAY_FILTER_USE_KEY)
diff --git a/omnitools/tests/Collections/HashMapTest.php b/omnitools/tests/Collections/HashMapTest.php
--- a/omnitools/tests/Collections/HashMapTest.php
+++ b/omnitools/tests/Collections/HashMapTest.php
@@ -364,6 +364,40 @@
$this->map->flatMap($callback);
}
+ public function testMapToVector() : void {
+ $expected = [
+ "The Culture by Iain Banks",
+ "Radchaai Empire by Ann Leckie",
+ "Barrayar by Lois McMaster Bujold",
+ "Hainish by Ursula K. Le Guin",
+ ];
+
+ $fn = fn($key, $value) => "$key by $value";
+
+ $this->assertEquals($expected, $this->map->mapToVector($fn)->toArray());
+ }
+
+ public function testMapToVectorWithOnlyValueParameter() : void {
+ $expected = [
+ "Author: Iain Banks",
+ "Author: Ann Leckie",
+ "Author: Lois McMaster Bujold",
+ "Author: Ursula K. Le Guin",
+ ];
+
+ $fn = fn($value) => "Author: $value";
+
+ $this->assertEquals($expected, $this->map->mapToVector($fn)->toArray());
+ }
+
+ public function testMapToVectorWithoutParameter() : void {
+ $this->expectException(InvalidArgumentException::class);
+
+ $fn = fn() => "";
+
+ $this->map->mapToVector($fn);
+ }
+
public function testFilter () {
// Let's filter to keep names with 3 parts or more

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 23, 23:25 (16 h, 30 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2259159
Default Alt Text
D3214.diff (2 KB)

Event Timeline