-
Notifications
You must be signed in to change notification settings - Fork 0
FoldLeft
The listutils:fold_left
function folds the list into one value from the smallest index to the largest index, using a fold function.
Input is given using the listutils:in
storage:
Field | Meaning |
---|---|
List |
The list to fold. |
Optionally, a fold function can be specified in the $listutils.function listutils.in
score. This argument defaults to function 0, which sums all elements in the list.
After defining the input, run the function listutils:fold_left
.
Various fold functions can be used to fold a list. For more about fold functions, see the Fold functions page.
Errors that display when executing the listutils:fold_left
function as a player in debug mode.
Error | Message |
---|---|
TBD | TBD |
The success of the operation is stored in the $listutils.success listutils.out
score. This score is 1
on success and 0
otherwise. By default, the success is always 1
when not running in debug mode. This can be changed in the fold function.
The result of the operation is stored inside the Data
field in the listutils:out
storage, or in the $listutils.result listutils.out
score. Which one is used depends on the fold function.
Take the example list ExampleList: ["foo", "Hello World!", "foo", "bar"]
in the listutils:examples
storage. We want to take the average length of a string in the list:
# Get list length.
execute store result score $length var run data get storage listutils:examples ExampleList
# Add List to the input.
data modify storage listutils:in List set from storage listutils:examples ExampleList
# Add the fold function to the input.
scoreboard players set $listutils.function listutils.in 0
# Call the function.
function listutils:fold_left
# Divide the result by the length of the list.
scoreboard players operation $listutils.result listutils.out /= $length var
This would return 30
in the $listutils.result listutils.out
score as the result of the operation. The final average is 7
.