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 map/filter methods for iterable types #3617

Closed
user6174 opened this issue Nov 30, 2021 · 1 comment
Closed

Add map/filter methods for iterable types #3617

user6174 opened this issue Nov 30, 2021 · 1 comment

Comments

@user6174
Copy link

Describe the project you are working on

A 2D match-3 game.

Describe the problem or limitation you are having in your project

I often iterate over enums and dictionaries, and would love to benefit from the introduction of functional features in the same way that I do with arrays.

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

As per title, I'm proposing that map and filter be implemented for that any type x that's accepted in a for i in x line of code.

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

enum E {FIRST, SECOND, THIRD}
var d = {E.FIRST: 1, E.SECOND: 2, E.THIRD: 3}

func _ready():
	print(E.\
		filter(func(e): return e!=E.SECOND).\
		map(func(e): return d[e] * 2))

OUTPUT: [2, 6]

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

Equivalent code to the one in my example would be:

enum E {FIRST, SECOND, THIRD}
var d = {E.FIRST: 1, E.SECOND: 2, E.THIRD: 3}

func _ready():
	var result = []
	for key in d:
		if key!=E.SECOND:
			result.append(d[key] * 2)
	print(result)

OUTPUT: [2, 6]

which is more verbose (the functional version could be even leaner if the lambdas didn't require an explicit return keyword, but that topic should probably go in its own feature proposal), uses an auxiliary variable, and personally feels less readable.

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

As it has to do with the syntax of GDScript it would be cleaner and more consistent for this to be core.

@Calinou
Copy link
Member

Calinou commented Nov 30, 2021

This is already implemented in the master branch: godotengine/godot#38645

It can't be backported to 3.x as it relies on first-class Callables. In the meantime, you can use the Golden Gadget add-on.

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

No branches or pull requests

2 participants