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

Add conditional and passmissing #89

Merged
merged 7 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 44 additions & 1 deletion src/Missings.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Missings

export allowmissing, disallowmissing, ismissing, missing, missings,
Missing, MissingException, levels, coalesce
Missing, MissingException, levels, coalesce,
conditional, passmissing

using Base: ismissing, missing, Missing, MissingException

Expand Down Expand Up @@ -165,4 +166,46 @@ function levels(x)
levs
end

struct Conditional{P,X,Y} <: Function end

"""
conditional(predicate, x, y)
bkamins marked this conversation as resolved.
Show resolved Hide resolved

Return a function that that applies function `predicate` to its positional and keyword
arguments and returns the value of `x` applied to those arguments if `predicate`
returns true and otherwise returns the value of `y` applied to those arguments.

# Examples
```jldoctest
julia> f = conditional(x -> x ≥ 0, sqrt, x -> sqrt(complex(x)));

julia> f.([4, -4])
2-element Array{Number,1}:
2.0
0.0 + 2.0im
"""
conditional(predicate::Function, x::Base.Callable, y::Base.Callable) =
Conditional{predicate, x, y}()
(::Conditional{P,X,Y})(xs...;kw...) where {P,X,Y} =
P(xs...; kw...) ? X(xs...; kw...) : Y(xs...; kw...)

_passmissing_predicate(xs...;kw...) =
any(ismissing.(xs)) || any(ismissing.(values(values(kw))))
Copy link
Member

Choose a reason for hiding this comment

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

It's more efficient to pass ismissing as a function argument to any rather than broadcasting, i.e.

    any(ismissing, xs) || any(ismissing, values(values(kw)))

Copy link
Member

@nalimilan nalimilan Aug 31, 2018

Choose a reason for hiding this comment

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

Right. In any case, this really needs to be unrolled, specializing on the number of arguments, or it will be too slow. EDIT: I mean that the compiler should hopefully be able to do that automatically.

_passmissing_value(xs...;kw...) = missing

"""
passmissing(f)

Return a function that returns `missing` if any of its positional or keyword arguments
are `missing` and otherwise applies `f` to those arguments.

# Examples
```jldoctest
julia> passmissing(sqrt).([missing, 4])
Copy link
Member

Choose a reason for hiding this comment

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

It would be useful to start with a simpler example with a single scalar (first non-missing, and then missing).

Copy link
Member Author

Choose a reason for hiding this comment

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

added

2-element Array{Union{Missing, Float64},1}:
missing
2.0
"""
passmissing(f) = conditional(_passmissing_predicate, _passmissing_value, f)

end # module
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,10 @@ using Test, Dates, InteractiveUtils, SparseArrays, Missings

# MissingException
@test sprint(showerror, MissingException("test")) == "MissingException: test"

# Lifting
fun1 = conditional(x -> x ≥ 0, sqrt, x -> sqrt(complex(x)));
@test fun1(4) === 2.0
@test fun1(-4) === 2.0im
@test isequal(passmissing(sqrt).([missing, 4]), [missing, 2.0])
end