Skip to content

Commit eb1ad22

Browse files
authored
Merge pull request #48530 from KoBeWi/FMR_reloaded
Improve docs for filter map and reduce
2 parents bcbd480 + a95c953 commit eb1ad22

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

doc/classes/Array.xml

+10-7
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@
264264
<argument index="0" name="method" type="Callable">
265265
</argument>
266266
<description>
267-
Calls the provided [Callable] for each element in array and removes all elements for which the method returned [code]false[/code].
267+
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].
268+
The callable's method should take one [Variant] parameter (the current array element) and return a boolean value.
268269
[codeblock]
269270
func _ready():
270271
print([1, 2, 3].filter(remove_1)) # Prints [2, 3].
@@ -379,7 +380,8 @@
379380
<argument index="0" name="method" type="Callable">
380381
</argument>
381382
<description>
382-
Calls the provided [Callable] for each element in array and replaces them with return value of the method.
383+
Calls the provided [Callable] for each element in the array and returns a new array filled with values returned by the method.
384+
The callable's method should take one [Variant] parameter (the current array element) and can return any [Variant].
383385
[codeblock]
384386
func _ready():
385387
print([1, 2, 3].map(negate)) # Prints [-1, -2, -3].
@@ -510,14 +512,15 @@
510512
<argument index="1" name="accum" type="Variant" default="null">
511513
</argument>
512514
<description>
513-
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.
515+
Calls the provided [Callable] for each element in array and accumulates the result in [code]accum[/code].
516+
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].
514517
[codeblock]
515518
func _ready():
516-
print([1, 2, 3].reduce(factorial, 1)) # Prints 6.
517-
print([1, 2, 3].reduce(func(accum, number): return accum * number)) # Same as above, but using lambda function.
519+
print([1, 2, 3].reduce(sum, 10)) # Prints 16.
520+
print([1, 2, 3].reduce(func(accum, number): return accum + number, 10)) # Same as above, but using lambda function.
518521

519-
func factorial(accum, number):
520-
return accum * number
522+
func sum(accum, number):
523+
return accum + number
521524
[/codeblock]
522525
</description>
523526
</method>

0 commit comments

Comments
 (0)