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

Routines shift and pop on lists should allow an option to pop or shift multiple elements in one op #436

Open
tbrowder opened this issue Aug 31, 2024 · 6 comments
Labels
language Changes to the Raku Programming Language

Comments

@tbrowder
Copy link
Member

Currently it is awkward to accomplish that with routine splice. For example, to shift two elements into another list:

my @a = 1, 2, 3;
my @b = @a.splice(0, 2):

To pop two elements is even more awkward if you want the elements removed from the end as one would expect. The routine name is not intuitive, either.

It would be much easier, and intuitive, to do this:

my @b = @a.shift: 2;

or

my @b = @a.pop: 2;
@tbrowder tbrowder added the language Changes to the Raku Programming Language label Aug 31, 2024
@ab5tract
Copy link

ab5tract commented Sep 2, 2024

I'm hesitant about this because the functionality you want is very easily composed using xx:

my @new = @old.pop xx $n

FWIW I would not have reached for splice here, as I associate it more for what it can do regarding putting things into arrays rather than what it can do taking things out.

But I have done the above with xx many times. We might be able to save a few cycles by making a dedicated pop($n), and I don't have strong objections to it beyond it feeling superfluous (which may be an argument easily deflated by TMTOWTDI).

@lizmat
Copy link
Collaborator

lizmat commented Sep 2, 2024

Note that .chop, which you could consider the equivalent of .pop for strings, allows the number of graphemes to chop.

@tbrowder
Copy link
Member Author

tbrowder commented Sep 2, 2024

I'm hesitant about this because the functionality you want is very easily composed using xx:

my @new = @old.pop xx $n

I was not aware of that, thanks! But, as @lizmat said, .pop is similar to .chop, so there is that precedent. And my proposal seems more natural to me. (What would Larry say?)

FWIW I would not have reached for splice here,...

Nor would I, but that was the easiest thing at hand. I had never needed it before, and it was the only thing suggested on IRC that took care of my immediate need.

I rest my case on the beauty of TMTOWTDI (as opposed to the ugly Pythonic way).

@patrickbkr
Copy link
Member

I think this is a low-risk change (API design wise). I for one would have guessed right what the meaning of the arg is. So my vote is: Why not?

@niner
Copy link

niner commented Sep 7, 2024

Before we get to a decision, we first need a fully specified proposal. We do not have that yet. We have an intriguing idea. What we still need is the rest of the design. Most notably, how would we treat edge cases like [1].shift(2). Would this throw? Would it return the only element? And what other edge cases are there?

@lizmat
Copy link
Collaborator

lizmat commented Sep 8, 2024

Other things to consider:

Should
.pop(N) / .shift(N) return a List, Seq or an Array?

Should Callables be allowed, as in .pop(*-1), .shift(*-3)?

Which should be the order in which multiple elements are to be returned?

my @a = ^10;
dd @a.pop(2);  # (8,9) or (9,8) ??

@Raku Raku deleted a comment from raiph Sep 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language Changes to the Raku Programming Language
Projects
None yet
Development

No branches or pull requests

5 participants