Idea

Serge A avatar image
4 Likes"
Serge A suggested

Array.filter(), Array.map() and Array.reduce()

The addition of the dynamic Arrays of Variants to Flexscript was a huge improvement of the FlexSim scripting language. It's an catch-all aggregate data structure. Personally, I use it as a replacement for all other aggregate types missing in Flexscript (tuples, structs, sets, associative arrays). And it is mostly a satisfactory substitute.

But Array is not perfect (yet). Apart from a missing .sort() method, the features I'd like to see implemented in Flexscript Array are filter, map and reduce methods (or functions). Many algorithms can be easily expressed in terms of these steps. Certainly, for-loop can replace all of them, but I find that sometimes it is just easier to reason about the code if it relies on common functional constructs.

A note on possible syntax extensions. Flexscript still lacks function literals, but it already supports function-like blocks in some commands (like in forobjecttreeunder()). Being able to write a reducer or mapping function inline would be very helpful.

This is an example of how this extension might look in code:

// a hypothetical .reduce() method example:
// mimicking an associative array with an Array of Arrays

Array operators = [["Alice",37], ["Bob",42], ["Charlie",39]];
int bobsAge = operators.reduce(0 /*accumulator a*/, {
    // reducer(a, elem) : return a
if (!a && elem[1] == "Bob") { return elem[2]; // 42 } return a; }); // or just bobsAge = operators.reduce(0, { return (!a && elem[1] == "Bob"]) ? elem[2] : a;
});
flexscriptfeature request
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

No Comments

Your Opinion Counts

Share your great idea, or help out by voting for other people's ideas.