Julia: enumerate from any index

Created on 15 Nov 2016  路  5Comments  路  Source: JuliaLang/julia

it would be nice to have the ability to enumerate from any index like in python
now:

it = "hello"
for (i,el) in enumerate(it)
@show i
end

shows i=1,2...
would be nice to have:

it = "hello"
for (i,el) in enumerate(it,10)  # or enumerate(10,it)
@show i
end

shows i=10,11...

help wanted

Most helpful comment

Seems like this can be accomplished simply and efficiently with zip(countfrom(n), x). See discussion in linked PR.

All 5 comments

It may also be useful to enumerate only up to a specific index. Perhaps keyword arguments like from= and to= could be used for this, e.g. enumerate(it, from=3, to=10). (Or maybe start and stop.) This functionality could probably be implemented fairly easily using take and drop from Base.Iterators.

Seems like this can be accomplished simply and efficiently with zip(countfrom(n), x). See discussion in linked PR.

Note that starting from a specific index isn't really easy with the current iterators interface: next isn't guaranteed to accept the linear index as its state argument. So a fully generic implementation would have to call next i times to get the element number i. It would be nice to add an ind2state function which could be overridden for types which can offer a more efficient approach. That would also fix https://github.com/JuliaLang/julia/pull/18668 (see https://github.com/JuliaLang/julia/pull/18668#discussion_r80393729).

I would like to work on this issue, please guide me how to proceed.

You implement the function, write tests for it and then make a Pull Request with the changes.

https://github.com/JuliaLang/julia/blob/master/CONTRIBUTING.md is a good read.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

StefanKarpinski picture StefanKarpinski  路  3Comments

yurivish picture yurivish  路  3Comments

sbromberger picture sbromberger  路  3Comments

omus picture omus  路  3Comments

Keno picture Keno  路  3Comments