Skip to content
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

Define length(::SkipMissing) #35946

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Standard library changes
------------------------
* The `nextprod` function now accepts tuples and other array types for its first argument ([#35791]).
* The function `isapprox(x,y)` now accepts the `norm` keyword argument also for numeric (i.e., non-array) arguments `x` and `y` ([#35883]).
* `length` now supports `SkipMissing`-wrapped containers ([#35946]).

#### LinearAlgebra

Expand Down
7 changes: 7 additions & 0 deletions base/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ keys(itr::SkipMissing) =
v === missing && throw(MissingException("the value at index $I is missing"))
v
end
function length(itr::SkipMissing)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function length(itr::SkipMissing)
length(itr::SkipMissing) = count(_->true, itr)

i = 0
for _ in itr
i += 1
end
return i
end

function show(io::IO, s::SkipMissing)
print(io, "skipmissing(")
Expand Down
1 change: 1 addition & 0 deletions test/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ end
@testset "skipmissing" begin
x = skipmissing([1, 2, missing, 4])
@test eltype(x) === Int
@test length(x) == 3
@test collect(x) == [1, 2, 4]
@test collect(x) isa Vector{Int}

Expand Down