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 filter, map and reduce to Array #38645

Merged
merged 1 commit into from
May 6, 2021
Merged

Conversation

KoBeWi
Copy link
Member

@KoBeWi KoBeWi commented May 10, 2020

Closes #17268

Example code:

func _ready() -> void:
	var array = [1, 2, 3, 4, 5, 6, 7, 8]
	
	print(array.filter(filter_test))
	print(array.map(map_test))
	print(array.reduce(reduce_test))

func filter_test(element: int) -> bool:
	return element < 4

func map_test(element: int) -> String:
	return str("element ", element)

func reduce_test(accum: int, element: int) -> int:
	return accum + element

The result is:

[1, 2, 3]
[element 1, element 2, element 3, element 4, element 5, element 6, element 7, element 8]
36

EDIT:
Here's lambda version btw:

func _ready() -> void:
	var array = [1, 2, 3, 4, 5, 6, 7, 8]
	print(array.filter(func(i): return i < 4))
	print(array.map(func(i): return str("element ", i)))
	print(array.reduce(func(i, accum): return accum + i))

@KoBeWi
Copy link
Member Author

KoBeWi commented May 11, 2020

Ok, added error checking (not sure if I did it correctly) and docs.

@KoBeWi KoBeWi marked this pull request as ready for review May 11, 2020 13:47
@KoBeWi KoBeWi requested a review from a team as a code owner May 11, 2020 13:47
@KoBeWi KoBeWi force-pushed the FMR branch 2 times, most recently from aca8ad5 to 8b07943 Compare May 11, 2020 19:03
Copy link
Member

@mhilbrunner mhilbrunner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs look good to me

@EricEzaM
Copy link
Contributor

If we are adding this, how about

Take(n) / TakeWhile(predicate)
Skip(n) / SkipWhile(predicate)
Any(predicate)
All(predicate)

Basically just implement part of C# linq lol.

Would be interested in working on that in a separate pr.

@Calinou
Copy link
Member

Calinou commented May 12, 2020

@EricEzaM These can be implemented in a separate PR. Note that Any and All are also called some and every in other languages like JavaScript's Array.

@akien-mga akien-mga requested a review from vnen May 15, 2020 16:05
core/array.cpp Outdated Show resolved Hide resolved
core/array.cpp Outdated Show resolved Hide resolved
core/array.h Outdated Show resolved Hide resolved
doc/classes/Array.xml Show resolved Hide resolved
@KoBeWi
Copy link
Member Author

KoBeWi commented Jun 2, 2020

I done the requested changes. Also in another commit I optimized the filter method.

@KoBeWi KoBeWi requested a review from vnen June 2, 2020 15:34
Copy link
Member

@vnen vnen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for taking so long, this slipped my mind. Although we need an okay from reduz to merge this.

core/array.cpp Outdated Show resolved Hide resolved
@KoBeWi
Copy link
Member Author

KoBeWi commented Sep 8, 2020

I pushed some changes. When doing the Tween PR I learned the proper way of handing Callable calls (by seeing how callv does it), so I changed the error handling. Should be more correct now.
(btw @vnen you still hold the "changes requested" status...)

@KoBeWi
Copy link
Member Author

KoBeWi commented May 3, 2021

Rebased and updated documentation with lambda functions (also edited OP with lambda examples).

core/variant/array.cpp Outdated Show resolved Hide resolved
Copy link
Member

@vnen vnen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@akien-mga akien-mga merged commit 01f8020 into godotengine:master May 6, 2021
@akien-mga
Copy link
Member

Thanks!

@KoBeWi KoBeWi deleted the FMR branch May 6, 2021 19:09
@kleonc
Copy link
Member

kleonc commented May 6, 2021

I see it's already merged but gonna write it in here anyway: docs descriptions for filter and map suggest these methods modify the original array. It should be explicitly stated that these methods return a new array with the results (and do not modify the original array they was called for). There's also a typo in reduce's docs.

@MikeSchulze
Copy link

very nice, thanks a lot

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

Successfully merging this pull request may close these issues.

GDScript: Basic Functional Concepts - Map, Filter, Reduce
8 participants