diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2021-05-07 14:37:48 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-07 14:37:48 +0200 |
commit | eb1ad225634f0ef5ebc0d32ea5edaa47bd41fcdf (patch) | |
tree | 6ad1804fdcdd5204b8890d0af9f241e9e984f83d /doc | |
parent | bcbd480c32f7c9a6fdaad297ef1fbb8fa5fa12ee (diff) | |
parent | a95c953c48e615f3bc6c61eb8f0561ada1585b8d (diff) |
Merge pull request #48530 from KoBeWi/FMR_reloaded
Improve docs for filter map and reduce
Diffstat (limited to 'doc')
-rw-r--r-- | doc/classes/Array.xml | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/doc/classes/Array.xml b/doc/classes/Array.xml index 404528db9a..624b51e463 100644 --- a/doc/classes/Array.xml +++ b/doc/classes/Array.xml @@ -264,7 +264,8 @@ <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]. + 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 @@ <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. + 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 @@ <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. + 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] </description> </method> |