From c50acc7339162eac734b20344c6422b740b978ab Mon Sep 17 00:00:00 2001 From: Tomasz Chabora Date: Mon, 19 Oct 2020 21:21:16 +0200 Subject: Add filter, map and reduce to Array --- doc/classes/Array.xml | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'doc/classes') 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] + + + + + + + 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] + + @@ -356,6 +373,23 @@ Returns [code]true[/code] if the array is empty. + + + + + + + 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] + + @@ -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. + + + + + + + + + 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] + + -- cgit v1.2.3