-
-
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 any position #42902
Conversation
Ok, I think this should be mostly working now. Before I add some tests, add some more overloads and finish this off, I would like some feedback on the proposed API here from triage:
|
Maybe
Can this be smart for things that return a Other than that, this looks nice! |
I do like this. I suppose it's predictable the next request will be to support |
No, array literals do promotion, so we can't do that in this case. We can definitely add a specialized version of
I'm afraid it might already be a little late for that since we do already support type annotations for slurping in the last argument, where it annotates the type of the slurped iterator itself though, not the element type. In this PR, we get the same behavior since lowering always works recursively. |
Ah, I didn't even think about array literals, only about arrays returned from functions! That should still be a O(1) case instead of O(n) in theory, right? The question is to |
Yeah, probably.
We already decided against using views in #37410, so we probably don't want to rehash that discussion. |
I may be reading the PR wrong or misunderstanding you, but I was thinking that e.g.
could result in |
That tuple is never created, but semantically you can probably think about it this way. What I'm saying is that |
Triage approves of the concept. @JeffBezanson would like to review the implementation. |
This extends the current slurping syntax by allowing the slurping to not only occur at the end, but anywhere on the lhs. This allows syntax like `a, b..., c = x` to work as expected. The feature is implemented using a new function called `split_rest` (definitely open to better names), which takes as arguments the iterator, the number of trailing variables at the end as a `Val` and possibly a previous iteration state. It then spits out a vector containing all slurped arguments and a tuple with the n values that get assigned to the rest of the variables. The plan would be to customize this for different finite collection, so that the first argument won't always be a vector, but that has not been implemented yet. `split_rest` differs from `rest` of course in that it always needs to be eager, since the trailing values need to be known immediately. This is why the slurped part has to be a vector for most iterables, instead of a lazy iterator as is the case for `rest`. Mainly opening this to get some feedback on the proposed API here.
54b2938
to
fc075da
Compare
205a381
to
0b438c4
Compare
Bump |
@assert _check_length_split_rest(length(s), n) | ||
end | ||
last_n = SubString(s, nextind(s, i), lastind) | ||
front = s[begin:i] |
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.
Is there a reason not to use either indexing or SubString for both values?
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 think my reasoning was that the type of last_n
doesn't really matter as long as it iterates correctly, so we might as well avoid the extra allocation. I guess we could make front
a SubString
as well, but that might be wasteful for small string if n
is not much smaller than the string length, since we can't free the original string
(list assigns))) | ||
(n (length lhss-)) | ||
(st (gensy)) | ||
(end (list after)) |
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.
IIUC, would be clearer to make this a mutable variable than a 1-element list.
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.
Oh I see, it is also passed to destructure-
😭
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.
Yeah, agree it's a little bit awkward, but couldn't think of something more elegant
I don't know if this is the right place to make the following remark / request, but this is where I saw (coming from here )that the new split_rest function has been defined. |
This extends the current slurping syntax by allowing the slurping to not
only occur at the end, but anywhere on the lhs. This allows syntax like
a, b..., c = x
to work as expected.The feature is implemented using a new function called
split_rest
(definitely open to better names), which takes as arguments the
iterator, the number of trailing variables at the end as a
Val
andpossibly a previous iteration state. It then spits out a vector
containing all slurped arguments and a tuple with the n values that get
assigned to the rest of the variables. The plan would be to customize
this for different finite collection, so that the first argument won't
always be a vector, but that has not been implemented yet.
split_rest
differs fromrest
of course in that it always needs to beeager, since the trailing values need to be known immediately. This is
why the slurped part has to be a vector for most iterables, instead of a
lazy iterator as is the case for
rest
.Mainly opening this to get some feedback on the proposed API here.
Closes #43413