Game Maker Games, Articles, Tutorials & More

Game Maker Network


Howdy, Guest! Please sign in or register an account.

Print

ds_list_foldl

By Daniel · January 3, 2009

Folds a ds_list from right to left and returns the result. This works as follows. The first sub-result is obtained by executing scr with two arguments: init, and the last element of the list. The next sub-result is obtained by combining this sub-result with the second-to-last 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_foldl(L, add, 0).

/* ds_list_foldl(id, scr, init)
 *
 * Folds a ds_list from right to left and returns the result. This works as
 * follows. The first sub-result is obtained by executing scr with two
 * arguments: init, and the last element of the list. The next sub-result
 * is obtained by combining this sub-result with the second-to-last 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_foldl(L, add, 0).
 */

var result, size, i;

size = ds_list_size(argument0);
result = script_execute(argument1,
                        argument2,
                        ds_list_find_value(argument0, size - 1));

for (i = size - 2; i >= 0; i -= 1)
  result = script_execute(argument1, result, ds_list_find_value(argument0, i));

return result;

Categories: Data processing

Comments

There are no comments to display.

Post a Comment

You must be signed in to post comments.

Advertisement