Skip to content

Commit

Permalink
add emptymissing (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins committed Dec 23, 2022
1 parent d626077 commit d034f67
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Missings"
uuid = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
version = "1.0.2"
version = "1.1.0"

[deps]
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
Expand Down
30 changes: 28 additions & 2 deletions src/Missings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Missings

export allowmissing, disallowmissing, ismissing, missing, missings,
Missing, MissingException, levels, coalesce, passmissing, nonmissingtype,
skipmissings
skipmissings, emptymissing

using Base: ismissing, missing, Missing, MissingException

Expand Down Expand Up @@ -459,7 +459,7 @@ end
Return a vector similar to the array wrapped by the given `SkipMissings` iterator
but skipping all elements with a `missing` value in one of the iterators passed
to `skipmissing` and elements for which `f` returns `false`. This method
only applies when all iterators passed to `skipmissings` are arrays.
only applies when all iterators passed to `skipmissings` are arrays.
# Examples
```
Expand Down Expand Up @@ -488,4 +488,30 @@ function filter(f, itr::SkipMissingsofArrays)
y
end

"""
emptymissing(f)
Return a function that takes at least one positional argument `x` and
returns `missing` if `x` is empty (`isempty(x)` returns `true`)
and otherwise returns `f(x, args...; kwargs...)`, where `args` are following
positional arguments passed to `f` and `kwargs` are its keyword arguments.
# Examples
```
julia> emptymissing(last)([])
missing
julia> emptymissing(last)([1, 2, 3])
3
julia> emptymissing(first)([], 2)
missing
julia> emptymissing(first)([1], 2)
1-element Vector{Int64}:
1
```
"""
emptymissing(f) = (x, args...; kwargs...) -> isempty(x) ? missing : f(x, args...; kwargs...)

end # module
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,15 @@ struct CubeRooter end
@inferred Union{Missing, Int} reduce(+, mx)
end
end

@testset "emptymissing" begin
@test emptymissing(last)([]) === missing
@test emptymissing(last)([1, 2, 3]) === 3
@test emptymissing(sum)(skipmissing(missings(Int, 3))) === missing
@test emptymissing(sum)(skipmissing([1, 2, 3])) === 6
fun(a, b; c) = (b, c)
@test emptymissing(fun)([], 1, c=2) === missing
@test emptymissing(fun)(3, 1, c=2) == (1, 2)
end

end

2 comments on commit d034f67

@bkamins
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/74585

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.0 -m "<description of version>" d034f673cf8acc8125409370a286a9cde2fc7f98
git push origin v1.1.0

Please sign in to comment.