NAME

Sidef::Types::Set::Set

DESCRIPTION

This class implements an unordered collection of unique elements. A Set automatically eliminates duplicate values and provides efficient membership testing. Sets support mathematical set operations like union, intersection, difference, and symmetric difference.

Sets are useful when you need to track unique items, test membership, or perform set algebra operations. Unlike arrays, sets don't maintain insertion order and don't allow duplicate elements.

SYNOPSIS

var a = Set(1,2,3,4)
var b = Set(2,3,5)

say (a ^ b)     #=> Set(1, 4, 5)
say (a - b)     #=> Set(1, 4)
say (a | b)     #=> Set(1, 2, 3, 4, 5)
say (a & b)     #=> Set(2, 3)

say a.has(2)    #=> true
say a.len       #=> 4

say b.map  {|n| n**2 }  #=> Set(4, 9, 25)
say b.grep {.is_odd}    #=> Set(3, 5)

INHERITS

Inherits methods from:

* Sidef::Types::Hash::Hash

METHODS

&

a & b

Returns the intersection of two sets (elements present in both sets).

var a = Set(1,2,3,4)
var b = Set(3,4,5,6)
say (a & b)     #=> Set(3, 4)

Aliases: , and, intersection

+

a + b

Returns the concatenation of two sets (union operation, same as |).

var a = Set(1,2,3)
var b = Set(4,5,6)
say (a + b)     #=> Set(1, 2, 3, 4, 5, 6)

Aliases: concat

-

a - b

Returns the difference of two sets (elements in the first set but not in the second).

var a = Set(1,2,3,4,5)
var b = Set(3,4,5)
say (a - b)     #=> Set(1, 2)

Aliases: , sub, diff, difference

...

a ... b

Returns a range from the minimum element of set a to the maximum element of set b as a list.

var a = Set(1,2,3)
var b = Set(8,9,10)
say (a ... b)   #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Aliases: to_list

<<

a << b

Adds element b to set a in-place and returns the modified set.

var s = Set(1,2,3)
s << 4
say s           #=> Set(1, 2, 3, 4)

Aliases: add, push, append

^

a ^ b

Returns the symmetric difference of two sets (elements in either set but not in both).

var a = Set(1,2,3,4)
var b = Set(3,4,5,6)
say (a ^ b)     #=> Set(1, 2, 5, 6)

Aliases: xor, symdiff, symmetric_difference

|

a | b

Returns the union of two sets (all elements from both sets).

var a = Set(1,2,3)
var b = Set(3,4,5)
say (a | b)     #=> Set(1, 2, 3, 4, 5)

Aliases: , or, union

a ∋ b

Returns true if set a contains element b, false otherwise.

var s = Set(1,2,3)
say (s ∋ 2)     #=> true
say (s ∋ 5)     #=> false

Aliases: has, exists, has_key, haskey, contain, include, contains, includes

a ∌ b

Returns true if set a does not contain element b, false otherwise.

var s = Set(1,2,3)
say (s ∌ 5)     #=> true
say (s ∌ 2)     #=> false

a ≡ b

Returns true if sets a and b contain exactly the same elements, false otherwise.

var a = Set(1,2,3)
var b = Set(3,2,1)
var c = Set(1,2,4)
say (a ≡ b)     #=> true
say (a ≡ c)     #=> false

a ≤ b

Returns true if set a is a subset of or equal to set b (all elements of a are in b).

var a = Set(1,2)
var b = Set(1,2,3,4)
say (a ≤ b)     #=> true

Aliases: , <=, is_subset

a ≥ b

Returns true if set a is a superset of or equal to set b (all elements of b are in a).

var a = Set(1,2,3,4)
var b = Set(1,2)
say (a ≥ b)     #=> true

Aliases: , >=, is_superset

all

self.all(block)

Returns true if the given block returns true for all elements in the set.

var s = Set(2,4,6,8)
say s.all { .is_even }      #=> true
say s.all { _ > 5 }         #=> false

any

self.any(block)

Returns true if the given block returns true for any element in the set.

var s = Set(1,2,3,4)
say s.any { _ > 3 }         #=> true
say s.any { _ > 10 }        #=> false

collect

self.collect(block)

Returns a new set with the results of running the block for each element (same as map).

var s = Set(1,2,3)
say s.collect { _ * 2 }     #=> Set(2, 4, 6)

contains_all

self.contains_all(*objects)

Returns true if the set contains all of the given objects.

var s = Set(1,2,3,4,5)
say s.contains_all(1,2,3)   #=> true
say s.contains_all(1,2,9)   #=> false

count

self.count(obj)

Returns the number of occurrences of obj in the set (always 0 or 1 for sets).

var s = Set(1,2,3)
say s.count(2)              #=> 1
say s.count(5)              #=> 0

count_by

self.count_by(block)

Returns a hash with counts of elements grouped by the result of the block.

var s = Set(1,2,3,4,5,6)
say s.count_by { .is_even } #=> Hash(true => 3, false => 3)

delete

self.delete(*objects)

Removes the specified objects from the set and returns the modified set.

var s = Set(1,2,3,4,5)
s.delete(2,4)
say s                       #=> Set(1, 3, 5)

Aliases: remove, discard

delete_first_if

self.delete_first_if(block)

Removes the first element for which the block returns true and returns the modified set.

var s = Set(1,2,3,4,5)
s.delete_first_if { _ > 3 }
say s.len                   #=> 4

delete_if

self.delete_if(block)

Removes all elements for which the block returns true and returns the modified set.

var s = Set(1,2,3,4,5,6)
s.delete_if { .is_even }
say s                       #=> Set(1, 3, 5)

dump

self.dump

Returns a string representation of the set suitable for debugging.

var s = Set(1,2,3)
say s.dump                  #=> Set(1, 2, 3)

each

self.each(block)

Iterates over each element in the set, passing it to the block. Returns the set itself.

var s = Set(1,2,3)
s.each { |n|
    say n
}

each_2d

self.each_2d(block)

Iterates over each element in the set with its index, passing both to the block.

var s = Set('a','b','c')
s.each_2d { |i, v|
    say "#{i}: #{v}"
}

grep

self.grep(block)

Returns a new set containing only the elements for which the block returns true.

var s = Set(1,2,3,4,5,6)
say s.grep { .is_even }     #=> Set(2, 4, 6)

Aliases: select

grep_2d

self.grep_2d(block)

Returns a new set of elements for which the block returns true, with elements and indices passed to the block.

var s = Set(10,20,30)
say s.grep_2d { |i, v| v > 15 }   #=> Set(20, 30)

iter

self.iter

Returns an iterator object for the set.

var s = Set(1,2,3)
var it = s.iter
say it.next                 #=> 1
say it.next                 #=> 2

join

self.join(*rest)

Joins the elements of the set into a string, using the first argument as separator.

var s = Set(1,2,3)
say s.join(', ')            #=> "1, 2, 3" (order may vary)

map

self.map(block)

Returns a new set with the results of running the block for each element.

var s = Set(1,2,3)
say s.map { _**2 }          #=> Set(1, 4, 9)

map_2d

self.map_2d(block)

Returns a new set with the results of running the block for each element and its index.

var s = Set('a','b','c')
say s.map_2d { |i, v| "#{i}:#{v}" }

max

self.max

Returns the maximum element in the set.

var s = Set(5,2,8,1,9)
say s.max                   #=> 9

max_by

self.max_by(block)

Returns the element for which the block returns the maximum value.

var s = Set(-5, 3, -10, 7)
say s.max_by { .abs }       #=> -10

min

self.min

Returns the minimum element in the set.

var s = Set(5,2,8,1,9)
say s.min                   #=> 1

min_by

self.min_by(block)

Returns the element for which the block returns the minimum value.

var s = Set(-5, 3, -10, 7)
say s.min_by { .abs }       #=> 3

new

self.new

Creates a new empty set, or a set from the given elements.

var s1 = Set.new
var s2 = Set.new(1,2,3)
var s3 = Set(1,2,3)         # shorthand

Aliases: call

none

self.none(block)

Returns true if the block returns false for all elements in the set.

var s = Set(1,3,5,7)
say s.none { .is_even }     #=> true
say s.none { _ > 5 }        #=> false

pop

self.pop

Removes and returns an arbitrary element from the set.

var s = Set(1,2,3)
say s.pop                   # returns and removes one element
say s.len                   #=> 2

prod

self.prod(block)

Returns the product of all elements, or the product of the results of applying the block to each element.

var s = Set(2,3,4)
say s.prod                  #=> 24
say s.prod { _**2 }         #=> 576

Aliases: prod_by

prod_2d

self.prod_2d(block)

Returns the product of the results of applying the block to each element and its index.

var s = Set(2,3,4)
say s.prod_2d { |i, v| v + i }

shift

self.shift

Removes and returns an arbitrary element from the set (same as pop).

var s = Set(1,2,3)
say s.shift                 # returns and removes one element

sort

self.sort(block)

Returns an array of the set's elements sorted according to the optional comparison block.

var s = Set(3,1,4,1,5)
say s.sort                  #=> [1, 3, 4, 5]
say s.sort { |a,b| b <=> a } #=> [5, 4, 3, 1]

sort_by

self.sort_by(block)

Returns an array of the set's elements sorted by the result of the block.

var s = Set(-5, 3, -10, 7)
say s.sort_by { .abs }      #=> [3, -5, 7, -10]

sum

self.sum(block)

Returns the sum of all elements, or the sum of the results of applying the block to each element.

var s = Set(1,2,3,4)
say s.sum                   #=> 10
say s.sum { _**2 }          #=> 30

Aliases: sum_by

sum_2d

self.sum_2d(block)

Returns the sum of the results of applying the block to each element and its index.

var s = Set(10,20,30)
say s.sum_2d { |i, v| i + v }

to_a

self.to_a

Returns an array containing all elements of the set.

var s = Set(1,2,3)
say s.to_a                  #=> [1, 2, 3] (order may vary)

Aliases: values, to_array

to_bag

self.to_bag

Converts the set to a Bag (multiset) where each element has a count of 1.

var s = Set(1,2,3)
var b = s.to_bag
say b                       #=> Bag(1, 2, 3)

to_set

self.to_set

Returns a copy of the set.

var s1 = Set(1,2,3)
var s2 = s1.to_set
say s2                      #=> Set(1, 2, 3)

SEE ALSO

Sidef::Types::Hash::Hash, Sidef::Types::Array::Array, Sidef::Types::Set::Bag

AUTHOR

Daniel "Trizen" Șuteu

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Sidef itself.