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.
Program: Realaxy ActionScript Editor (RASE) Beta 10 (build 8177+)
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
Nevertheless, if you have even a small experience with modern functional languages, you will find AS3 potential of working with collections very limited.
Well, let’s introduce the Collections AS3 language extension that is available in RASE Beta 10, build 8177+.
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
- Then peek only those that fulfil a condition (“
- Do something with every picked element (“
- Convert them to Strings (“
- Finally, to cycle through the
listand to trace the result.
selectMany — these operations are easy to use when you’re building a query.
containsAll, contains work perfectly in conditional phrases (“
if” statement, etc).
To modify a list, go ahead to use a wide range of weapons —
For those who definitely have an eye for perversion :D we have prepared a bunch of operations like
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.
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 "
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
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 is quite similar to a trivial
Dictionary that holds some useful methods —
containsValue (useful for checks and conditions),
pushMap (to merge values),
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
Instead of Conclusion
The new Collections language extension allows you:
- to improve your AS3 code making it more concise
- to make your code more human-readable
- to facilitate turning your sequential code into parallel.