Skip to content

Commit 5d534bf

Browse files
committed
[Collection] Set/NonEmptySet intersect and diff ops.
1 parent 80a4a68 commit 5d534bf

File tree

6 files changed

+151
-17
lines changed

6 files changed

+151
-17
lines changed

src/Fp/Collections/HashSet.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,24 @@ public function subsetOf(Set|NonEmptySet $superset): bool
404404

405405
return $isSubset;
406406
}
407+
408+
/**
409+
* @inheritDoc
410+
* @param Set<TV>|NonEmptySet<TV> $that
411+
* @return Set<TV>
412+
*/
413+
public function intersect(Set|NonEmptySet $that): Set
414+
{
415+
return $this->filter(fn($elem) => /** @var TV $elem */ $that($elem));
416+
}
417+
418+
/**
419+
* @inheritDoc
420+
* @param Set<TV>|NonEmptySet<TV> $that
421+
* @return Set<TV>
422+
*/
423+
public function diff(Set|NonEmptySet $that): Set
424+
{
425+
return $this->filter(fn($elem) => /** @var TV $elem */ !$that($elem));
426+
}
407427
}

src/Fp/Collections/NonEmptyHashSet.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,24 @@ public function subsetOf(Set|NonEmptySet $superset): bool
445445

446446
return $isSubset;
447447
}
448+
449+
/**
450+
* @inheritDoc
451+
* @param Set<TV>|NonEmptySet<TV> $that
452+
* @return Set<TV>
453+
*/
454+
public function intersect(Set|NonEmptySet $that): Set
455+
{
456+
return $this->filter(fn($elem) => /** @var TV $elem */ $that($elem));
457+
}
458+
459+
/**
460+
* @inheritDoc
461+
* @param Set<TV>|NonEmptySet<TV> $that
462+
* @return Set<TV>
463+
*/
464+
public function diff(Set|NonEmptySet $that): Set
465+
{
466+
return $this->filter(fn($elem) => /** @var TV $elem */ !$that($elem));
467+
}
448468
}

src/Fp/Collections/NonEmptySetChainableOps.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,34 @@ public function tap(callable $callback): NonEmptySet;
155155
* @psalm-return Set<TV>
156156
*/
157157
public function tail(): Set;
158+
159+
/**
160+
* Computes the intersection between this set and another set.
161+
*
162+
* ```php
163+
* >>> NonEmptyHashSet::collectNonEmpty([1, 2, 3])
164+
* ->intersect(HashSet::collect([2, 3]))->toArray();
165+
* => [2, 3]
166+
* ```
167+
*
168+
* @param Set<TV>|NonEmptySet<TV> $that the set to intersect with.
169+
* @return Set<TV> a new set consisting of all elements that are both in this
170+
* set and in the given set `that`.
171+
*/
172+
public function intersect(Set|NonEmptySet $that): Set;
173+
174+
/**
175+
* Computes the difference of this set and another set.
176+
*
177+
* ```php
178+
* >>> NonEmptyHashSet::collectNonEmpty([1, 2, 3])
179+
* ->diff(HashSet::collect([2, 3]))->toArray();
180+
* => [1]
181+
* ```
182+
*
183+
* @param Set<TV>|NonEmptySet<TV> $that the set of elements to exclude.
184+
* @return Set<TV> a set containing those elements of this
185+
* set that are not also contained in the given set `that`.
186+
*/
187+
public function diff(Set|NonEmptySet $that): Set;
158188
}

src/Fp/Collections/SetChainableOps.php

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public function removed(mixed $element): Set;
4747
* => [2]
4848
* ```
4949
*
50-
* @psalm-param callable(TV): bool $predicate
51-
* @psalm-return Set<TV>
50+
* @param callable(TV): bool $predicate
51+
* @return Set<TV>
5252
*/
5353
public function filter(callable $predicate): Set;
5454

@@ -60,10 +60,10 @@ public function filter(callable $predicate): Set;
6060
* => [Foo(2)]
6161
* ```
6262
*
63-
* @psalm-template TVO
64-
* @psalm-param class-string<TVO> $fqcn fully qualified class name
65-
* @psalm-param bool $invariant if turned on then subclasses are not allowed
66-
* @psalm-return Set<TVO>
63+
* @template TVO
64+
* @param class-string<TVO> $fqcn fully qualified class name
65+
* @param bool $invariant if turned on then subclasses are not allowed
66+
* @return Set<TVO>
6767
*/
6868
public function filterOf(string $fqcn, bool $invariant = false): Set;
6969

