-
-
Notifications
You must be signed in to change notification settings - Fork 99
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
Add remove_all(predicate)
to Array
#3116
Comments
This has already been implemented in 4.0 (the Lambda functions have been added to GDScript too: |
Filter is more like |
I think that when you're willing to optimize for memory and speed, using an imperative loop is better as it also avoids creating a lambda and all that stuff. Readability generally becomes a secondary concern when performance is critical. |
@Calinou Yeah, I really wish more stuff supported some of the functional concepts like inlining anonymous functions. Then |
RemoveAll( Predicate )
to Array
remove_all(predicate)
to Array
weakItems = weakItems.Filter( (it) => it.GetRef() != null ); |
@dalexeev yeah, that was already discussed above |
Generally, I'd agree, but remove is a special case where you need to be careful about how you iterate over the list while removing. There are several questions about it because you need to iterate in reverse, right?
But even then, it's not really efficient because you should only shift each element once:
(A rough and untested port of a lua implementation.) Thus having |
Describe the project you are working on
A game in C#
Describe the problem or limitation you are having in your project
I have an array of
WeakRef
that I occasionally scrub. Removing multiple items from a Godot Array gets annoying.Since C# has lambda support, it'd be nice to have something like List's
RemoveAll
for Godot's Array class.It would likely be a touch faster as well.
If/when GDScript lambdas are ready, it could benefit from this function as well.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
Basically you could do something like:
For those unfamiliar, the lambda takes in each item and returns a boolean of whether or not it should be deleted.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
When doing a "remove all", it's best if you can iterate just once and only move items once.
Something like:
Not sure if some of that could be done on the C++ side, especially if GDScript gets lambdas.
If this enhancement will not be used often, can it be worked around with a few lines of script?
I've seen a lot of folks recommending various ways to remove a lot of items. Each method is of varying speed. I imagine they are usually fine enough.
The method pasted above is an extension method, so folks can just use that.
Is there a reason why this should be core and not an add-on in the asset library?
Removing items from a list based on a condition is fairly useful...?
This is a 'nice to have' not a 'need'.
The text was updated successfully, but these errors were encountered: