Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hack language - Set intersection

Tags:

hacklang

The Hack Set has a difference method but I don't see a method called intersect or any similar one.

How to get an intersection of two sets?

$set1 = Set { 'a', 'x' };
$set2 = Set { 'b', 'c', 'x', 'y' };
$intersection = ??? // Set { 'x' }

Docs: https://docs.hhvm.com/hack/reference/class/HH.Set/

like image 940
Martin Konicek Avatar asked Jul 14 '26 07:07

Martin Konicek


1 Answers

I'm not sure why that function doesn't exist, but you can put this code in some utility function:

  $intersection = Set {};
  foreach ($other as $v) {
    if ($set->contains($v)) {
      $intersection->add($v);
    }
  }

Additionally, you can add a check so you iterate the smaller set.

OR, if you like one liners, this also works :)

$set1->filter($x ==> $set2->contains($x))
like image 187
Gareve Avatar answered Jul 21 '26 22:07

Gareve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!