|
264 | 264 | <argument index="0" name="method" type="Callable">
|
265 | 265 | </argument>
|
266 | 266 | <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. |
268 | 269 | [codeblock]
|
269 | 270 | func _ready():
|
270 | 271 | print([1, 2, 3].filter(remove_1)) # Prints [2, 3].
|
|
379 | 380 | <argument index="0" name="method" type="Callable">
|
380 | 381 | </argument>
|
381 | 382 | <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]. |
383 | 385 | [codeblock]
|
384 | 386 | func _ready():
|
385 | 387 | print([1, 2, 3].map(negate)) # Prints [-1, -2, -3].
|
|
510 | 512 | <argument index="1" name="accum" type="Variant" default="null">
|
511 | 513 | </argument>
|
512 | 514 | <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]. |
514 | 517 | [codeblock]
|
515 | 518 | 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. |
518 | 521 |
|
519 |
| - func factorial(accum, number): |
520 |
| - return accum * number |
| 522 | + func sum(accum, number): |
| 523 | + return accum + number |
521 | 524 | [/codeblock]
|
522 | 525 | </description>
|
523 | 526 | </method>
|
|
0 commit comments