Add Array.reverse_iter

The method Array.reverse_iter returns an Iterator that iterates over the
values in an Array in reverse order.
parent 4af9354f
Pipeline #107003987 passed with stages
in 15 minutes and 43 seconds
......@@ -28,6 +28,26 @@ impl Array!(T) {
}
}
# Returns an `Iterator` that iterates over all values in `self` in reverse
# order.
#
# # Examples
#
# Iterating over an `Array` in reverse order:
#
# let numbers = Array.new(10, 20, 30)
# let iter = numbers.iter
#
# iter.next # => 30
# iter.next # => 20
# iter.next # => 10
# iter.next # => Nil
def reverse_iter -> Iterator!(T) {
iterator.reverse_index_enumerator(length) do (index) {
self[index]
}
}
# Joins the values of `self` together using the separator.
#
# # Examples
......
......@@ -438,3 +438,43 @@ def index_enumerator!(T)(
}
)
}
# Returns an `Enumerator` that can be used to easily enumerate over an index
# based collection in reverse order.
#
# This `Enumerator` will start the maximum index, and reduce the index until
# reaching the start of the collection. For every value, the supplied block is
# called and its return value will be the value produced by the `Enumerator`.
#
# # Examples
#
# Iterating over an `Array`:
#
# import std::iterator::(self, Enumerator)
#
# let numbers = Array.new(10, 20, 30)
# let enum: Enumerator!(Integer) =
# iterator.reverse_index_enumerator(numbers.length) do (index) {
# numbers[index]
# }
#
# enum.next # => 30
# enum.next # => 20
# enum.next # => 10
def reverse_index_enumerator!(T)(
length: Integer,
block: do (Integer) -> ?T
) -> Enumerator!(T) {
let mut index = length - 1
Enumerator.new(
while: { index > -1 },
yield: {
let value = block.call(index)
index -= 1
value
}
)
}
......@@ -200,7 +200,7 @@ test.group('std::array::Array.==') do (g) {
}
test.group('std::array::Array.iter') do (g) {
g.test('Obtaining an Iterator from an Array') {
g.test('Iterating over an Array') {
let numbers = Array.new(10, 20, 30)
let iter = numbers.iter
......@@ -211,6 +211,18 @@ test.group('std::array::Array.iter') do (g) {
}
}
test.group('std::array::Array.reverse_iter') do (g) {
g.test('Iterating over an Array in reverse order') {
let numbers = Array.new(10, 20, 30)
let iter = numbers.reverse_iter
assert.equal(iter.next, 30)
assert.equal(iter.next, 20)
assert.equal(iter.next, 10)
assert.equal(iter.next, Nil)
}
}
test.group('std::array::Array.contains?') do (g) {
g.test('Checking if an Array contains a given value') {
assert.false(Array.new.contains?(10))
......
......@@ -209,3 +209,18 @@ test.group('std::iterator.index_enumerator') do (g) {
assert.equal(enum.next, Nil)
}
}
test.group('std::iterator.reverse_index_enumerator') do (g) {
g.test('Iterating over an Array in reverse order') {
let numbers = Array.new(10, 20, 30)
let enum: Enumerator!(Integer) =
iterator.reverse_index_enumerator(numbers.length) do (index) {
numbers[index]
}
assert.equal(enum.next, 30)
assert.equal(enum.next, 20)
assert.equal(enum.next, 10)
assert.equal(enum.next, Nil)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment