Skip to content

Commit f2cc861

Browse files
committed
missing: add length(::SkipMissing)
Define `length(::SkipMissing)` with `O(N)` complexity.
1 parent c72625f commit f2cc861

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

base/missing.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ IteratorSize(::Type{<:SkipMissing}) = SizeUnknown()
246246
IteratorEltype(::Type{SkipMissing{T}}) where {T} = IteratorEltype(T)
247247
eltype(::Type{SkipMissing{T}}) where {T} = nonmissingtype(eltype(T))
248248

249+
length(s::SkipMissing) = count(!ismissing, s.x)
250+
249251
function iterate(itr::SkipMissing, state...)
250252
y = iterate(itr.x, state...)
251253
y === nothing && return nothing

test/missing.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,17 @@ end
441441
@test collect(x) == [1, 2, 4]
442442
@test collect(x) isa Vector{Int}
443443

444+
@testset "length" begin
445+
allmiss = Vector{Union{Int,Missing}}(missing, 10)
446+
@test (lengthskipmissing)(allmiss) == 0
447+
448+
somemiss = [1, missing, 2, 3, 4, missing, 5, 6, 7, 8, 9, 10]
449+
@test (lengthskipmissing)(somemiss) == 10
450+
451+
nomiss = rand(1:10, 10)
452+
@test (lengthskipmissing)(nomiss) == 10
453+
end
454+
444455
@testset "indexing" begin
445456
x = skipmissing([1, missing, 2, missing, missing])
446457
@test collect(eachindex(x)) == collect(keys(x)) == [1, 3]

0 commit comments

Comments
 (0)