Skip to content

Commit 132a27b

Browse files
authored
Implement removeAll for Set (#85)
* Implement removeAll for Set * Add more tests
1 parent d41bc69 commit 132a27b

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/Collection/Set.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ public function remove($element): self
109109
return self::fromPointer($elements);
110110
}
111111

112+
/**
113+
* @param Set<T> $elements
114+
*
115+
* @return Set<T>
116+
*/
117+
public function removeAll(Set $elements): self
118+
{
119+
$new = [];
120+
foreach ($this->elements as $current) {
121+
if (!$elements->contains($current)) {
122+
$new[] = $current;
123+
}
124+
}
125+
126+
return self::fromPointer($new);
127+
}
128+
112129
/**
113130
* @param Set<T> $set
114131
*

tests/Collection/SetTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,20 @@ public function testFlatMap(): void
248248
Set::ofAll([1, 2, 4, 3, 6])
249249
));
250250
}
251+
252+
public function testSetRemoveAll(): void
253+
{
254+
$set = Set::ofAll(['alpha', 'beta', 'gamma']);
255+
$new = $set->removeAll(Set::ofAll(['beta', 'gamma']));
256+
257+
self::assertFalse($new->contains('beata'));
258+
self::assertFalse($new->contains('gamma'));
259+
self::assertTrue($set !== $new);
260+
self::assertEquals(1, $new->length());
261+
262+
self::assertTrue(Set::ofAll(['alpha', 'beta', 'gamma'])->removeAll(Set::empty())->equals(Set::ofAll(['alpha', 'beta', 'gamma'])));
263+
self::assertTrue(Set::ofAll(['alpha', 'beta', 'gamma'])->removeAll(Set::ofAll(['alpha', 'beta', 'gamma']))->equals(Set::empty()));
264+
self::assertTrue(Set::ofAll(['alpha', 'beta', 'gamma'])->removeAll(Set::ofAll(['1', '2', '3']))->equals(Set::ofAll(['alpha', 'beta', 'gamma'])));
265+
self::assertTrue(Set::ofAll(['alpha', 'beta', 'gamma'])->removeAll(Set::ofAll(['1', '2', 'gamma']))->equals(Set::ofAll(['alpha', 'beta'])));
266+
}
251267
}

0 commit comments

Comments
 (0)