@@ -75,7 +75,7 @@ public function filterOf(string $fqcn, bool $invariant = false): Set;
7575
* => [1]
7676
* ```
7777
*
78-
* @psalm-return Set<TV>
78+
* @return Set<TV>
7979
*/
8080
public function filterNotNull(): Set;
8181

@@ -92,9 +92,9 @@ public function filterNotNull(): Set;
9292
* => [1, 2]
9393
* ```
9494
*
95-
* @psalm-template TVO
96-
* @psalm-param callable(TV): Option<TVO> $callback
97-
* @psalm-return Set<TVO>
95+
* @template TVO
96+
* @param callable(TV): Option<TVO> $callback
97+
* @return Set<TVO>
9898
*/
9999
public function filterMap(callable $callback): Set;
100100

@@ -104,9 +104,9 @@ public function filterMap(callable $callback): Set;
104104
* => [1, 2, 3, 4, 5, 6]
105105
* ```
106106
*
107-
* @psalm-template TVO
108-
* @psalm-param callable(TV): iterable<TVO> $callback
109-
* @psalm-return Set<TVO>
107+
* @template TVO
108+
* @param callable(TV): iterable<TVO> $callback
109+
* @return Set<TVO>
110110
*/
111111
public function flatMap(callable $callback): Set;
112112

@@ -120,8 +120,8 @@ public function flatMap(callable $callback): Set;
120120
* ```
121121
*
122122
* @template TVO
123-
* @psalm-param callable(TV): TVO $callback
124-
* @psalm-return Set<TVO>
123+
* @param callable(TV): TVO $callback
124+
* @return Set<TVO>
125125
*/
126126
public function map(callable $callback): Set;
127127

@@ -137,7 +137,7 @@ public function map(callable $callback): Set;
137137
* ```
138138
*
139139
* @param callable(TV): void $callback
140-
* @psalm-return Set<TV>
140+
* @return Set<TV>
141141
*/
142142
public function tap(callable $callback): Set;
143143

@@ -149,7 +149,37 @@ public function tap(callable $callback): Set;
149149
* => [2, 3]
150150
* ```
151151
*
152-
* @psalm-return Set<TV>
152+
* @return Set<TV>
153153
*/
154154
public function tail(): Set;
155+
156+
/**
157+
* Computes the intersection between this set and another set.
158+
*
159+
* ```php
160+
* >>> HashSet::collect([1, 2, 3])
161+
* ->intersect(HashSet::collect([2, 3]))->toArray();
162+
* => [2, 3]
163+
* ```
164+
*
165+
* @param Set<TV>|NonEmptySet<TV> $that the set to intersect with.
166+
* @return Set<TV> a new set consisting of all elements that are both in this
167+
* set and in the given set `that`.
168+
*/
169+
public function intersect(Set|NonEmptySet $that): Set;
170+
171+
/**
172+
* Computes the difference of this set and another set.
173+
*
174+
* ```php
175+
* >>> HashSet::collect([1, 2, 3])
176+
* ->diff(HashSet::collect([2, 3]))->toArray();
177+
* => [1]
178+
* ```
179+
*
180+
* @param Set<TV>|NonEmptySet<TV> $that the set of elements to exclude.
181+
* @return Set<TV> a set containing those elements of this
182+
* set that are not also contained in the given set `that`.
183+
*/
184+
public function diff(Set|NonEmptySet $that): Set;
155185
}

tests/Runtime/Interfaces/NonEmptySet/NonEmptySetOpsTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,21 @@ public function testTail(): void
185185
NonEmptyHashSet::collectNonEmpty([1, 2, 3])->tail()->toArray()
186186
);
187187
}
188+
189+
public function testIntersectAndDiff(): void
190+
{
191+
$this->assertEquals(
192+
[2, 3],
193+
NonEmptyHashSet::collectNonEmpty([1, 2, 3])
194+
->intersect(HashSet::collect([2, 3]))
195+
->toArray()
196+
);
197+
198+
$this->assertEquals(
199+
[1],
200+
NonEmptyHashSet::collectNonEmpty([1, 2, 3])
201+
->diff(HashSet::collect([2, 3]))
202+
->toArray()
203+
);
204+
}
188205
}

tests/Runtime/Interfaces/Set/SetOpsTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,21 @@ public function testTail(): void
192192
HashSet::collect([1, 2, 3])->tail()->toArray()
193193
);
194194
}
195+
196+
public function testIntersectAndDiff(): void
197+
{
198+
$this->assertEquals(
199+
[2, 3],
200+
HashSet::collect([1, 2, 3])
201+
->intersect(HashSet::collect([2, 3]))
202+
->toArray()
203+
);
204+
205+
$this->assertEquals(
206+
[1],
207+
HashSet::collect([1, 2, 3])
208+
->diff(HashSet::collect([2, 3]))
209+
->toArray()
210+
);
211+
}
195212
}

0 commit comments

Comments
 (0)