List Complement Operation


#1

I was trying to find the complement of 2 lists (A not in B). I saw several List methods that appeared like they might work (remove_list_same, removing_same)… but they weren’t working on my List{Integer} and I noticed the comment:

// Removes all *equivalent* items - exact same object, not necessarily logically equal (=)
// - in specified list from this list.

So I hacked this up, specifically for my scenario (something that runs very infrequently on Lists of integers whose lengths are relatively small):

!list_int_diff : (List{Integer} l1, List{Integer} l2) List{Integer}
[
   // remove l2 from l1
  l2.length.do
  [
    l1.remove?[item = l2.at(idx)]
  ]
  l1
]
list_int_diff({0 1 2 3 4 5 6}, {1,5}) // returns {0, 2, 3, 4, 6}
list_int_diff({1,5,7}, {0 1 2 3 4 5 6}) // returns {7}

I was curious if there were any more :sk: ways of doing a list complement.