By Daniel · January 3, 2009
Folds a ds_list from left to right and returns the result. This works as follows. The first sub-result is obtained by executing scr with two arguments: init, and the first element of the list. The next sub-result is obtained by combining this sub-result with the second element of the list. This process is repeated until the whole list has been traversed, at which point the final result is returned.
For example, to sum the elements in a list, you could write a simple script named add(n1, n2) and call ds_list_foldr(L, add, 0).
/* ds_list_foldr(id, scr, init) * * Folds a ds_list from left to right and returns the result. This works as * follows. The first sub-result is obtained by executing scr with two * arguments: init, and the first element of the list. The next sub-result * is obtained by combining this sub-result with the second element of the list. * This process is repeated until the whole list has been traversed, at which * point the final result is returned. * * For example, to sum the elements in a list, you could write a simple script * named add(n1, n2) and call ds_list_foldr(L, add, 0). */ var result, size, i; size = ds_list_size(argument0); result = script_execute(argument1, argument2, ds_list_find_value(argument0, 0)); for (i = 1; i < size; i += 1) result = script_execute(argument1, result, ds_list_find_value(argument0, i)); return result;
Categories: Data processing
There are no comments to display.
You must be signed in to post comments.