diff --git a/README.md b/README.md index aa84323..ab7f85a 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,8 @@ This package provides additional functionality for working with `missing` values - `Missings.replace` to wrap a collection in a (possibly indexable) iterator replacing `missing` with another value - `Missings.fail` to wrap a collection in a (possibly indexable) iterator throwing an error if `missing` is encountered - `skipmissings` to loop through a collection of iterators excluding indices where any iterators are `missing` +- `missingsmallest(f)` to create a partial order function that behaves as `f` and making `missing` the smallest value +- `missingsless` the standard `isless` function modified so that `missing` is less than any other element ## Contributing and Questions diff --git a/src/Missings.jl b/src/Missings.jl index 08ed26c..1f298f2 100644 --- a/src/Missings.jl +++ b/src/Missings.jl @@ -2,7 +2,7 @@ module Missings export allowmissing, disallowmissing, ismissing, missing, missings, Missing, MissingException, levels, coalesce, passmissing, nonmissingtype, - skipmissings, emptymissing + skipmissings, emptymissing, missingsmallest, missingsless using Base: ismissing, missing, Missing, MissingException @@ -514,4 +514,32 @@ julia> emptymissing(first)([1], 2) """ emptymissing(f) = (x, args...; kwargs...) -> isempty(x) ? missing : f(x, args...; kwargs...) +# Variant of `isless` where `missing` is the smallest value +""" + missingsmallest(f::Function) + +Creates a function of two arguments `x` and `y` that tests whether `x` is less +than `y` such that `missing` is always less than the other argument. In other +words, modifies the partial order function `f` such that `missing` is the +smallest possible value, and all other non-`missing` values are compared +according to `f`. + +See also: [`missingsless`](@ref), the standard order where `missing` is the +smallest possible value. + +# Examples +``` +julia> missingsmallest(isless)(missing, Inf) +true + +julia> missingsless(-Inf, missing) +false +``` +""" +missingsmallest(f) = (x, y) -> ismissing(y) ? false : ismissing(x) ? true : f(x, y) + +" The standard partial order `isless` modified so that `missing` is always the +smallest possible value." +const missingsless = missingsmallest(isless) + end # module diff --git a/test/runtests.jl b/test/runtests.jl index 92e8a2c..cddc768 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -257,4 +257,11 @@ struct CubeRooter end @test emptymissing(fun)(3, 1, c=2) == (1, 2) end +@testset "missingsmallest" begin + @test missingsless(missing, Inf) == true + @test missingsless(-Inf, missing) == false + @test missingsless(3, 4) == true + @test missingsless(-Inf, Inf) == true +end + end