-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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 skipnothing function which returns an iterator without nothing
#30549
Conversation
It looks like the build failures are unrelated to the code in the PR, but I'm not 100% sure how to interpret them. |
Just to motivate the need for
Whereas It seems to me that if |
If https://github.com/JuliaLang/julia rejects this (without a simple alternative), it's welcome at https://github.com/JuliaCollections/IterTools.jl |
It may be worth considering adding |
Good idea @tkf, I just did that. |
Makes sense. Could those who downvoted the proposal give their reasons? |
From https://discourse.julialang.org/t/how-to-calculate-a-weighted-mean-with-missing-observations/19281/12, how about skip(somevalue, iterator) where I think using |
I consider various implementations of |
@tpapp So you're opposed to the introduction of a new function name dedicated to handling the presence of If so, I disagree with that sentiment: as is shown by #29679 Your suggested specialization of |
I am just cautious about filling in every combination of I see nothing intrinsically wrong with I also think the functionality of this particular PR could live in a package while people experiement with this. |
I see your point, but this is just filling out a missing slot with Even if Maybe someone else can integrate the type narrowing into |
Also, |
There's also |
@iamed2 I wasn't aware of that, thanks for pointing it out. |
I don't know how things work around here: is this PR still under consideration? |
Sometimes PRs that suggest changes to the API are revisited later, especially if there is other stuff going on (new releases). |
base/skipoftype.jl
Outdated
|
||
IteratorSize(::Type{<:SkipOfType}) = SizeUnknown() | ||
IteratorEltype(::Type{SkipOfType{T, A}}) where {T, A} = IteratorEltype(A) | ||
eltype(::Type{SkipOfType{T, A}}) where {T, A} = union_poptype(T, eltype(A)) |
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.
typesubtract
?
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.
Changed
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.
@galenlynch typesubtract
is already a function that exists, at Core.Compiler.typesubtract
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.
Ah I had no idea! I changed the PR to use the existing function.
This adds `skipnothing`, which mirrors the utility of `skipmissing` except for `nothing` values. The code underlying `skipnothing` is largely copy and pasted from `skipmissing`, with necessary modifications.
Introduces `skipoftype(T, itr)` which skips elements in an iterator of type `T`. This can be used to replace `skipmissing` as well as `skipnothing`.
Previously, `union_poptype` would fail if the arguments only partially overlapped: i.e. `union_poptype(Union{A, B}, Union{A, C})`. First taking the union of all the types allows `union_poptype` even in this edge-case.
e89788a
to
d801ea6
Compare
I attempted to rebase this PR onto |
@nalimilan I see that many of the rebase conflicts I encountered came from #31008. I've tried to port over your PR to |
Triage wants a more general API where you give a type to exclude, e.g. |
Another approach would be to have |
As part of this PR I converted the core of |
AFAICT the difference between |
I think another difference is that |
If we could make |
I think you can specialize |
So it seems like the most general API would allow @tkoolen also suggested allowing for skipping over other values, such as |
Indeed, |
The other feature that |
Also change filter(f, itr::SkipMissing) to work with Skip instead of SkipMissing. In addition, this method now pre-allocates its output instead of appending elements to an empty array.
I'm still a bit worried about adding a new Overall I'd be more inclined to add a special case to |
And add indexing to it too?
…On Sat, Mar 30, 2019, 6:55 PM Milan Bouchet-Valat ***@***.***> wrote:
I'm still a bit worried about adding a new skip function, as nothing in
its name indicates it should skip values according to their type, rather
than just skipping values according to a predicate like filter and
Iterators.filter do. One could easily confuse these functions, which
makes the language harder to grasp. I'm also not sure there's a lot of use
for a general skip(T, itr) function apart from skipmissing and skipnothing,
so it sounds kind of radical to claim such a common term in Base just for
this.
Overall I'd be more inclined to add a special case to Iterators.filter
(and maybe filter), e.g. for !==(x) when x is a singleton type or for
!isnothing/!ismissing. The former would be consistent with what we do
with replace(itr, nothing=>0).
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#30549 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGg8z9T-zza1SUUFhCnSrZFzDifm0NOPks5vb-tNgaJpZM4Zl6Q9>
.
|
@nalimilan I don't really understand what you're proposing, move all the code that used to belong to It doesn't seem like there's much consensus on which name to use. I don't have a strong opinion. |
Generally, using up commonly used short verbs for function names in It is generally preferred to built up functionality from existing vocabulary, especially if that can be done with zero overhead. My preferred solution would be specializing filter(!isequal(nothing), [1, nothing, 2]) so that it returns a Finally, I realize that it is frustrating to make a PR and then receive conflicting suggestions and/or not getting a clear direction for finalizing it. But with a seemingly simple addition to the language, you ran into a problem which hinges on many questions which are not yet clearly answered, and since things in |
How about just exporting |
Or not even exporting |
|
If |
This adds a new function
skipnothing
, which mirrors the utility ofskipmissing
except fornothing
values instead ofmissing
ones.To achieve this, I have converted the logic behind
skipmissing
into a more general function,skipoftype(T, itr)
, which skips over elements of typeT
in an iteratoritr
.skipmissing(itr)
is now simply defined as:Similarly, the new function
skipnothing(itr)
is simplyskipoftype(Nothing, itr)
.The benefits of
skipnothing
versus filtering or array comprehensions are described below.