-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add eachindex() for AbstractString and Associative #11708
Conversation
I'm not sure we want a definition as generic as this. The second value returned by |
@JeffBezanson Right. So would you prefer a definition just for strings, or extending |
Bump! |
I'm not convinced that this should be defined for all iterables. This is currently wrong for lots of base iterables — dict, set, task, zip, … I think that this is something that non-array types should opt into themselves on a case-by-case basis. |
OK, I'll change it to be specific to strings. |
Don't you think there should also be a (different) definition for |
Yes, possibly. I'm trying to think of use-cases where it'd be useful to code in the general instead of just using |
I've updated the PR. Actually, |
👍 LGTM. |
Thanks! I'll merge this today if nobody objects. |
Are there thoughts about the fact that |
It doesn't bother me, really... |
Yeah. I wondered whether deprecating |
Thanks to @mbauman's new docs about interfaces, I realized it would be useful to add |
There's no reason not to allow this. Fixes #11649.
Add eachindex() for AbstractString and Associative
Fixes #11649 which was only about strings, but in the end I didn't see the point in restricting this to strings: it could be useful for any other type which would use unevenly spaced or non-integer indices. Actually, it's very similar to
Enumerate
, but returning the actual indices which can be passed togetindex
(whileEnumerate
returns the element number), and of course not returning the value.The potential issue with this very general approach is that I'm using
next
instead ofnextind
, which is wasteful since we don't use the value. We could restorenextind
, which is currently deprecated for non-strings, if we want to keep this general approach. But in my tests usingnex
ornextind
doesn't make any difference for strings (see below).Either way, it's not very efficient. Calling
eachindex
and indexing the string is about twice slower as iterating over it directly for aUTF8String
. Indeed, it appears thatnext
is not inlined in either case. Is that expected?Another funny result is that with an
ASCIIString
, usingnextind
ineachindex
is actually faster than iterating over the string. This is becausenext
is inlined in that case, but not when iterating. I'm not sure why, but this seems to indicate something is wrong in the standard behavior.And with
nextind
: