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

Add some(), every() and reduce_right() methods to Array #2926

Closed
ghost opened this issue Jun 27, 2021 · 4 comments · Fixed by godotengine/godot#50349
Closed

Add some(), every() and reduce_right() methods to Array #2926

ghost opened this issue Jun 27, 2021 · 4 comments · Fixed by godotengine/godot#50349
Milestone

Comments

@ghost
Copy link

ghost commented Jun 27, 2021

Describe the project you are working on

None yet, I'm still learning Godot.

Describe the problem or limitation you are having in your project

None.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Simple Array methods based on JavaScript Array (developer.mozilla.org).

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

reduce_right:

var a = [[0, 1], [2, 3], [4, 5]].reduce_right(
    func(accumulator, current_value) : accumulator + current_value, [])

# a = [4, 5, 2, 3, 0, 1]

some:

[5, 10, 0].some(func(v): v > 5) # true
[5, 5 , 0].some(func(v): v > 5) # false

every:

[6, 10, 6].every(func(v): v > 5) # true
[6, 10, 0].every(func(v): v > 5) # false

If this enhancement will not be used often, can it be worked around with a few lines of script?

How it looks with a workaround:

var ArrayHelpers = preload("res://gd-libs/array-helpers/lib.gd")

func reduce_right(array: Array, reduce_fn: Callable, initial_value = 0):
    return ArrayHelpers.reduce_right(array, reduce_fn, initial_value)

var r = reduce_right ( [[10], [5, 2], [1, 3]], func(a, v): a + v )

Is there a reason why this should be core and not an add-on in the asset library?

It's simple enough to not be built-in. The PR godotengine/godot#38645 added filter/map/reduce.

I can't do PR due to the repository weight.

@Calinou Calinou changed the title GDScript: Add Array some, every and reduce_right methods Add some(), every() and reduce_right() methods to Array Jun 27, 2021
@KoBeWi
Copy link
Member

KoBeWi commented Jun 27, 2021

reduce() exists in 4.0. You can achieve reduce_right() by reversing the array before using it.

Instead of some(), we could just add find_custom() method (bsearch_custom() doesn't work for unsorted arrays) and every() is the same as some() with opposite condition.

@Calinou
Copy link
Member

Calinou commented Jun 27, 2021

While you can use array.filter(...).size() >= 1 and array.filter(...).size() == array.size(), I like the syntactic sugar aspect of some() and every(). some() and every() can also bail out early, which improves performance with large arrays.

reduce_right() avoids having to reverse the array before reducing it (which has a performance cost for large arrays), but I think concrete use cases for it are few and far between.

@nathanfranke
Copy link
Contributor

I think reduce_right it too oddly specific to have its own method. I think invert().reduce() would be more readable, and if performance is important, just use a for loop.

@Calinou
Copy link
Member

Calinou commented Aug 13, 2021

Instead of some(), we could just add find_custom() method (bsearch_custom() doesn't work for unsorted arrays) and every() is the same as some() with opposite condition.

This was requested in #1120, but I think it should be addressed in a separate pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants