-
Notifications
You must be signed in to change notification settings - Fork 576
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
Feature Request: Splice for Mojo::Collection #1350
Comments
Currently I get around this like this:
|
There are times when I'd like to chain splice as well! 👍 |
As you have to hold a reference to the original collection to use the leftovers, splice would have to always be the first method in the chain. For example, this is the same as
Unless you plan on using
Do you have an example where having splice available would make things clearer? Perhaps one where it isn't the first in the chain? Creating a collection role for this (and your #1320 methods) is also an option to bear in mind if this isn't introduced to Mojo::Collection :) |
I think splice() in a chain gets very awkward, so based on that I'm not very positive to adding this Mojo::Collection. I do wonder though if we could add a bit more logic to slice() so we don't get undef elements. The one-liner below prints "2" when I expect it to print "0":
I could be off topic here, but the code example @srchulo looks more like "slice" than "splice". |
I think that slice behavior is consistent with list slicing. |
Speaking of slice(), I've often wanted the ability to slice the end, instead of the beginning. For example, When I'm dealing with a large collection, I might want to do some testing and grab the first 100.
But sometimes, I need to focus more on the latter elements, so it'd be nice to slice the end.
Is there a way that slicing could grab to the end? Perhaps by passing a callback if necessary to provide access to $_?
|
@CandyAngel So the example that I ran into I would have a reference to
@jhthorsen Is the reason that it's awkward in a chain because it modifies the original collection? I would think that methods could be useful in Mojo::Collection that aren't part of a chain. |
Would wrappers of the new List::Util head and tail functions suit your use case? |
@srchulo: I think it gets awkward when doing |
@Grinnz: Using compact() would not result in the same thing, in the case of |
The List::UtilsBy role could be a good way to do this. (I do the same thing with splice but in this case, I don't think there's any sensible and intuitive way to add the behavior at least to the core Mojo::Collection.) |
Yes! That looks like it would be great!! |
This example would require
As it would have behaviour that is very inconsistent with the other methods in Also, if you are looking to partition data in X sized chunks,
EDIT: Fixed incorrect reference to partition_by, should have been bundle_by |
@CandyAngel that's a good point. I modified my example from this:
And just using The suggestion of It's possible
With the only real benefits here being that it may be expected since it is available on arrays, and that you are returned a
I guess the same may be true for the methods I mentioned in #1320. I wonder if it makes sense to have methods that can be chained, and methods that are included for completeness with perl arrays. Or is the expectation there to use the built in methods and then wrap the results with |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Looks like everyone agrees that |
I think this could be a useful method. It would remove the elements from the existing collection, and return a new collection with those elements. I'd be happy to help with a pull request and tests if this seems like a good idea.
https://perldoc.perl.org/functions/splice.html
The text was updated successfully, but these errors were encountered: