Length(skipmissing( )) not implemented

julia> x = [2.0, missing, 3.0];

julia> length(x)
3

julia> length(skipmissing(x))
ERROR: MethodError: no method matching length(::Base.SkipMissing{Vector{Union{Missing, Float64}}})

julia> count(a -> true, skipmissing(x))
2

it would be nice if length is implemented for SkipMissing. Using count in this case is less readable and counter intuitive.

There is an issue for this here.

The problem is that length will always be O(n) for SkipMissing. It would be very slow, which is against the design of skipmissing in the first place.

3 Likes

thanks.

but the situation is quite strange: if O(n) is the best we can possibly get, why hesitate to do so? After all, O(n) should be considered as “very good” for most algorithms…

now, using count is still O(n) (and with some overhead in the anonymous function). We just cannot avoid it…

I don’t think O(n) is very good for something like length, which is O(1) for vectors. The consequence of supporting, out of the box, length is that a lot of people will complain that length(skipmissing(x)) is slow, and they would be right, when thinking about other length calls.

2 Likes

… but the point is there is no way to achieve O(1) for length(skipmissing()), in my understanding at least.

you want users to be aware they are doing this, where other wise they may be calling length() over and over like they would normally do.

1 Like

skipmissing is designed to be a very lightweight and fast iterator which skips missing values. It comes with a guarantee “every function in Base which supports SkipMissing will be fast”. That unfortunately means it won’t support all common functions.

3 Likes

Even more readable:

count(!ismissing, x)
4 Likes