Idea

Serge A avatar image
2 Likes"
Serge A suggested Serge A edited

functools - a user library with Array utilities (sort, map, filter), and support for sets and alists (key-value pairs)

I'd like to share a small library of user commands with functions missing in the built-in Array class.

functools_0.8.1.fsl

1. Sorting arrays and trees

To reorder Array elements or tree nodes, use commands like sortarray, sortarray_inplace, sorttree, sorttree_inplace.

Usage:

Array xs = [2,1,3];
return sortarray(xs); // new Array

or

Array xs = [2,1,3];
sortarray_inplace(xs);
return xs; // modified Array

or

/* sort by absolute values */
Array xs = [3,-4,2,1,5];
Array ys = sortarray(xs, "return fabs(%1);");
return ys; // [1,2,3,-4,5];

2. Arrays as associative arrays

There are some commands which allow to use Arrays as associative arrays, to store key-value pairs, similar to association lists Lisp. The commands are: aget, aupdate, akeys.

Usage:

Array map = []; // map stores key-value pairs
aupdate(map, "name", "Alice"); // map is now ["name", "Alice"]
aupdate(map, "age", 7.5); // map is now ["name", "Alice", "age", 7.5]
return aget(map, "name"); // returns "Alice"

3. Sets

You can use Array to represent a "set", un unordered collection of distinct objects. These commands operate on Arrays as if they were sets: set_union, set_difference, set_intersection, unique.

Usage:

set_union([1,2,3], [5,4,3,2]) // [1,2,3,4,5]

set_intersection([1,3,5], [3,4,5]) // [3,5]

set_difference([1,2,3,4], [5,4,3]) // [1,2]

4. Other higher order functions

Some additional tools can be helpful for functionally inclined users: foldarray, maparray, filterarray, foldtree. All of them may take function parameter as a string or treenode, and will build a temporary nodefunction if necessary (variant2lambda, purgelambda).

Usage:

int sumOfSquares = foldarray([3,4,5], 0, "return %1 + %2*%2;");
return sumOfSquares; // 50 = ((0 + 3^2) + 4^2) + 5^2

Caveat. Due to limitations of Flexscript and the necessity to compile nodefunctions, these higher order functions are slower than equivalent hand-written loops. But sometimes they can be more expressive.

Installation

Save functools-0.8.1.fsl (attached to this post) in Documents\FlexSim 2020 Projects\libraries. Open a model where you want to use these commands. Use File/Open User Libraries... and select the .fsl file.

The library auto-installs user commands into the model when opened. Expect compiler errors on the first opening, but it should work fine after that. I used these commands across several projects, but use them at your own risk.

I hope that Flexscript will eventually support this functionality out of the box, and this library will no longer be necessary.

arrayuser librarysortingkey-valuesets
functools-081.fsl (8.3 KiB)
functools-081.fsx (47.0 KiB)
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

·

Write a Comment

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

Your Opinion Counts

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

Related Ideas