3.31. kink/container/SET

Helper mod for sets.

3.31.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.nonempty?

Set.nonempty? returns whether the Set contains one or more elements.

Example:

:TREE_SET.require_from('kink/container/')

stdout.print_line(TREE_SET.of().nonempty?.repr) # => false
stdout.print_line(TREE_SET.of('foo' 'bar').nonempty?.repr)  # => true

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.op_ne(Another_set)

Set.op_ne returns whether the Set does not contain the equivalent collection of elements to ones of Another_set.

In other words, the method returns whether the Set does not have all the elements of Another_set, or Another_set does not have 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)  # => false
stdout.print_line((X != Z).repr)  # => true

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.31.2. SET.set?(Val)

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

3.31.3. type ordered_set

Ordered_set is a subtype of set which supports ordering of elements.

The ordering is defined by each ordered_set implementation.

The following builtin mods provide ordered_set implementations:

• kink/container/FLAT_SET

• kink/container/TREE_SET

Ordered_set.each($consume_elem)

Ordered_set.each calls $consume_elem with each element of the Ordered_set, in the ordering of the set.

This method suffices the contract of Set.each.

Ordered_set.iter

Ordered_set.iter returns an iter which iterates over all the elements in the ordering of the set.

This method suffices the contract of Set.iter.

Ordered_set.fold(Init $combine)

Ordered_set.fold accumulates elements of the Ordered_set seeded by Init.

If the elements of the Ordered_set is E1, E2, ,,, En_1, En, in the ordering of the set, this fun returns combine(combine(,,,combine(combine(Init E1) E2),,, En_1) En).

This method suffices the contract of Ordered_set.fold.

Ordered_set.reduce($combine)

Ordered_set.reduce accumulates elements of the Ordered_set mutually, then returns a maybe tuple.

If the Ordered_set has elements E1, E2, E3, ,,, En_1, En, in the ordering of the set, this method returns [combine(combine(,,,combine(combine(E1 E2) E3),,, En_1) En)].

If the Ordered_set has a single element E, this method returns [E].

If the Ordered_set is empty, this method returns [].

This method suffices the contract of Ordered_set.reduce.

Ordered_set.front

Ordered_set.front returns the element which comes first in the ordering of the set.

Precondition:

• The Ordered_set must not be empty

Example:

:TREE_SET.require_from('kink/container/')

:Set <- TREE_SET.of('foo' 'bar' 'baz')
stdout.print_line(Set.front.repr) # => "bar"

Ordered_set.back

Ordered_set.back returns the element which comes last in the ordering of the set.

Precondition:

• The Ordered_set must not be empty

Example:

:TREE_SET.require_from('kink/container/')

:Set <- TREE_SET.of('foo' 'bar' 'baz')
stdout.print_line(Set.back.repr) # => "foo"

Ordered_set.pop_front

Ordered_set.pop pops the element which comes first in the ordering of the set.

Precondition:

• The Ordered_set must not be empty.

Example:

:FLAT_SET.require_from('kink/container/')

:Set <- FLAT_SET.of('foo' 'bar' 'baz')
:Front <- Set.pop_front
stdout.print_line(Front.repr) # => "bar"
stdout.print_line(Set.repr)   # => Flat_set("baz" "foo")

Ordered_set.pop_back

Ordered_set.pop_back pops the element which comes last in the ordering of the set.

Precondition:

• The Ordered_set must not be empty.

Example:

:FLAT_SET.require_from('kink/container/')

:Set <- FLAT_SET.of('foo' 'bar' 'baz')
:Back <- Set.pop_back
stdout.print_line(Back.repr)  # => "foo"
stdout.print_line(Set.repr)   # => Flat_set("bar" "baz")

Ordered_set.iter_from(Min)

Ordered_set.iter_from returns an iter which iterates over elements equivalent to or bigger than Min in the ordering of the set.

Precondition:

• Ordering between Min and elements of the Ordered_set must be able to be tested

Example:

:TREE_SET.require_from('kink/container/')

:Set <- TREE_SET.of('foo' 'bar' 'baz' 'qux')

Set.iter_from('baz').each{(:E) stdout.print_line(E.repr) }
# Output:
#   "baz"
#   "foo"
#   "qux"

Set.iter_from('bazzz').each{(:E) stdout.print_line(E.repr) }
# Output:
#   "foo"
#   "qux"

Set.iter_from('foo').each{(:E) stdout.print_line(E.repr) }
# Output:
#   "foo"
#   "qux"

Set.iter_from('foooo').each{(:E) stdout.print_line(E.repr) }
# Output:
#   "qux"

3.31.4. SET.ordered_set?(Val)

SET.ordered_set? returns whether the Val is an ordered_set.