countfrom
countfrom(start=1, step=1)
An iterator that counts forever, starting at start
and incrementing by step
.
Examples
-
Count from a specific start value with default step:
julia> c = countfrom(5); julia> collect(take(c, 5)) 5-element Array{Int64,1}: 5 6 7 8 9
In this example,
countfrom(5)
generates an iterator that counts forever, starting at 5 and incrementing by the default step of 1. We usetake
to limit the number of values collected to 5. -
Count from a specific start value with a custom step:
julia> c = countfrom(10, 2); julia> collect(take(c, 4)) 4-element Array{Int64,1}: 10 12 14 16
Here,
countfrom(10, 2)
generates an iterator that starts at 10 and increments by a step of 2. We usetake
to collect the first 4 values from the iterator. - Infinite counting from the default start and step:
julia> c = countfrom(); julia> collect(take(c, 3)) 3-element Array{Int64,1}: 1 2 3
This example demonstrates the default behavior of
countfrom()
, which starts at 1 and increments by 1. We usetake
to collect the first 3 values from the infinite iterator.
Common mistake example:
julia> c = countfrom(5);
julia> collect(c) # Mistake: Collecting from an infinite iterator without limiting it
In this example, the mistake is trying to collect all values from an infinite iterator. It results in an infinite loop, and the program may hang or run out of memory. Always use a function like take
to limit the number of values collected from an infinite iterator.
See Also
countfrom, cycle, done, drop, eachindex, enumerate, first, repeated, rest, start, svds, take, vecdot, vecnorm, zip,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.