Interface Collection

A collection of elements.

Interface summary


do [block] -> none
Invokes the given block on each element of this instance.
size -> int
The number of elements in this instance.
operator == other/Collection -> bool
Whether this instance is equal to other.
is-empty -> bool
Whether this instance is empty.
every [predicate] -> bool
Whether all elements in the collection satisfy the given predicate.
any [predicate] -> bool
Whether at least one element in the collection satisfies the given predicate.
contains element/any -> bool
Whether this instance contains the given element.
reduce [block] -> any
Takes all elements and combines them using block.
reduce --initial/any [block] -> any
Takes all elements and combines them using block.

Methods

any [predicate] -> bool
Whether at least one element in the collection satisfies the given predicate.
Returns false, if the collection is empty.

contains element/any -> bool
Whether this instance contains the given element.

do [block] -> none
Invokes the given block on each element of this instance.
Users must not modify the collection while iterating over it.
Inheritance
Needs to be implemented by all subclasses.
This function should never throw.
This function does *not* need to protect itself against modifications of this instance during the iteration. Subclasses are, however, encouraged to do so if they can do it cheaply (especially in debug-mode).
Examples

[1, 2].do: debug it  // Prints 1, 2
Categories
  • Iteration

every [predicate] -> bool
Whether all elements in the collection satisfy the given predicate.
Returns true, if the collection is empty.

Whether this instance is empty.
Inheritance
Subclasses should overwrite this method if the size getter is not constant and the subclass has a more efficient way of determining whether this instance is empty.

Whether this instance is equal to other.
Equality only returns true when both operands are of the same type.
Returns false, if this instance and other are not of the same size, or if the contained elements are not equal themselves. Unless otherwise specified or configured, uses the default == operator for comparison.
Subclasses may decide not to support deep equality.
It is an error to compare self-recursive data-structures.
Inheritance
Collections do *not* need to ensure that recursive data structures don't lead to infinite loops.

reduce [block] -> any
Takes all elements and combines them using block.
It's an error if this instance does not have at least one element.

reduce --initial/any [block] -> any
Takes all elements and combines them using block.
Returns the initial value if this instance is empty.
If this instance contains at least one element calls block first with the initial value and the first element.

The number of elements in this instance.
This operation can be assumed to be in O(1).
Inheritance
This method must be implemented by subclasses.
If the operation is not in O(1) consider not implementing the Collection interface. Exceptional cases, where all users are clearly aware of this restriction, are permitted.