Return self in implementations of "each"

This is more useful than different implementations returning different
(dynamic) types.
parent ba9df4f1
Pipeline #99698815 passed with stages
in 14 minutes and 37 seconds
......@@ -129,7 +129,7 @@ impl Array!(T) {
# Array.new(10, 20, 30).each do (number) {
# stdout.print(number)
# }
def each(block: do (T)) {
def each(block: do (T)) -> Self {
let mut index = 0
let max = length
......@@ -140,6 +140,8 @@ impl Array!(T) {
index += 1
}
self
}
# Yields all the values and their indexes in this `Array` to the supplied
......@@ -154,7 +156,7 @@ impl Array!(T) {
# Array.new(10, 20, 30).each_with_index do (number, index) {
# stdout.print(index) # => 0, 1, 2
# }
def each_with_index(block: do (T, Integer)) {
def each_with_index(block: do (T, Integer)) -> Self {
let mut index = 0
each do (value: T) {
......
......@@ -153,7 +153,7 @@ impl ByteArray {
# ByteArray.new(10, 20, 30).each do (byte) {
# stdout.print(byte)
# }
def each(block: do (Integer)) {
def each(block: do (Integer)) -> Self {
let mut index = 0
let max = length
......@@ -164,6 +164,8 @@ impl ByteArray {
index += 1
}
self
}
# Yields all the values and their indexes in this `ByteArray` to the supplied
......@@ -179,7 +181,7 @@ impl ByteArray {
# ByteArray.new(10, 20, 30).each_with_index do (byte, index) {
# stdout.print(index) # => 0, 1, 2
# }
def each_with_index(block: do (Integer, Integer)) {
def each_with_index(block: do (Integer, Integer)) -> Self {
let mut index = 0
each do (value) {
......
......@@ -220,10 +220,12 @@ trait Iterator!(T) {
# Array.new(10, 20, 30).iter.each do (number) {
# stdout.print(number)
# }
def each(block: do (T)) {
def each(block: do (T)) -> Self {
{ next? }.while_true {
block.call(next!)
}
self
}
# Returns the first value for which the supplied `Block` returns `True`.
......
......@@ -265,10 +265,12 @@ object Map!(K: Hash + Equal, V) {
# key # => 'name'
# value # => 'Alice'
# }
def each(block: do (K, V)) {
def each(block: do (K, V)) -> Self {
iter.each do (pair) {
block.call(pair.key, pair.value)
}
self
}
# Returns an `Iterator` that iterates over all key-value pairs in this
......
......@@ -80,10 +80,12 @@ object Set!(V: Hash + Equal) {
# set.each do (value) {
# value # => 10, 20
# }
def each(block: do (V)) {
def each(block: do (V)) -> Self {
@map.each do (key, _) {
block.call(key)
}
self
}
# Returns an `Iterator` over the values in this `Set`.
......
......@@ -64,11 +64,11 @@ test.group('std::array::Array.each') do (g) {
g.test('Iterating over the values of an Array') {
let input = Array.new(10, 20, 30)
let output = Array.new
input.each do (number) {
let returned = input.each do (number) {
output.push(number)
}
assert.equal(returned, input)
assert.equal(output, Array.new(10, 20, 30))
}
}
......@@ -79,11 +79,12 @@ test.group('std::array::Array.each_with_index') do (g) {
let output = Array.new
let indexes = Array.new
input.each_with_index do (number, index) {
let returned = input.each_with_index do (number, index) {
output.push(number)
indexes.push(index)
}
assert.equal(returned, input)
assert.equal(output, Array.new(10, 20, 30))
assert.equal(indexes, Array.new(0, 1, 2))
}
......
......@@ -77,10 +77,11 @@ test.group('std::byte_array::ByteArray.each') do (g) {
let input = ByteArray.new(10, 20, 30)
let output = Array.new
input.each do (byte) {
let returned = input.each do (byte) {
output.push(byte)
}
assert.equal(returned, input)
assert.equal(output, Array.new(10, 20, 30))
}
}
......@@ -91,11 +92,12 @@ test.group('std::byte_array::ByteArray.each_with_index') do (g) {
let values = Array.new
let indexes = Array.new
input.each_with_index do (byte, index) {
let returned = input.each_with_index do (byte, index) {
values.push(byte)
indexes.push(index)
}
assert.equal(returned, input)
assert.equal(values, Array.new(10, 20, 30))
assert.equal(indexes, Array.new(0, 1, 2))
}
......
......@@ -59,10 +59,11 @@ test.group('std::iterator::Iterator.each') do (g) {
let iter = SimpleIterator.new
let values = Array.new
iter.each do (value) {
let returned = iter.each do (value) {
values.push(value)
}
assert.equal(returned, iter)
assert.equal(values, Array.new(10, 20, 30))
}
......
......@@ -341,11 +341,10 @@ test.group('std::map::Map.empty?') do (g) {
test.group('std::map::Map.each') do (g) {
g.test('Iterating over the key-value pairs of an empty Map') {
let mut iters = 0
let map = Map.new
let returned = map.each do (_, _) { iters += 1 }
Map.new.each do (_, _) {
iters += 1
}
assert.equal(returned, map)
assert.equal(iters, 0)
}
......@@ -357,11 +356,12 @@ test.group('std::map::Map.each') do (g) {
map[1] = 10
map[2] = 20
map.each do (key, val) {
let returned = map.each do (key, val) {
key_total += key
val_total += val
}
assert.equal(returned, map)
assert.equal(key_total, 3)
assert.equal(val_total, 30)
}
......
......@@ -35,10 +35,11 @@ test.group('std::set::Set.each') do (g) {
set.insert(1)
set.insert(2)
set.each do (value) {
let returned = set.each do (value) {
total += value
}
assert.equal(returned, set)
assert.equal(total, 3)
}
}
......
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