Set
A generic unordered collection of elements that does not support duplicate elements, and supports adding and removing elements.
Literal for MutableSet is #(1 2 3)
because its hash based
Creating set
set = #(1 2 3 3 3) // 1 2 3
It's possible to create new set from the two others by by addition, subtraction or intersection:
sum = #(1 2 3) + #(3 4) // $(1 2 3 4)
sub = #(1 2 3) - #(4 5) // #()
intersect = #(1 2 3) intersect: #(2 3 4) // #(2 3)
You can also correctly compare sets with ==
!=
#(1 2 3) == #(1 2 3) // true
Accessing elements
You cant access elements by index, since order of elements is not the same.
But you can iterate over set the same way as over list:
#{1 2 3 3 4} forEach: [it echo]
Mutation
set = #(1 2 3)
set add: 4
set clear // #()
Common messages
set = #(1 2 3)
set count // 3
Converting
#(1 1 1 2) toMutableList // {1 2}
#(1 1 1 2) toList // {1 2}
Last modified: 23 October 2024