6.35. kink/container/SET

6.35.1. type set

`set` is an abstract type of containers of unique elements. Elements must be unique in the `set`, in the equivalence relation defined by the implementation.

`ordered_set` is a subtype of `set` which supports total ordering of elements. See kink/container/ORDERED_SET.

6.35.1.1. Set.have?(Elem)

`have?` returns whether `Set` has an element equivalent to `Elem`.

Precondition

`Elem` must be in the domain of the equivalence relation of elements.

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

6.35.1.2. Set.size

`size` returns the number of elements of `Set` as an integer `num`.

Example

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

:Set <- TREE_SET.of('foo' 'bar')
stdout.print_line(Set.size.repr)  # => 2

6.35.1.3. Set.push(Elem)

`push` inserts `Elem` to `Set`.

If `Set` already has an element equivalent to `Elem`, `push` might either do nothing, or replace the element by `Elem`.

Precondition

`Elem` must be in the domain of the equivalence relation of elements.

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

6.35.1.4. Set.append(Eacher)

`append` inserts elements of `Eacher` to `Set`.

Preconditions

`Eacher` must support .each($consume) method, which calls $consume for each element of `Eacher`.

The elements of `Eacher` must be in the domain of the elements of `Set`.

Example

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

:Set <- FLAT_SET.of('foo' 'bar')

Set.append(['bar' 'baz'])
stdout.print_line(Set.repr) # => (flat_set "bar" "baz" "foo")

6.35.1.5. Set.remove(Elem)

`remove` deletes an element equivalent to `Elem`. If `Set` does not have such an element, `remove` does nothing.

Precondition

`Elem` must be in the domain of the equivalence relation of elements.

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

6.35.1.6. Set.clear

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

6.35.1.7. Set.iter

`iter` method returns an `iter` of the elements of `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"

6.35.1.8. Set.each($consume)

`each` calls $consume for each element of `Set`.

Precondition

$consume must be a fun which takes a value.

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"

6.35.1.9. Set.all?($match?)

`all?` returns whether all the elements of `Set` satisfy the predicate $match?.

Precondition

$match? must be a predicate.

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

6.35.1.10. Set.any?(...[$match?={ true }])

`any?` returns whether one or more elements of `Set` satisfy the predicate $match?.

$match? is an optional arg with a default value { true }. So, if $match? is not passed, `any?` returns whether `Set` is nonempty.

Precondition

$match? must be a predicate.

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

Example

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

stdout.print_line(FLAT_SET.of().any?.repr)    # => false
stdout.print_line(FLAT_SET.of(1).any?.repr)   # => true
stdout.print_line(FLAT_SET.of(1 2).any?.repr) # => true

6.35.1.11. Set.count($counted?)

`count` returns the number of elements of `Set` which satisfy the predicate $counted?, as an integer `num`.

Precondition

$counted? must be a predicate.

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

6.35.1.12. Set.fold(Init $combine)

`fold` folds elements of `Set` seeded by `Init`.

If `Set` has elements E1, E2, ,,, En_1, En, `fold` returns combine(combine(,,,combine(combine(Init E1) E2),,, En_1) En). Elements can appear in any order, unless `Set` is ordered.

If `Set` is empty, `fold` returns `Init`.

Precondition

$combine must be a fun which takes two values.

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

6.35.1.13. Set.reduce($combine ...[$empty_fallback])

`reduce` folds elements of `Set` mutually.

If `Set` has elements E1, E2, E3, ,,, En_1, En, `reduce` returns combine(combine(,,,combine(combine(E1 E2) E3),,, En_1) En). Elements can appear in any order, unless `Set` is ordered.

If `Set` has a single element `E`, `reduce` returns `E`.

If `Set` is empty, and $empty_fallback is given, `reduce` tail-calls $empty_fallback with no arg.

If `Set` is empty, and $empty_fallback is not given, `reduce` raises an exception.

Preconditions

$combine must take two values.

$empty_fallback must be a thunk.

Example

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

:Empty <- TREE_SET.of()
:Non_empty <- TREE_SET.of(1 2 3 4 5)

stdout.print_line(Non_empty.reduce{(:X :Y) X + Y}.repr) # => 15
stdout.print_line(Empty.reduce({(:X :Y) X + Y} { 'empty' }).repr) # => "empty"

6.35.1.14. Set.have_all?(Vals)

`have_all?` returns whether `Set` has all the elements of `Vals`.

Preconditions

`Vals` must implement .all?($match?).

The elements of `Vals` must be in the domain of the equivalence relation of elements of `Set`.

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

6.35.1.15. Set.have_any?(Vals)

`have_any?` returns whether `Set` has any element of `Vals`.

Preconditions

`Vals` must implement .any?($match?).

The elements of `Vals` must be in the domain of the equivalence relation of elements of `Set`.

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

6.35.1.16. Set1 == Set2

`==` operator, or `op_eq` method, returns whether `Set1` and `Set2` have equivalent collections of elements.

In other words, `op_eq` returns whether `Set1` has all the elements of `Set2`, and `Set2` has all the elements of `Set1`.

Preconditions

`Set2` must be a `set`.

The equivalence relations of elements of `Set1` and `Set2` must be the same.

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

6.35.2. SET.is?(Val)

`is?` returns whether `Val` is a `set`.