-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
add Base.length(::SkipMissing)
for AbstractArray
#42689
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
base: master
Are you sure you want to change the base?
Conversation
Apart from the potentially unexpected runtime, this seems problematic as it means that calling |
@martinholters could you please elaborate? Calling |
julia> itr = Iterators.Stateful([missing, 1, missing])
Base.Iterators.Stateful{Vector{Union{Missing, Int64}}, Union{Nothing, Tuple{Union{Missing, Int64}, Int64}}}(Union{Missing, Int64}[missing, 1, missing], (missing, 2), 0)
julia> count(!ismissing, skipmissing(itr).x) # proposed definition of length
1
julia> count(!ismissing, skipmissing(itr).x)
0 |
Ah, I see. The only way I can think of would be to call |
Maybe we could restrict the signature to |
I agree. The operation is O(N) but that's also the case for strings so I don't see that as an argument not to implement it. |
153cce8
to
2512934
Compare
Define `length(::SkipMissing)` with `O(N)` complexity.
`length(::SkipMissing)`, as defined, can actually change the length of some objects (like stateful iterators [1]). Thus, restrict the method to work only on `AbstractArray`. [1] `length(::SkipMissing)` changing the length of a `Stateful` iterator. ``` julia> itr = Iterators.Stateful([missing, 1, missing]) Base.Iterators.Stateful{Vector{Union{Missing, Int64}}, Union{Nothing, Tuple{Union{Missing, Int64}, Int64}}}(Union{Missing, Int64}[missing, 1, missing], (missing, 2), 0) julia> count(!ismissing, skipmissing(itr).x) # proposed definition of length 1 julia> count(!ismissing, skipmissing(itr).x) 0 ```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
@thazhemadam - when this PR gets merged could you also open a follow-up PR to Compat.jl to ensure backward compatibility? Thank you!
Yes, I can do that. 🙂 |
Base.length(::SkipMissing)
Base.length(::SkipMissing)
for AbstractArray
Add test checking if a `MethodError` is thrown while trying to use `length(::SkipMissing)` with a Stateful iterator.
Contractually, length should probably be O(1) though; and we hope to fix that for String at some point. |
I think |
Is that really possible? Note that we also define Marking for triage to get a decision. |
This feels weird to me but it's hard to point to a real problem it causes, so might as well? |
Add
Base.length
definition forSkipMissing
.Resolves #42672.
Before this change:
With this change: