3.34. kink/container/SET

Helper mod for sets.

3.34.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)

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")

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

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

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

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

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")

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")

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()

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"

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"

Set.for_all?($ok?)

Set.for_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.for_all?{(:E) E % 2 == 0 }.repr)  # => false
stdout.print_line(Set.for_all?{(:E) E < 10 }.repr)    # => true
stdout.print_line(Set.for_all?{(:E) E >= 10 }.repr)   # => false

Set.for_any?($ok?)

Set.for_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.for_any?{(:E) E % 2 == 0 }.repr)  # => true
stdout.print_line(Set.for_any?{(:E) E < 10 }.repr)    # => true
stdout.print_line(Set.for_any?{(:E) E >= 10 }.repr)   # => false

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

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

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]

Set.have_all?(For_aller)

Set.have_all? returns whether the Set has all the elements of For_aller.

Preconditions:

• For_aller must implement .for_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

Set.have_any?(For_anyer)

Set.have_any? returns whether Set has any element of For_anyer.

Preconditions:

• For_anyer must implement .for_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

3.34.2. SET.is?(Val)

SET.is? returns whether the Val is a set.