-
Notifications
You must be signed in to change notification settings - Fork 19
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1516580
add conditional and passmissing
bkamins 3bf10c0
leave a minimal implementation of passmissing
bkamins f947bcf
Improved docstring
bkamins e775f2b
use generated function
bkamins 4cc1a5e
improved passmissing implementation
bkamins 7f491bf
fix a typo
bkamins 8c0bd5d
test keyword arguments
bkamins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module Missings | ||
|
||
export allowmissing, disallowmissing, ismissing, missing, missings, | ||
Missing, MissingException, levels, coalesce | ||
Missing, MissingException, levels, coalesce, passmissing | ||
|
||
using Base: ismissing, missing, Missing, MissingException | ||
|
||
|
@@ -165,4 +165,57 @@ function levels(x) | |
levs | ||
end | ||
|
||
struct PassMissing{F} <: Function | ||
f::F | ||
end | ||
|
||
function (f::PassMissing{F})(x) where {F} | ||
if @generated | ||
return x === Missing ? missing : :(f.f(x)) | ||
else | ||
return x === missing ? missing : f.f(x) | ||
end | ||
end | ||
|
||
function (f::PassMissing{F})(xs...) where {F} | ||
if @generated | ||
for T in xs | ||
T === Missing && return missing | ||
end | ||
return :(f.f(xs...)) | ||
else | ||
return any(ismissing, xs) ? missing : f.f(xs...) | ||
end | ||
end | ||
|
||
""" | ||
passmissing(f) | ||
|
||
Return a function that returns `missing` if any of its positional arguments | ||
are `missing` (even if their number or type is not consistent with any of the | ||
methods defined for `f`) and otherwise applies `f` to these arguments. | ||
|
||
`passmissing` does not support passing keyword arguments to the `f` function. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> passmissing(sqrt)(4) | ||
2.0 | ||
|
||
julia> passmissing(sqrt)(missing) | ||
missing | ||
|
||
julia> passmissing(sqrt).([missing, 4]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
2-element Array{Union{Missing, Float64},1}: | ||
missing | ||
2.0 | ||
|
||
julia> passmissing((x,y)->"\$x \$y")(1, 2) | ||
"1 2" | ||
|
||
julia> passmissing((x,y)->"\$x \$y")(missing) | ||
missing | ||
""" | ||
passmissing(f::Base.Callable) = PassMissing{typeof(f)}(f) | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally prefer
Missings.propagate
if we're still bikeshedding the name here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like
propagate
too, the only gripe is that it's quite long. But well...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But do we want to export it or not? If yes then
propagate
is ok, butMissings.propagate
is a bit long.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately
propagate
is quite general to claim it for this feature...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, but that is why I thought the idea was
propagatemissing
which tab-completes.