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
Multi pop/shift #149
Comments
+1 for popn. |
I am working on this task for Google Code-In. So far, in my version of popn(), given this code: my @array = (1, 2, 3, 4, 5); my @Newarray = @Array->popn(3); It will return @array == (1, 2); and @Newarray == (5, 4, 3). My question is, should it be (5, 4, 3) or (3, 4, 5)? My argument for (5, 4, 3) is, it is the same as @Newarray = (@Array->pop(), @Array->pop(), @Array->pop()); But the counter-argument is, you are asking for a lump of 3 things, and maybe you'd want to individually pop from that array (ie, @newarray->pop() would return what @Array->pop would, before the popn). What are others thoughts on this? |
I've thought up some more arguments in favor of
I think that last one is the killer. Perl programmers work with lists far more often than stacks. @player1537 Unless there's further discussion, please do it the Also note that all perl5i array operations return an array in list context and an array reference in array context. This allows them to chain like |
Okay, I've changed popn to return (3, 4, 5). Also, I just went with a loop instead of using splice or something similar, so... Should I change that to splice (maybe for speed issues?) or just leave it the way I thought to do it first? Also, I've already written tests for shiftn and popn, and I followed the wantarray pattern that all the other methods used :) When, or how, should I get the code to you? Or should I just wait until you get back on IRC? |
I've re-written the code to use splice() and all of my tests still work. The ARRAY.pm diff, as well as t/popn.t and t/shiftn.t are up on my server, at: http://tanner.isthecoolest.com/filesys/popn-shiftn.tar Let me know if anything needs to be changed about it :) |
@player1537 Good work. Thorough testing on the argument boundaries. I'm putting it into a branch called issue/149 so we can collaborate on it. If you Here's a few observations and changes.
I'll make those changes.
3 is interesting because it guarantees that the array returned will be the same length as what you asked for. 4 is interesting because it still allows the operation and it marks the place where the problem happened. I think it comes down to 2 or 4+3. Either it should shut up and just DWIM or it should warn immediately. Thoughts? |
@player1537 PS For this to be a complete patch it needs documentation. That goes into lib/perl5i.pm in the "Array Autoboxing" section. Documentation is written using Plain Old Documentation (see http://perldoc.perl.org/perlpod.html) and you can just crib from any of the other entries. |
Indeed, I had thought about, and I think I even implemented it one time, making @Array->popn($too_long) return extra values. At first, I wanted to make it return 0s, except that the data type could be different from a number. I also didn't want to write a test for comparing against undefined values, so I chose not to fill it with those. I'm liking just returning the whole array. For the part about an arg of 0, I chose to do that basically for the same reasons as you. I'll write some documentation for this when I get home, being sure to go over what the various arguments do. Should I make a case of string arguments? Right now, if you give it a string arg, it fails in the function, with some odd error (like, non-numeric comparison values for ">"). |
I wrote some tests, and fixed the functions to die on <0 || !defined, but what do you mean by "test in scalar contexts"? Once I get that scalar contexts part worked out, this should be finished :) Oh, and documentation, which I'll get started on now. |
@player1537 "Test in scalar context" is checking that Erroring out nicely on string arguments would be good, yes. perl5i has methods you can use to check if a number is an integer or positive. See I'm not sure if you noticed that I've done work on top of your initial patch. That's what those "schwern referenced this issue from a commit" messages are for. It's in a new branch called issue/149. You should be working on top of that. Let me know if you don't know how to get it. http://help.github.com/remotes/ has some help on updating your repository. A test against undefined values is fine. The testing functions all work pretty well. |
Ah, that makes sense about the scalar context :) I'll try to change things over to use ->is_positive, etc. Regarding the new branch, I tried to download it, but... Ah, my issue was trying to clone everything in the same directory as I have been using. I see your commits, now. I'll go ahead and update it with the documentation I wrote and add in ->is_integer tests. Also, I don't know how to check in an update, so... I'll probably just tar the diffs and such, again. |
Here is a tar of: t/popn.t, t/shiftn.t, lib/perl5i.pm, and lib/perl5i/2/ARRAY.pm. http://tanner.isthecoolest.com/filesys/issue.tar Hopefully, if you are ever online when I am, you can help me with committing things myself, so I don't have to do this again :) The updates are: fixing popn and shiftn to error out on string args (or, rather, non-integer), adding that to the tests, and adding some documentation for popn and shiftn. |
Insted of enumerating all the ways it could go wrong, tell the user how it should go right. For #149
That will help users debug their problem. For #149
I'm happy with where it's at. Closing it up. |
Popping or shifting multiple items off an array is a pita. Either you have to figure out how to use splice to do it, or use a for loop.
Make a pop and shift that take the number of things to pop off.
Issue: pop() returns a scalar. @Array->pop(3); should return an array or array ref. Does this mean we modify @Array->pop so it returns an array if there's an argument? Or do we write popn? I lean towards popn/shiftn.
The text was updated successfully, but these errors were encountered: