Array Methods: find, sum, sort

Built-in reduction, locator and ordering methods that replace hand-written loops.

Unpacked arrays and queues come with methods that do in one call what would otherwise be a loop: totals, minimum and maximum, finding matching elements and their positions, sorting. The optional with (...) clause adds a condition or an expression per element.

Three families

byte b[] = '{100, 100}; int total = b.sum(); // wraps int total = b.sum() with (int'(item));

sum() returns the element type, so summing bytes overflows at 127. Cast each item to a wider type in the with clause.

What type does find() return?

A queue of the element type (even if only one or zero elements match). find_index() returns a queue of int indices.

How do you count the elements of q that are negative?

q.sum() with (int'(item < 0)).