Skip to content

Commit 153cce8

Browse files
committed
missing: add length(::SkipMissing)
1 parent 9cad1e0 commit 153cce8

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
@@ -428,6 +428,17 @@ end
428428
@test collect(x) == [1, 2, 4]
429429
@test collect(x) isa Vector{Int}
430430

431+
@testset "length" begin
432+
allmiss = Vector{Union{Int,Missing}}(missing, 10)
433+
@test (lengthskipmissing)(allmiss) == 0
434+
435+
somemiss = [1, missing, 2, 3, 4, missing, 5, 6, 7, 8, 9, 10]
436+
@test (lengthskipmissing)(somemiss) == 10
437+
438+
nomiss = rand(1:10, 10)
439+
@test (lengthskipmissing)(nomiss) == 10
440+
end
441+
431442
@testset "indexing" begin
432443
x = skipmissing([1, missing, 2, missing, missing])
433444
@test collect(eachindex(x)) == collect(keys(x)) == [1, 3]

0 commit comments

Comments
 (0)