-
-
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
find* functions and pairs()/keys() #25999
Comments
Very good points. I think functions like |
Well, it sounds useful to be able to pass non-indexable iterators and get the position of the match, even if that's not really an index which can be used with that iterator. For example, you could use Overall it seems to me that we lack collection traits, which would make things much clearer and would allow providing reasonably useful fallbacks for non-indexable iterators. |
I don't agree that traits make things much clearer. With traits, in order to make |
I agree with Jeff here that it doesn't really make sense to implicitly use |
We can deprecate the current behavior of arbitrary iterators to something like
|
Or to: for (i, x) in enumerate(itr)
f(x) && return i
end if we want to preserve the not-found behavior of returning |
Note that while on 0.6 the methods accepted any iterable, in practice they relied on |
find*
functions have been made more general in #24673, and now usepairs
(and thereforekeys
) to get indices forAbstractArray
,AbstractDict
,Tuple
,NamedTuple
andAbstractString
types. ForHasShape
iterators, they use cartesian indices, and for other iterators they use linear indices.julia/base/array.jl
Lines 1500 to 1505 in 11c08ad
However, this scheme suffers from a significant limitation: even if a custom indexable type other than the ones listed above implements
pairs
/keys
, these functions are ignored and the generic iterator fallback will be used instead. This is because there is currently no trait to identify indexable types, so the internal_pairs
function has to be defined on a hardcoded set of types so that we don't try to callkeys
on iterators which may not implement it.Changing this after 1.0 would be technically breaking
pairs
/keys
would suddenly be used for types which implement these functions, and they could return different types from the fallback.Possible solutions to fix this before 1.0:
method_exists(pairs, itr)
. This would work but AFAIK the compiler does not handle this efficiently at this point.Indexable
trait, and callpairs
/keys
for types which implement it. This trait could also be useful in other cases (e.g.broadcast
, see Broadcast had one job (e.g. broadcasting over iterators and generator) #18618)._pairs
function to something else, and recommend that types override it. This doesn't sound like a good approach since it would be mostly redundant withpairs
/keys
.The text was updated successfully, but these errors were encountered: