Skip to content
Snippets Groups Projects
Unverified Commit 146b462a authored by Yorick Peterse's avatar Yorick Peterse
Browse files

Added Integer.times

This method returns an Iterator that iterates from 0 up to the value of
the integer.
parent 839916ae
No related branches found
No related tags found
No related merge requests found
# Extensions for the `Integer` type that can only be defined later on in the
# bootstrapping process.
import std::iterator::(Enumerator, Iterator)
import std::process
import std::string_buffer::StringBuffer
 
Loading
Loading
@@ -68,4 +69,18 @@ impl Integer {
 
StringBuffer.from_array(characters).to_string
}
# Returns an `Iterator` that iterates from `0` up to the value of `self`.
#
# # Examples
#
# Iterating from `0` to `10`:
#
# 4.times.to_array # => Array.new(0, 1, 2, 3)
def times -> Iterator!(Integer) {
let mut index = -1
let max = self - 1
Enumerator.new(while: { index < max }, yield: { index += 1 })
}
}
Loading
Loading
@@ -345,3 +345,11 @@ test.group('std::integer::Integer.format') do (g) {
}
}
}
test.group('std::ineger::Integer.times') do (g) {
g.test('Iterating from 0 up to the value of an Integer') {
let values = 4.times.to_array
assert.equal(values, Array.new(0, 1, 2, 3))
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment