mixin Amethyst :: Core :: Enumerable[T]
requires def each (&block: (T) -> Void): Void
Enumerable[T] gives any type that can iterate its elements the full set of collection
operations below.
Amethyst types include it explicitly and implement each:
def each (&block: (Int) -> Void): Void
.NET types that implement IEnumerable<T> get it automatically. List[String],
HashSet[Int] and any C# collection support map, select and the rest with no
wrapping:
puts names.map { | name | name.to_upper }.first
Signature
Description
each_with_index(&block: (T, Int) -> Void): Void
Yields each element with its zero-based index.
each_slice(size: Int, &block: (Array[T]) -> Void): Void
Yields consecutive, non-overlapping groups of size elements; the last may be shorter.
each_cons(size: Int, &block: (Array[T]) -> Void): Void
Yields every run of size consecutive elements (a sliding window).
Signature
Description
map[R](&block: (T) -> R): Array[R]
The result of the block for each element.
flat_map[R](&block: (T) -> Array[R]): Array[R]
Like map, then flattens one level.
reduce[A](initial: A, &block: (A, T) -> A): A
Folds the elements into an accumulator, starting from initial.
group_by[K](&block: (T) -> K): Hash[K, Array[T]]
Groups elements by the block’s result.
zip[U](other: Enumerable[U]): Array[Tuple[T, U]]
Pairs elements by position; stops at the shorter sequence.
Signature
Description
select(&block: (T) -> Bool): Array[T]
Elements for which the block returns true.
reject(&block: (T) -> Bool): Array[T]
Elements for which the block returns false.
take(count: Int): Array[T]
The first count elements.
drop(count: Int): Array[T]
Everything after the first count elements.
Signature
Description
any?(&block: (T) -> Bool): Bool
true if the block is true for at least one element.
all?(&block: (T) -> Bool): Bool
true if the block is true for every element (and for an empty collection).
none?(&block: (T) -> Bool): Bool
true if the block is true for no element.
count: Int
The number of elements.
first: T?
The first element, or nil if empty.
last: T?
The last element, or nil if empty.
These require elements, or the block’s result, to be
Comparable .
Signature
Description
min: T?
The smallest element, or nil if empty.
max: T?
The largest element, or nil if empty.
min_by[K](&block: (T) -> K): T?
The element with the smallest block result.
max_by[K](&block: (T) -> K): T?
The element with the largest block result.
sort: Array[T]
The elements in ascending order.
sort_by[K](&block: (T) -> K): Array[T]
The elements ordered by the block’s result.
Signature
Description
to_array: Array[T]
The elements as a new array.
to_hash[K, V]: Hash[K, V]
For an enumerable of Tuple[K, V] pairs, a hash of those pairs.
These are implemented but not yet part of the
standard library specification .
Signature
Description
each(&block: (T) -> Void): Void
Yields each element.
any?: Bool
true if there is at least one element.
empty?: Bool
true if there are no elements.
includes?(item: T): Bool
true if an element equals item.
count(&block: (T) -> Bool): Int
The number of elements for which the block returns true.
reduce(&block: (T, T) -> T): T
Folds the elements, starting from the first. Raises on an empty collection.
reverse: Array[T]
The elements in reverse order.
sum: Int
The sum of an Int enumerable.
sum: Float
The sum of a Float enumerable.
join(separator: String): String
The elements converted with to_s and joined with separator.
For an enumerable of a value type, such as Array[Int], first, last, min, max,
min_by and max_by return a nullable value (Int?), which is nil when the
collection is empty.
These [] overloads extend arrays, strings and hashes:
Receiver
Signature
Description
Array[T]
[](index: System::Index): T
The element at index; ^1 is the last element.
Array[T]
[](range: Range): Array[T]
The elements in an integer range.
Array[T]
[](range: System::Range): Array[T]
The elements in a range with a ^ bound.
String
[](index: System::Index): Char
The character at index.
String
[](range: Range): String
The characters in an integer range.
String
[](range: System::Range): String
The characters in a range with a ^ bound.
Hash[K, V]
fetch(key: K): V?
The value for key, or nil if there is none.