diff options
Diffstat (limited to 'doc/classes/Array.xml')
-rw-r--r-- | doc/classes/Array.xml | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/doc/classes/Array.xml b/doc/classes/Array.xml index 38b74cb436..624b51e463 100644 --- a/doc/classes/Array.xml +++ b/doc/classes/Array.xml @@ -258,6 +258,24 @@ [/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] on each element in the array and returns a new array with the elements for which the method returned [code]true[/code]. + The callable's method should take one [Variant] parameter (the current array element) and return a boolean value. + [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 +374,24 @@ 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 the array and returns a new array filled with values returned by the method. + The callable's method should take one [Variant] parameter (the current array element) and can return any [Variant]. + [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 +504,26 @@ [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 callable's method takes two arguments: the current value of [code]accum[/code] and the current array element. If [code]accum[/code] is [code]null[/code] (default value), the iteration will start from the second element, with the first one used as initial value of [code]accum[/code]. + [codeblock] + func _ready(): + print([1, 2, 3].reduce(sum, 10)) # Prints 16. + print([1, 2, 3].reduce(func(accum, number): return accum + number, 10)) # Same as above, but using lambda function. + + func sum(accum, number): + return accum + number + [/codeblock] + </description> + </method> <method name="remove"> <return type="void"> </return> |