Iterable is meant to use as a mixin for classes that implements the iterator protocol. It provides some useful methods to iterate through the elements of the iterable.

Methods

accumulate(val, op)

Compute the left fold of the iterable with operator op. op takes two parameters, an accumulator and a new value, and produces a new accumulator. The initial accumulator is val. If the iterable is empty, val is returned.

IO.println([1, 2, 3].accumulate(4) {|a, b| a + b }) #> 10

See also: reduce(op)

all(predicate)

Return true if, for all the elements of the iterable, predicate returns true. If predicate returns a value that is false, then the iteration stops and returns that value. Otherwise, return true.

IO.println([ 1, 2, 3 ].all {|n| n > 2 }) #> false
IO.println([ 1, 2, 3 ].all {|n| n < 4 }) #> true

any(predicate)

Return true if, for any element of the iterable, predicate returns true. If predicate returns a value that is true, then the iteration stops and returns that value. Otherwise return false. It is the contrary of none(predicate).

IO.println([ 1, 2, 3 ].any {|n| n < 1 }) #> false
IO.println([ 1, 2, 3 ].any {|n| n > 2 }) #> true

contains(value)

Return true is the iterable contains an element equal to value.

IO.println([ 1, 2, 3 ].contains(2)) #> true
IO.println([ 1, 2, 3 ].contains(4)) #> false

count(predicate)

Return the number of elements in the iterable that satisfies predicate.

IO.println([ 1, 2, 3 ].count {|n| n > 2 }) #> 1
IO.println([ 1, 2, 3 ].count {|n| n < 4 }) #> 3

each(fn)

Iterate through the iterable applying fn to each element of the iterable.

[ 1, 2, 3 ].each {|n| IO.print(n) } #> 123
IO.println()

empty

Return true if the iterable has no elements. It is often more efficient to call this method instead of comparing size to zero.

IO.println([].empty) #> true
IO.println((42..42).empty) #> false

See also: size

join()

Create a String by concatenating all the elements of the iterable. The elements are converted to strings using to_s.

IO.println([1, 2, 3].join()) #> 123

join(sep)

Create a String by concatenating all the elements of the iterable, separated by sep. The elements are converted to strings using to_s. sep can be a String or a Char.

IO.println([1, 2, 3].join('|')) #> 1|2|3

min

Return the minimum of the iterable according to the < operator.

IO.println([3, 1, 4, 2].min) #> 1

See also: max

map(fn)

Create a lazy iterable that applies fn to each element of the iterable.

def doubles = [1, 2, 3].map {|n| n * 2 }
for (n in doubles) {
  IO.println(n)
}
#> 2
#> 4
#> 6

max

Return the maximum of the iterable according to the < operator.

IO.println([3, 1, 4, 2].max) #> 4

See also: min

none(predicate)

Return true if, for all element of the iterable, predicate returns false. If predicate returns a value that is true, then the iteration stops and returns the negation of that value. Otherwise return true. It is the contrary of any(predicate).

IO.println([ 1, 2, 3 ].none {|n| n < 1 }) #> true
IO.println([ 1, 2, 3 ].none {|n| n > 2 }) #> false

product

Return the product of the iterable according to the * operator.

IO.println([3, 1, 4, 2].product) #> 24

See also: sum

reduce(op)

Compute the left fold of the iterable with operator op. op takes two parameters, an accumulator and a new value, and produces a new accumulator. The initial accumulator is the first element of the iterable. It is a runtime error to call this method on an empty iterable.

IO.println([1, 2, 3].reduce {|a, b| a + b }) #> 6

size

Return the number of elements in the iterable by iterating the whole iterable. This method may be overriden if it can be computed more efficiently.

IO.println((1..42).size) #> 42

See also: empty

skip(count)

Create a lazy iterable with the count first elements of the iterable skipped.

def seq = [1, 2, 3].skip(1)
for (item in seq) {
  IO.println(item)
}
#> 2
#> 3

sorted(comp), sorted()

Return true if the elements of the iterable are sorted according to comparator comp. If comp is not provided, < is used to compare elements.

IO.println([1, 2, 3, 4].sorted()) #> true
IO.println([4, 2, 3, 1].sorted {|lhs,rhs| lhs > rhs }) #> false

sum

Return the sum of the iterable according to the + operator.

IO.println([3, 1, 4, 2].sum) #> 10

See also: product

take(count)

Create a lazy iterable with only the count first elements of the iterable.

def seq = [1, 2, 3].take(2)
for (item in seq) {
  IO.println(item)
}
#> 1
#> 2

to_a

Force the evaluation of the whole iterable and put the result of the iteration in an Array.

IO.println((2..5).to_a) #> [2, 3, 4, 5]

uniq()

Create a lazy iterable where only the first occurrence of consecutive equal items are kept.

IO.println([1, 1, 2, 2, 2, 3, 4, 4].uniq().to_a) #> [1, 2, 3, 4]

where(predicate)

Create a lazy iterable with only the elements for which predicate returns true.

def seq = [1, 2, 3].where {|n| n % 2 != 0 }
for (item in seq) {
  IO.println(item)
}
#> 1
#> 3