Method: Immutable::Set#difference

Defined in:
lib/immutable/set.rb

#difference(other) ⇒ Set Also known as: subtract, -

Return a new Set with all the items in other removed. other can be any Enumerable object.

Examples:

Immutable::Set[1, 2] - Immutable::Set[2, 3] # => Immutable::Set[1]

Parameters:

  • other (Enumerable)

    The collection to subtract from this set

Returns:



346
347
348
349
350
351
352
353
# File 'lib/immutable/set.rb', line 346

def difference(other)
  trie = if (@trie.size <= other.size) && (other.is_a?(Immutable::Set) || (defined?(::Set) && other.is_a?(::Set)))
    @trie.select { |key, _| !other.include?(key) }
  else
    @trie.bulk_delete(other)
  end
  new_trie(trie)
end