summaryrefslogtreecommitdiff
path: root/doc/classes
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <remi@verschelde.fr>2021-05-06 20:44:01 +0200
committerGitHub <noreply@github.com>2021-05-06 20:44:01 +0200
commit01f80201bf6a7a3f3d0de420f18c8f68363dbe62 (patch)
tree56e4e98f6b31d109e6404e898f8c6ba3396a34d4 /doc/classes
parentee4ef9709dcd03c17f5d7ee19c75ad29cd14cb44 (diff)
parentc50acc7339162eac734b20344c6422b740b978ab (diff)
Merge pull request #38645 from KoBeWi/FMR
Add filter, map and reduce to Array
Diffstat (limited to 'doc/classes')
-rw-r--r--doc/classes/Array.xml53
1 files changed, 53 insertions, 0 deletions
diff --git a/doc/classes/Array.xml b/doc/classes/Array.xml
index 38b74cb436..404528db9a 100644
--- a/doc/classes/Array.xml
+++ b/doc/classes/Array.xml
@@ -258,6 +258,23 @@
[/codeblocks]
</description>
</method>
+ <method name="filter" qualifiers="const">
+ <return type="Array">
+ </return>
+ <argument index="0" name="method" type="Callable">
+ </argument>
+ <description>
+ Calls the provided [Callable] for each element in array and removes all elements for which the method returned [code]false[/code].
+ [codeblock]
+ func _ready():
+ print([1, 2, 3].filter(remove_1)) # Prints [2, 3].
+ print([1, 2, 3].filter(func(number): return number != 1)) # Same as above, but using lambda function.
+
+ func remove_1(number):
+ return number != 1
+ [/codeblock]
+ </description>
+ </method>
<method name="find" qualifiers="const">
<return type="int">
</return>
@@ -356,6 +373,23 @@
Returns [code]true[/code] if the array is empty.
</description>
</method>
+ <method name="map" qualifiers="const">
+ <return type="Array">
+ </return>
+ <argument index="0" name="method" type="Callable">
+ </argument>
+ <description>
+ Calls the provided [Callable] for each element in array and replaces them with return value of the method.
+ [codeblock]
+ func _ready():
+ print([1, 2, 3].map(negate)) # Prints [-1, -2, -3].
+ print([1, 2, 3].map(func(number): return -number)) # Same as above, but using lambda function.
+
+ func negate(number):
+ return -number
+ [/codeblock]
+ </description>
+ </method>
<method name="max" qualifiers="const">
<return type="Variant">
</return>
@@ -468,6 +502,25 @@
[b]Note:[/b] On large arrays, this method is much slower than [method push_back] as it will reindex all the array's elements every time it's called. The larger the array, the slower [method push_front] will be.
</description>
</method>
+ <method name="reduce" qualifiers="const">
+ <return type="Variant">
+ </return>
+ <argument index="0" name="method" type="Callable">
+ </argument>
+ <argument index="1" name="accum" type="Variant" default="null">
+ </argument>
+ <description>
+ Calls the provided [Callable] for each element in array and accumulates the result in [code]accum[/code]. The method for [Callable] takse two arguments: current value of [code]accum[/code] and the current array element. If [code]accum[/code] is [code]null[/code] (default value), the method will use first element from the array as initial value.
+ [codeblock]
+ func _ready():
+ print([1, 2, 3].reduce(factorial, 1)) # Prints 6.
+ print([1, 2, 3].reduce(func(accum, number): return accum * number)) # Same as above, but using lambda function.
+
+ func factorial(accum, number):
+ return accum * number
+ [/codeblock]
+ </description>
+ </method>
<method name="remove">
<return type="void">
</return>