A New Functional Way Of Working With Collections In ActionScript3 using Realaxy Editor


Nearly every application you ever write uses collections. Work with collections can be called a basic routine activity that takes up to 60-80% of your time as long as you deal with algorithms. A collection is a general term that means something like “a bunch of similarly typed objects that are grouped together”. AS3, which is a modern language like any other, also provides a lot of convenient tools (like loops, sophisticated collections API, sorting, etc) to process collections. The only trouble is the lack of a functional approach that is so common for modern and trendy languages, such as Scala, Groovy or Ruby.


ProgramRealaxy ActionScript Editor (RASE) Beta 10 (build 8177+)
Difficulty: Intermediate
Estimated time: 7-12 minutes

The old-school classics: Arrays and Vectors

Generally, building a collection in ActionScript 3.0 can be done by using either arrays or vectors. They both have some makings of a perfect collection concept except none of them are ideal.

The principal trouble with arrays is that ActionScript does not currently support typed arrays - a single array may contain both primitives and objects, for example.


That means, every time you use an array in AS3, you need always take care of it introducing rules for each situation:


If you forget to set these rules, an item from AS3 array can do nothing on its own. Just compare the autocomplete capabilities between an Array item and a String:


Another opportunity to organize your data collections with ActionScript is the using of Vectors. This class was introduced since AS3 and Flash Player 10. Vector is an array whose elements all have the same data type. The base type can be any class, including built in classes and custom classes. The base type is specified when declaring a Vector variable as well as when creating an instance by calling the class constructor.

Another advanage of Vectors: array element access and iteration are much faster when using a Vector instance than when using an Array.

Nevertheless, if you have even a small experience with modern functional languages, you will find AS3 potential of working with collections very limited.

Collections Language

Well, let’s introduce the Collections AS3 language extension that is available in RASE Beta 10, build 8177+.


The complete vocabulary of methods with samples is available here: one for lists and another one for maps (scroll them down, they are really HUGE).

In order to not get lost in this jungle, let’s take a survey on some simple use case. The first true-life sample demonstrates the conciseness of a code.

We create a list, which can contain only int values.

  1. Then peek only those that fulfil a condition (“where”).
  2. Do something with every picked element (“select”).
  3. Convert them to Strings (“select”).
  4. Finally, to cycle through the list and to trace the result.


Where, select, selectMany — these operations are easy to use when you’re building a query.


Operations like all, any, containsAll, contains work perfectly in conditional phrases (“if” statement, etc).


To modify a list, go ahead to use a wide range of weapons — remove, removeAll, removeWhere, removeHead, removeTail, etc.


For those who definitely have an eye for perversion :D we have prepared a bunch of operations like foldLeft/foldRight, reduceLeft/reduceRight, intersect, etc.

Simply said, there are a plenty of various operations suited to every fancy and almost every task. In some operations you just transmit one or more values to it, in some other ones you add a closure.


A closure is, in fact, the same anonymous function, but with a number of small differences.


First, a Closure has a very concise syntax. Parameters don’t have a type declaration (to be more exact, they have it, but such declarations are hidden). A Closure has a special behavior “the last statement is a return value”. In actual fact, use “1;" (in the last line) instead of "return 1;

Second, it has a special view for single-line closures (they outnumber) — in such a closure a semi-colon on the end is hidden (for two reasons, readability and brevity).

In distinction from an anonymous function (and also as a counter to the mentioned Arrays and Vectors), the closure is type safe. That means, either autocomplete or type-check are working in closures.


To conclude, a Closure is a kind of function on steroids. It has a lot of tasty features and helps us to write everything quick and concise.

Lists and Maps

Collection language is intentionally simplified. It supports two types, a List and a Map. Map is quite similar to a trivial Dictionary that holds some useful methods — keys, values, containsKey, containsValue (useful for checks and conditions), pushMap (to merge values), removeKey, removeValue, etc.

Maps are smart and fail-safe. You can’t add a wrong key or value:


Maps work well with any lists and queries.


There are also conversion operations that can be utilized to facilitate embedding the new Collections extensions to your actual pure-ActionScript project. Just take a trivial array (or vector) and apply .toList operation. After processing a list (or a map) you can always convert it back to the old-school AS3 style using .toArray or .toVector operations.

Instead of Conclusion

The new Collections language extension allows you:

  1. codeorchestra posted this