diff --git a/CHANGELOG.md b/CHANGELOG.md index d04dfb6..af94f56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 5.4.0 - 2024-05-29 + +### Added + +- `Innmind\Immutable\Set::unsorted()` + ## 5.3.0 - 2023-11-06 ### Added diff --git a/docs/SET.md b/docs/SET.md index ebc123a..8d47fed 100644 --- a/docs/SET.md +++ b/docs/SET.md @@ -267,6 +267,16 @@ $sequence = Set::ints(1, 4, 2, 3)->sort(fn($a, $b) => $a <=> $b); $sequence->equals(Sequence::ints(1, 2, 3, 4)); ``` +## `->unsorted()` + +It will transform the set into an unordered sequence. + +```php +$sequence = Set::ints(1, 4, 2, 3)->unsorted(); +// is the same as +$sequence = Sequence::of(...Set::of(1, 4, 2, 3)->toList()); +``` + ## `->merge()` Create a new set with all the elements from both sets. diff --git a/proofs/predicate.php b/proofs/predicate.php index 39ccf58..9f312a1 100644 --- a/proofs/predicate.php +++ b/proofs/predicate.php @@ -7,21 +7,21 @@ yield test( 'Or predicate', static function($assert) { - $array = new \SplFixedArray; + $array = new SplFixedArray; $assert->true( - Instance::of(\Countable::class) - ->or(Instance::of(\stdClass::class)) + Instance::of(Countable::class) + ->or(Instance::of(stdClass::class)) ($array), ); $assert->true( - Instance::of(\stdClass::class) - ->or(Instance::of(\Countable::class)) + Instance::of(stdClass::class) + ->or(Instance::of(Countable::class)) ($array), ); $assert->false( - Instance::of(\Throwable::class) - ->or(Instance::of(\Unknown::class)) + Instance::of(Throwable::class) + ->or(Instance::of(Unknown::class)) ($array), ); }, @@ -30,21 +30,21 @@ static function($assert) { yield test( 'And predicate', static function($assert) { - $array = new \SplFixedArray; + $array = new SplFixedArray; $assert->true( - Instance::of(\Countable::class) - ->and(Instance::of(\Traversable::class)) + Instance::of(Countable::class) + ->and(Instance::of(Traversable::class)) ($array), ); $assert->false( - Instance::of(\Throwable::class) - ->and(Instance::of(\Countable::class)) + Instance::of(Throwable::class) + ->and(Instance::of(Countable::class)) ($array), ); $assert->false( - Instance::of(\Countable::class) - ->and(Instance::of(\Throwable::class)) + Instance::of(Countable::class) + ->and(Instance::of(Throwable::class)) ($array), ); }, diff --git a/proofs/set.php b/proofs/set.php index 38be0c5..7792ba7 100644 --- a/proofs/set.php +++ b/proofs/set.php @@ -34,4 +34,22 @@ static function($assert, $first, $rest) { $assert->same($rest, $packed[1]->toList()); }, ); + + yield proof( + 'Set::unsorted()', + given( + DataSet\Sequence::of(DataSet\Type::any()), + ), + static function($assert, $values) { + $set = Set::of(...$values); + $sequence = $set->unsorted(); + + $assert->true( + $sequence->matches($set->contains(...)), + ); + $assert->true( + $set->matches($sequence->contains(...)), + ); + }, + ); }; diff --git a/src/Accumulate.php b/src/Accumulate.php index 634129a..829b3a6 100644 --- a/src/Accumulate.php +++ b/src/Accumulate.php @@ -54,7 +54,9 @@ public function key(): mixed public function next(): void { + /** @psalm-suppress InaccessibleProperty */ \next($this->keys); + /** @psalm-suppress InaccessibleProperty */ \next($this->values); if ($this->reachedCacheEnd()) { @@ -65,7 +67,9 @@ public function next(): void public function rewind(): void { + /** @psalm-suppress InaccessibleProperty */ \reset($this->keys); + /** @psalm-suppress InaccessibleProperty */ \reset($this->values); } diff --git a/src/Map/DoubleIndex.php b/src/Map/DoubleIndex.php index b3b828c..002b682 100644 --- a/src/Map/DoubleIndex.php +++ b/src/Map/DoubleIndex.php @@ -5,7 +5,6 @@ use Innmind\Immutable\{ Map, - Str, Sequence, Set, Pair, diff --git a/src/Map/Implementation.php b/src/Map/Implementation.php index 63af24d..8e2a733 100644 --- a/src/Map/Implementation.php +++ b/src/Map/Implementation.php @@ -5,7 +5,6 @@ use Innmind\Immutable\{ Map, - Str, Set, Sequence, Pair, diff --git a/src/Map/ObjectKeys.php b/src/Map/ObjectKeys.php index 3538854..3fb69b1 100644 --- a/src/Map/ObjectKeys.php +++ b/src/Map/ObjectKeys.php @@ -5,7 +5,6 @@ use Innmind\Immutable\{ Map, - Str, Sequence, Set, Pair, diff --git a/src/Map/Primitive.php b/src/Map/Primitive.php index 0ff1a6d..8b21ec0 100644 --- a/src/Map/Primitive.php +++ b/src/Map/Primitive.php @@ -5,7 +5,6 @@ use Innmind\Immutable\{ Map, - Str, Sequence, Set, Pair, @@ -328,7 +327,10 @@ public function reduce($carry, callable $reducer) public function empty(): bool { - /** @psalm-suppress MixedArgumentTypeCoercion */ + /** + * @psalm-suppress InaccessibleProperty + * @psalm-suppress MixedArgumentTypeCoercion + */ \reset($this->values); /** @psalm-suppress MixedArgumentTypeCoercion */ diff --git a/src/Sequence/Defer.php b/src/Sequence/Defer.php index 392f3e1..094bdbe 100644 --- a/src/Sequence/Defer.php +++ b/src/Sequence/Defer.php @@ -6,7 +6,6 @@ use Innmind\Immutable\{ Map, Sequence, - Str, Set, Maybe, Accumulate, diff --git a/src/Sequence/Implementation.php b/src/Sequence/Implementation.php index 8685efc..7238474 100644 --- a/src/Sequence/Implementation.php +++ b/src/Sequence/Implementation.php @@ -6,7 +6,6 @@ use Innmind\Immutable\{ Map, Sequence, - Str, Set, Maybe, SideEffect, diff --git a/src/Sequence/Lazy.php b/src/Sequence/Lazy.php index 66c9439..914a994 100644 --- a/src/Sequence/Lazy.php +++ b/src/Sequence/Lazy.php @@ -6,7 +6,6 @@ use Innmind\Immutable\{ Map, Sequence, - Str, Set, Maybe, SideEffect, @@ -273,6 +272,7 @@ public function contains($element): bool /** @psalm-suppress ImpureFunctionCall */ $generator = ($this->values)($register); + /** @psalm-suppress ImpureMethodCall */ foreach ($generator as $value) { if ($value === $element) { /** @psalm-suppress ImpureMethodCall */ diff --git a/src/Sequence/Primitive.php b/src/Sequence/Primitive.php index dad7833..6af6821 100644 --- a/src/Sequence/Primitive.php +++ b/src/Sequence/Primitive.php @@ -6,7 +6,6 @@ use Innmind\Immutable\{ Map, Sequence, - Str, Set, Maybe, SideEffect, @@ -385,7 +384,10 @@ public function intersect(Implementation $sequence): self public function sort(callable $function): self { $self = clone $this; - /** @psalm-suppress ImpureFunctionCall */ + /** + * @psalm-suppress InaccessibleProperty + * @psalm-suppress ImpureFunctionCall + */ \usort($self->values, $function); return $self; @@ -528,6 +530,7 @@ public function aggregate(callable $map, callable $exfiltrate): self $values = $aggregate(static fn($a, $b) => $exfiltrate($map($a, $b))->iterator()); $aggregated = []; + /** @psalm-suppress ImpureMethodCall */ foreach ($values as $value) { $aggregated[] = $value; } diff --git a/src/Set.php b/src/Set.php index 60e6656..e8a99b6 100644 --- a/src/Set.php +++ b/src/Set.php @@ -362,6 +362,19 @@ public function sort(callable $function): Sequence return $this->implementation->sort($function); } + /** + * Return an unsorted sequence + * + * @return Sequence + */ + public function unsorted(): Sequence + { + return $this + ->implementation + ->sequence() + ->toSequence(); + } + /** * Create a new set with elements of both sets * diff --git a/src/Set/Defer.php b/src/Set/Defer.php index 801a26a..45f6fd4 100644 --- a/src/Set/Defer.php +++ b/src/Set/Defer.php @@ -7,7 +7,6 @@ Map, Sequence, Set, - Str, Maybe, SideEffect, }; diff --git a/src/Set/Implementation.php b/src/Set/Implementation.php index 1d85606..a6d33d7 100644 --- a/src/Set/Implementation.php +++ b/src/Set/Implementation.php @@ -7,7 +7,6 @@ Map, Sequence, Set, - Str, Maybe, SideEffect, }; diff --git a/src/Set/Lazy.php b/src/Set/Lazy.php index 790de79..1f1034b 100644 --- a/src/Set/Lazy.php +++ b/src/Set/Lazy.php @@ -7,7 +7,6 @@ Map, Sequence, Set, - Str, Maybe, SideEffect, RegisterCleanup, diff --git a/src/Set/Primitive.php b/src/Set/Primitive.php index 8f18b7c..9ff2819 100644 --- a/src/Set/Primitive.php +++ b/src/Set/Primitive.php @@ -7,7 +7,6 @@ Map, Sequence, Set, - Str, Maybe, SideEffect, }; diff --git a/tests/Map/DoubleIndexTest.php b/tests/Map/DoubleIndexTest.php index 132ee16..44e65d6 100644 --- a/tests/Map/DoubleIndexTest.php +++ b/tests/Map/DoubleIndexTest.php @@ -7,8 +7,6 @@ Map\DoubleIndex, Map\Implementation, Map, - Pair, - Str, Set, Sequence, }; @@ -20,7 +18,7 @@ public function testInterface() { $m = new DoubleIndex; - $this->assertInstanceOf(Map\Implementation::class, $m); + $this->assertInstanceOf(Implementation::class, $m); $this->assertInstanceOf(\Countable::class, $m); } diff --git a/tests/Map/ObjectKeysTest.php b/tests/Map/ObjectKeysTest.php index a4ce325..19c50b6 100644 --- a/tests/Map/ObjectKeysTest.php +++ b/tests/Map/ObjectKeysTest.php @@ -8,8 +8,6 @@ Map\DoubleIndex, Map\Implementation, Map, - Pair, - Str, Set, Sequence, }; @@ -21,7 +19,7 @@ public function testInterface() { $m = new ObjectKeys; - $this->assertInstanceOf(Map\Implementation::class, $m); + $this->assertInstanceOf(Implementation::class, $m); $this->assertInstanceOf(\Countable::class, $m); } diff --git a/tests/Map/PrimitiveTest.php b/tests/Map/PrimitiveTest.php index dcd3a01..23256ca 100644 --- a/tests/Map/PrimitiveTest.php +++ b/tests/Map/PrimitiveTest.php @@ -7,8 +7,6 @@ Map\Primitive, Map\Implementation, Map, - Pair, - Str, Set, Sequence, }; @@ -20,7 +18,7 @@ public function testInterface() { $m = new Primitive; - $this->assertInstanceOf(Map\Implementation::class, $m); + $this->assertInstanceOf(Implementation::class, $m); $this->assertInstanceOf(\Countable::class, $m); } diff --git a/tests/MapTest.php b/tests/MapTest.php index 434e295..14c3060 100644 --- a/tests/MapTest.php +++ b/tests/MapTest.php @@ -5,8 +5,6 @@ use Innmind\Immutable\{ Map, - Pair, - Str, Set, Sequence, }; diff --git a/tests/Sequence/DeferTest.php b/tests/Sequence/DeferTest.php index a591b9c..8081fe5 100644 --- a/tests/Sequence/DeferTest.php +++ b/tests/Sequence/DeferTest.php @@ -7,9 +7,6 @@ Sequence\Defer, Sequence\Implementation, Map, - Sequence, - Str, - Set, SideEffect, }; use Innmind\BlackBox\PHPUnit\Framework\TestCase; diff --git a/tests/Sequence/LazyTest.php b/tests/Sequence/LazyTest.php index 465bcde..e7eba1e 100644 --- a/tests/Sequence/LazyTest.php +++ b/tests/Sequence/LazyTest.php @@ -7,9 +7,6 @@ Sequence\Lazy, Sequence\Implementation, Map, - Sequence, - Str, - Set, SideEffect, }; use Innmind\BlackBox\PHPUnit\Framework\TestCase; diff --git a/tests/Sequence/PrimitiveTest.php b/tests/Sequence/PrimitiveTest.php index da3f75e..a886c36 100644 --- a/tests/Sequence/PrimitiveTest.php +++ b/tests/Sequence/PrimitiveTest.php @@ -7,9 +7,6 @@ Sequence\Primitive, Sequence\Implementation, Map, - Sequence, - Str, - Set, SideEffect, }; use Innmind\BlackBox\PHPUnit\Framework\TestCase; diff --git a/tests/Set/DeferTest.php b/tests/Set/DeferTest.php index aa02f97..f1676e6 100644 --- a/tests/Set/DeferTest.php +++ b/tests/Set/DeferTest.php @@ -6,9 +6,7 @@ use Innmind\Immutable\{ Set\Defer, Set\Implementation, - Set, Map, - Str, Sequence, SideEffect, }; diff --git a/tests/Set/LazyTest.php b/tests/Set/LazyTest.php index 2612e66..d8f1fa7 100644 --- a/tests/Set/LazyTest.php +++ b/tests/Set/LazyTest.php @@ -6,9 +6,7 @@ use Innmind\Immutable\{ Set\Lazy, Set\Implementation, - Set, Map, - Str, Sequence, SideEffect, }; diff --git a/tests/Set/PrimitiveTest.php b/tests/Set/PrimitiveTest.php index d2d00f8..5937aa3 100644 --- a/tests/Set/PrimitiveTest.php +++ b/tests/Set/PrimitiveTest.php @@ -6,9 +6,7 @@ use Innmind\Immutable\{ Set\Primitive, Set\Implementation, - Set, Map, - Str, Sequence, SideEffect, }; diff --git a/tests/SetTest.php b/tests/SetTest.php index 1418b61..0f0da2d 100644 --- a/tests/SetTest.php +++ b/tests/SetTest.php @@ -6,7 +6,6 @@ use Innmind\Immutable\{ Set, Map, - Str, Sequence, Predicate, }; diff --git a/tests/StrTest.php b/tests/StrTest.php index 1490ea3..3a79c92 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -8,7 +8,6 @@ Sequence, Map, Set, - Exception\InvalidRegex, }; use Innmind\BlackBox\PHPUnit\Framework\TestCase;