module Enumerable(T)
Overview
The Enumerable
mixin provides collection classes with several traversal, searching,
filtering and querying methods.
Including types must provide an each
method, which yields successive members
of the collection.
For example:
class Three
include Enumerable(Int32)
def each(&)
yield 1
yield 2
yield 3
end
end
three = Three.new
three.to_a # => [1, 2, 3]
three.select &.odd? # => [1, 3]
three.all? { |x| x < 10 } # => true
Note that most search and filter methods traverse an Enumerable eagerly,
producing an Array
as the result. For a lazy alternative refer to
the Iterator
and Iterable
modules.
Direct including types
Defined in:
chem/core_ext/enumerable.crInstance Method Summary
-
#average(weights : Indexable(Number))
Returns the weighted average of the elements in the collection.
-
#average(weights : Indexable(Number), & : T -> _)
Returns the weighted average of the results of the passed block for each element in the collection.
-
#mean
Returns the arithmetic mean of the elements in the collection.
-
#mean(& : T -> _)
Returns the arithmetic mean of the results of the passed block for each element in the collection.
Instance Method Detail
Returns the weighted average of the elements in the collection.
Raises EmptyError
if the collection is empty or ArgumentError
if
weights has a different number of elements.
Expects all element types to respond to #+
and #/
methods.
[1, 2, 3, 4, 5, 6].average((1..6).to_a) # => 4.333333333333333
(1..6).average((1..6).to_a) # => 4.333333333333333
([] of Int32).average([1, 1, 1]) # raises EmptyError
(1..6).average([1, 1]) # raises ArgumentError
NOTE This method calls .additive_identity
on the element type and
weights' element type to determine the type of the intermediate
sum values.
Returns the weighted average of the results of the passed block for
each element in the collection. Raises EmptyError
if the
collection is empty or ArgumentError
if weights has a different
number of elements.
Expects all element types to respond to #+
and #/
methods.
["Alice", "Bob"].average([7, 2], &.size) # => 4.555555555555555
('a'..'z').average((0..25).to_a, &.ord) # => 4.333333333333333
([] of String).average([1, 1], &.size) # raises EmptyError
["Alice", "Bob"].average([1, 1, 1], &.size) # raises ArgumentError
NOTE This method calls .additive_identity
on the yielded type and
weights' element type to determine the type of the intermediate
sum values.
Returns the arithmetic mean of the elements in the collection.
Raises EmptyError
if the collection is empty.
Expects all element types to respond to #+
and #/
methods.
[1, 2, 3, 4, 5, 6].mean # => 3.5
(1..6).mean # => 3.5
([] of Int32).mean # raises EmptyError
NOTE This method calls .additive_identity
on the element type to
determine the type of the intermediate sum.
Returns the arithmetic mean of the results of the passed block for
each element in the collection. Raises EmptyError
if the
collection is empty.
Expects all element types to respond to #+
and #/
methods.
["Alice", "Bob"].mean(&.size) # => 4
('a'..'z').mean(&.ord) # => 109.5
([] of String).mean(&.size) # raises EmptyError
NOTE This method calls .additive_identity
on the yielded type to
determine the type of the intermediate sum.