Skip to content

Commit 085455d

Browse files
authored
Merge pull request #25 from Innmind/map-to-sequence
Add `Map::toSequence()`
2 parents 99aa7f8 + 4a320a0 commit 085455d

9 files changed

+100
-0
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## [Unreleased]
4+
5+
### Added
6+
7+
- `Innmind\Immutable\Map::toSequence()`
8+
9+
### Fixed
10+
11+
- Using `string`s or `int`s as a `Map` key type and then adding keys of different types was throwing an error.
12+
313
## 5.9.0 - 2024-07-05
414

515
### Added

docs/structures/map.md

+14
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,17 @@ Return an empty new map of the same type. Useful to avoid to respecify the templ
349349
$map = Map::of([1, 2], [3, 4]);
350350
$map->clear()->size(); // 0
351351
```
352+
353+
### `->toSequence()`
354+
355+
Returns an unsorted `Sequence` with all value pairs.
356+
357+
```php
358+
$map = Map::of([1, 2], [3, 4]);
359+
$map
360+
->toSequence()
361+
->equals(Sequence::of(
362+
new Pair(1, 2),
363+
new Pair(3, 4),
364+
)); // true
365+
```

proofs/map.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
use Innmind\Immutable\Map;
5+
use Innmind\BlackBox\Set;
6+
7+
return static function() {
8+
yield proof(
9+
'Map::toSequence()',
10+
given(
11+
Set\Sequence::of(Set\Type::any()),
12+
Set\Sequence::of(Set\Type::any()),
13+
),
14+
static function($assert, $keys, $values) {
15+
$map = Map::of();
16+
17+
foreach ($keys as $index => $key) {
18+
$map = ($map)($key, $values[$index] ?? null);
19+
}
20+
21+
$assert->true(
22+
$map->equals(Map::of(
23+
...$map
24+
->toSequence()
25+
->map(static fn($pair) => [$pair->key(), $pair->value()])
26+
->toList(),
27+
)),
28+
);
29+
},
30+
);
31+
};

src/Map.php

+8
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,12 @@ public function any(callable $predicate): bool
328328
static fn() => false,
329329
);
330330
}
331+
332+
/**
333+
* @return Sequence<Pair<T, S>>
334+
*/
335+
public function toSequence(): Sequence
336+
{
337+
return $this->implementation->toSequence();
338+
}
331339
}

src/Map/DoubleIndex.php

+5
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ public function find(callable $predicate): Maybe
302302
return $this->pairs->find(static fn($pair) => $predicate($pair->key(), $pair->value()));
303303
}
304304

305+
public function toSequence(): Sequence
306+
{
307+
return $this->pairs->toSequence();
308+
}
309+
305310
/**
306311
* @return Map<T, S>
307312
*/

src/Map/Implementation.php

+5
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,9 @@ public function empty(): bool;
166166
* @return Maybe<Pair<T, S>>
167167
*/
168168
public function find(callable $predicate): Maybe;
169+
170+
/**
171+
* @return Sequence<Pair<T, S>>
172+
*/
173+
public function toSequence(): Sequence;
169174
}

src/Map/ObjectKeys.php

+9
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,15 @@ public function find(callable $predicate): Maybe
430430
return Maybe::nothing();
431431
}
432432

433+
public function toSequence(): Sequence
434+
{
435+
/** @var Sequence<Pair<T, S>> */
436+
return $this->reduce(
437+
Sequence::of(),
438+
static fn(Sequence $pairs, $key, $value) => ($pairs)(new Pair($key, $value)),
439+
);
440+
}
441+
433442
/**
434443
* @return Map<T, S>
435444
*/

src/Map/Primitive.php

+13
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public function __invoke($key, $value): Implementation
4747
return (new DoubleIndex)->merge($this)($key, $value);
4848
}
4949

50+
if (!\is_string($key) && !\is_int($key)) {
51+
return (new DoubleIndex)->merge($this)($key, $value);
52+
}
53+
5054
$values = $this->values;
5155
$values[$key] = $value;
5256

@@ -350,6 +354,15 @@ public function find(callable $predicate): Maybe
350354
return Maybe::nothing();
351355
}
352356

357+
public function toSequence(): Sequence
358+
{
359+
/** @var Sequence<Pair<T, S>> */
360+
return $this->reduce(
361+
Sequence::of(),
362+
static fn(Sequence $pairs, $key, $value) => ($pairs)(new Pair($key, $value)),
363+
);
364+
}
365+
353366
/**
354367
* @return Map<T, S>
355368
*/

src/Map/Uninitialized.php

+5
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ public function find(callable $predicate): Maybe
214214
return Maybe::nothing();
215215
}
216216

217+
public function toSequence(): Sequence
218+
{
219+
return Sequence::of();
220+
}
221+
217222
/**
218223
* @return Map<T, S>
219224
*/

0 commit comments

Comments
 (0)