4.35. kink/container/SET¶
Helper mod for sets.
4.35.1. type set¶
A set is a container of unique elements.
Uniqueness or equivalence of elements is defined by each set implementation.
Set implementations are provided in the following builtin mods:
• kink/container/HASH_SET
• kink/container/FLAT_SET (is also an ordered_set)
• kink/container/TREE_SET (is also an ordered_set)
4.35.1.1. Set.push(Elem)¶
Set.push inserts an Elem to the Set if the Set does not contain the Elem.
If the set contains an element equivalent to the Elem, it depends on implementation whether the element is replaced by Elem or not.
Precondition:
• Equivalence between Elem and elements of the Set must be able to be tested
Example:
:TREE_SET.require_from('kink/container/')
:Set <- TREE_SET.of()
Set.push('foo')
Set.push('bar')
Set.push('foo')
stdout.print_line(Set.repr) # => Tree_set("bar" "foo")
4.35.1.2. Set.have?(Elem)¶
Set.have? returns whether the Set contains an element equivalent to the Elem, or returns false.
Precondition:
• Equivalence between Elem and elements of the Set must be able to be tested
Example:
:FLAT_SET.require_from('kink/container/')
:Set <- FLAT_SET.of('foo' 'bar')
stdout.print_line(Set.have?('foo').repr) # => true
stdout.print_line(Set.have?('xxx').repr) # => false
4.35.1.3. Set.size¶
Set.size returns the size of the Set, which is the number of elements.
Example:
:TREE_SET.require_from('kink/container/')
:Set <- TREE_SET.of('foo' 'bar')
stdout.print_line(Set.size.repr) # => 2
4.35.1.4. Set.empty?¶
Set.empty? returns whether the Set contains no element.
Example:
:TREE_SET.require_from('kink/container/')
stdout.print_line(TREE_SET.of().empty?.repr) # => true
stdout.print_line(TREE_SET.of('foo' 'bar').empty?.repr) # => false
4.35.1.5. Set.op_eq(Another_set)¶
Set.op_eq returns whether the Set contains the equivalent collection of elements to ones of Another_set.
In other words, the method returns whether the Set has all the elements of Another_set, and Another_set has all the elements of the Set.
Preconditions:
• Another_set must be a set
• Equivalence of elements must be defined as the same in the two sets
Example:
:TREE_SET.require_from('kink/container/')
:X <- TREE_SET.of('foo' 'bar' 'baz')
:Y <- TREE_SET.of('foo' 'bar' 'baz')
:Z <- TREE_SET.of('A' 'B' 'C')
stdout.print_line((X == Y).repr) # => true
stdout.print_line((X == Z).repr) # => false
4.35.1.6. Set.push_each(Eacher)¶
Set.push_each inserts each element to the Set.
Preconditions:
• Eacher must support .each($consume) method, which iterates over the elements of Eacher
• Equivalence between elements of the Set and ones of Eacher must be able to be tested
If the Set contains an element equivalent to one in Eacher, it depends on implementation whether the element is replaced or not.
Example:
:FLAT_SET.require_from('kink/container/')
:Set <- FLAT_SET.of('foo' 'bar')
Set.push_each(['bar' 'baz'])
stdout.print_line(Set.repr) # => Flat_set("bar" "baz" "foo")
4.35.1.7. Set.remove(Elem)¶
Set.remove removes an equivalent element to the Elem when the Set contains it. If the Set does not contain an element equivalent to the Elem, this method does nothing.
Precondition:
• Equivalence between Elem and elements of the Set must be able to be tested
Example:
:FLAT_SET.require_from('kink/container/')
:Set <- FLAT_SET.of('foo' 'bar' 'baz')
Set.remove('foo')
Set.remove('xxx')
stdout.print_line(Set.repr) # => Flat_set("bar" "baz")
4.35.1.8. Set.clear¶
Set.clear removes all the elements.
Example:
:FLAT_SET.require_from('kink/container/')
:Set <- FLAT_SET.of('foo' 'bar' 'baz')
Set.clear
stdout.print_line(Set.repr) # => Flat_set()
4.35.1.9. Set.iter¶
Set.iter returns an iter of the elements of the Set.
Example:
:TREE_SET.require_from('kink/container/')
:Set <- TREE_SET.of('foo' 'bar' 'baz')
Set.iter.each{(:E)
stdout.print_line(E.repr)
}
# Output:
# "bar"
# "baz"
# "foo"
4.35.1.10. Set.each($consume_elem)¶
Set.each calls $consume_elem with each element of the Set.
Example:
:TREE_SET.require_from('kink/container/')
:Set <- TREE_SET.of('foo' 'bar' 'baz')
Set.each{(:E)
stdout.print_line(E.repr)
}
# Output:
# "bar"
# "baz"
# "foo"
4.35.1.11. Set.all?($ok?)¶
Set.all? returns whether all the elements of the Set satisfies $ok?.
Example:
:FLAT_SET.require_from('kink/container/')
:Set <- FLAT_SET.of(1 2 3 4 5)
stdout.print_line(Set.all?{(:E) E % 2 == 0 }.repr) # => false
stdout.print_line(Set.all?{(:E) E < 10 }.repr) # => true
stdout.print_line(Set.all?{(:E) E >= 10 }.repr) # => false
4.35.1.12. Set.any?($ok?)¶
Set.any? returns whether at least one element of the Set satisfies $ok?.
Example:
:FLAT_SET.require_from('kink/container/')
:Set <- FLAT_SET.of(1 2 3 4 5)
stdout.print_line(Set.any?{(:E) E % 2 == 0 }.repr) # => true
stdout.print_line(Set.any?{(:E) E < 10 }.repr) # => true
stdout.print_line(Set.any?{(:E) E >= 10 }.repr) # => false
4.35.1.13. Set.count($counted?)¶
Set.count returns the number of elements of the Set which satisfy $counted?.
Example:
:FLAT_SET.require_from('kink/container/')
:Set <- FLAT_SET.of(1 2 3 4 5)
:Even_count <- Set.count{(:E) E % 2 == 0 }
stdout.print_line(Even_count.repr) # => 2
4.35.1.14. Set.fold(Init $combine)¶
Set.fold accumulates elements of the Set seeded by Init.
If the elements of the Set is E1, E2, ,,, En_1, En, in any order, this fun returns combine(combine(,,,combine(combine(Init E1) E2),,, En_1) En).
Example:
:TREE_SET.require_from('kink/container/')
:Set <- TREE_SET.of(1 2 3 4 5)
:N <- Set.fold(100){(:X :Y) X + Y }
stdout.print_line(N.repr) # => 115
4.35.1.15. Set.reduce($combine)¶
Set.reduce accumulates elements of the Set mutually, then returns a maybe tuple.
If the Set has elements E1, E2, E3, ,,, En_1, En, in any order, this method returns [combine(combine(,,,combine(combine(E1 E2) E3),,, En_1) En)].
If the Set has a single element E, this method returns [E].
If the Set is empty, this method returns [].
Example:
:TREE_SET.require_from('kink/container/')
:Empty <- TREE_SET.of()
:Non_empty <- TREE_SET.of(1 2 3 4 5)
stdout.print_line(Empty.reduce{(:X :Y) X + Y}.repr) # => []
stdout.print_line(Non_empty.reduce{(:X :Y) X + Y}.repr) # => [15]
4.35.1.16. Set.have_all?(For_aller)¶
Set.have_all? returns whether the Set has all the elements of For_aller.
Preconditions:
• For_aller must implement .all?($match?).
• All the elements of For_aller must be able to be tested by Set.have?
Example:
:FLAT_SET.require_from('kink/container/')
:Set <- FLAT_SET.of('foo' 'bar' 'baz')
stdout.print_line(Set.have_all?(['foo' 'bar']).repr) # => true
stdout.print_line(Set.have_all?(['foo' 'X']).repr) # => false
stdout.print_line(Set.have_all?(['X' 'Y' 'Z']).repr) # => false
4.35.1.17. Set.have_any?(For_anyer)¶
Set.have_any? returns whether Set has any element of For_anyer.
Preconditions:
• For_anyer must implement .any?($match?).
• All the elements of For_anyer must be able to be tested by Set.have?
Example:
:FLAT_SET.require_from('kink/container/')
:Set <- FLAT_SET.of('foo' 'bar' 'baz')
stdout.print_line(Set.have_any?(['foo' 'bar']).repr) # => true
stdout.print_line(Set.have_any?(['foo' 'X']).repr) # => true
stdout.print_line(Set.have_any?(['X' 'Y' 'Z']).repr) # => false
4.35.2. SET.is?(Val)¶
SET.is? returns whether the Val is a set.