From a95c953c48e615f3bc6c61eb8f0561ada1585b8d Mon Sep 17 00:00:00 2001 From: kobewi Date: Fri, 7 May 2021 13:06:30 +0200 Subject: [PATCH] Improve docs for filter map and reduce --- doc/classes/Array.xml | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/doc/classes/Array.xml b/doc/classes/Array.xml index 404528db9a0..624b51e4636 100644 --- a/doc/classes/Array.xml +++ b/doc/classes/Array.xml @@ -264,7 +264,8 @@ - Calls the provided [Callable] for each element in array and removes all elements for which the method returned [code]false[/code]. + 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]. @@ -379,7 +380,8 @@ - Calls the provided [Callable] for each element in array and replaces them with return value of the method. + 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]. @@ -510,14 +512,15 @@ - 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. + 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(factorial, 1)) # Prints 6. - print([1, 2, 3].reduce(func(accum, number): return accum * number)) # Same as above, but using lambda function. + 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 factorial(accum, number): - return accum * number + func sum(accum, number): + return accum + number [/codeblock]