-
-
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
allow slurping in lhs of assignment #37410
Conversation
Cool! My current thinking is that if we do this, it should use |
Ok, that can certainly be discussed, I was just under the impression that Stefan's comment concluded the discussion in #2626. I personally would still favor just always returning tuples, because I think it's going to be difficult to find a rule for which container types should slurp to which type for the rest and it's simpler to remember that it's just always a tuple. For example, what should |
It's not changing meaning, it's using a different implementation of that meaning. Multiple data structures support the concept of |
I mostly want this just for tuples, so what to do for other iterators is not a hill I'm willing to die on. What do you reckon would be the best way forward here? Should this just be triaged, or would it make sense to reopen the discussion in #2626, so that people that participated there can chime in? |
Ok, let's have this call |
So I'm thinking as the default return types of
Does that make sense, or is there anything else that would make sense to special-case? Perhaps |
Ah, this doesn't work with just passing the iteration state to |
e5a220f
to
490515d
Compare
So the interesting thing is that this feature is slightly incompatible with #37268, since that change means we don't always have an iteration state available. To get elements, you don't need the iteration state, but for |
I think we are not going to do the |
490515d
to
1b09f69
Compare
So we decided to make |
@@ -2399,3 +2399,11 @@ function hash(A::AbstractArray, h::UInt) | |||
|
|||
return h | |||
end | |||
|
|||
# The semantics of `collect` are weird. Better to write our own |
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.
How about collect(T, Iterators.rest(a, state))
? Then for the 1-argument case Vector{T}(a)
. Better to avoid repeated push!
since it's a bit slow.
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.
Then for the 1-argument case Vector{T}(a).
That won't work for multi-dimensional arrays, unfortunately:
julia> Vector{Int}([1 2; 3 4])
ERROR: MethodError: no method matching Array{Int64,1}(::Array{Int64,2})
Closest candidates are:
Array{Int64,1}(::AbstractArray{S,N}) where {T, N, S} at array.jl:562
Array{Int64,1}() where T at boot.jl:425
Array{Int64,1}(::UndefInitializer, ::Int64) where T at boot.jl:406
...
Stacktrace:
[1] top-level scope at REPL[25]:1
collect
also doesn't always return a Vector
and I think it would be weird if Base.rest(::A)
and Base.rest(::A, st)
returned different types.
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.
Also, note that:
julia> Base.IteratorSize(Iterators.rest([1 2; 3 4], 1))
Base.SizeUnknown()
so collect
will end up just using push!
anyways.
Looking good. |
Co-authored-by: Jeff Bezanson <jeff.bezanson@gmail.com>
e4a12d4
to
d357579
Compare
OMG, I did not think this was happening! |
This reverts commit 6edf6d9.
There's also a bunch of issue links that are missing (JuliaLang#37410, JuliaLang#37247, JuliaLang#37540, JuliaLang#37973, JuliaLang#37461, JuliaLang#37753) but it seems there's a script that generates the links so I'm assuming that will be fixed automatically. Co-authored-by: Viral B. Shah <ViralBShah@users.noreply.github.com>
* Document assignment destructuring * Add reference to slurping assignment (#37410) * mention swapping variables and other kinds of LHS Co-authored-by: Jameson Nash <vtjnash@gmail.com> Co-authored-by: Simeon Schaub <simeondavidschaub99@gmail.com>
* Document assignment destructuring * Add reference to slurping assignment (JuliaLang#37410) * mention swapping variables and other kinds of LHS Co-authored-by: Jameson Nash <vtjnash@gmail.com> Co-authored-by: Simeon Schaub <simeondavidschaub99@gmail.com>
* Document assignment destructuring * Add reference to slurping assignment (JuliaLang#37410) * mention swapping variables and other kinds of LHS Co-authored-by: Jameson Nash <vtjnash@gmail.com> Co-authored-by: Simeon Schaub <simeondavidschaub99@gmail.com>
* Document assignment destructuring * Add reference to slurping assignment (JuliaLang#37410) * mention swapping variables and other kinds of LHS Co-authored-by: Jameson Nash <vtjnash@gmail.com> Co-authored-by: Simeon Schaub <simeondavidschaub99@gmail.com>
This is currently branched off #37268. I will rebase once that is merged, but for now, only the last commit is relevant.This implements @StefanKarpinski's comment in #2626 (comment). I am not sure,slurp_rest
is the best name and API for this, perhaps it would make more sense to extenditerate_and_index
to return a third function for slurping, sinceslurp_rest
currently relies on whatiterate
does, which could lead to gotchas if users overloaditerate_and_index
.fixes #2626