diff options
author | Tomasz Chabora <kobewi4e@gmail.com> | 2020-10-19 21:21:16 +0200 |
---|---|---|
committer | kobewi <kobewi4e@gmail.com> | 2021-05-05 15:54:57 +0200 |
commit | c50acc7339162eac734b20344c6422b740b978ab (patch) | |
tree | 3fe18cdce547c3899c0663810212baf6a6b6cc52 /doc | |
parent | eb57dcdb909dd45eaff6e25858a1907e13df4f59 (diff) |
Add filter, map and reduce to Array
Diffstat (limited to 'doc')
-rw-r--r-- | doc/classes/Array.xml | 53 |
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> |