Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. ActionScript

Querying (And Conquering) Big Collections in AS3 With Realaxy Editor


Good news, everyone. Everybody who works with arrays and vectors - so, all developers - will enjoy the new opportunities: since the late August 2011 build of Flash, Realaxy ActionScript Editor (RASE) supports the new Collections AS3 language extension. In this post we'll show you what it can do.

Realaxy logo

A collection is a general term that means, roughly, “a bunch of similarly typed objects that are grouped together”. Building a collection in ActionScript 3.0 can be done by using arrays, vectors, dictionaries, or objects; they each have some of the makings of a perfect concept. However, if you have even a basic acquaintance with any modern and trendy language like Scala, Groovy or Ruby, you'll definitely feel the lack of a functional approach in the pure AS3-way of processing Collections.

The Collections Language

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

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

In order to not get lost in this jungle, let's take a look at some simple use cases. The first true-life sample demonstrates the conciseness of collection code:

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

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

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

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

To modify a list, we have a wide range of weapons: remove, removeAll, removeWhere, removeHead, removeTail, etc.

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

Simply said, there are plenty of 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.

Lists and Maps

The collection language is intentionally simple. It supports two types: List and 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. They won't let you add an incorrect 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 the .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.

A Real World Example

To demonstrate how to get started with these collections, here's a step-by-step guide based on a trivial situation. Suppose your task is to create a "suggested users" list for a Twitter account. We have to process a very long collection of hundreds or thousands of objects, to retrieve a short list (that match a number of criteria) and to apply some operation on every item in that short list.

We'll not turn our attention to interacting with Twitter API, since our goal is only to show how to get started with the Collections language and to show advantages of a functional approach in working with collections in AS3.

Step 1: Creating a Project

Create a new project from scratch and name it Collections. If this is your first experience with the editor, we recommend you to read the Realaxy HelloWord Tutorial and/or Realaxy Overview For Beginners.

Step 2: Creating a Class (Beginning)

Now we need to create three classes to store the data structure: UserTwitterAccount and Message. We can do it from the Generate menu, which is available through right-clicking the code or pressing Ctrl+N.

Step 3: Creating a Class (In Progress)

Type the class name in a pop-up box.

Step 4: Creating a Class (Adding Fields)

Jump to the <<Field>> position and hit Enter.

Step 5: Creating a Class (More Fields)

Add the following fields: username, surname and id. The code will look like this:

Step 6: Adding Getters and Setters

Invoke the Ctrl+N menu again.

Step 7: Still Adding Getters and Setters

A new pop-up window will appear. Select all newly created fields and press OK.

Step 8: Adding a .toString() Method

Don't forget to add a text presentation to the User class. Add .toString() method — item 5 from the Ctrl-N menu (same as on screenshot to Step 2 and 6).

Step 9: Code Overview

The User class is ready. Its code will look like this:

Step 10: TwitterAccount and Message Classes

Using the same process as Steps 2-9 you have to create TwitterAccount and Message classes.


NB: To avoid a type error (like the one shown in the image above), you must import the Collections language from the Ctrl+L menu:

NB2: The TwitterAccount and Message classes should be cross-linked. In order for this to happen, after importing the Collections language, you have to create the Message class, jump back to the TwitterAccount class and complete the line that caused an error.

NB3: Don't forget to add getters, setters and a .toString() method.

Step 11: Main()

Now it's time to write some code in Main(). First, we need to add some fields. 

NB: Use the Smart Complete keyboard shortcut (Ctrl-Shift-Space) to save a little time while typing these phrases:

Since our tutorial is just a demo that shows a how to work with collections in AS3, we'll skip the part that refers how to get this data from Twitter API.

Let's just imagine that we already have:

  1. a list of our followers
  2. a list of users followedBefore,
  3. a very long list of potential candidates for following — candidatesLongList,
  4. and, of course, a candidatesShortList, which is empty at the moment.

The third collection can be extremly large, containing hundreds or even thousands of items. Our goal is to apply some sophisticated query and thus to cut off the needless items according to Buonarotti's principle “I saw the angel in the marble and carved until I set him free.”

Step 12: Building the Query

Jump to Main() constructor, and enter the candidatesLongList with its method “where” (hit Ctrl-Space to use autocompletion like in the screenshot below):

The following phrase will appear:

Don't be surprised, it's just a Closure, and "it" is just its parameter.

Closures (a small lyrical digression)

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

First, the Closure has a very concise syntax. Parameters don't have a type declaration (to be more precise, they have it, but such declarations are hidden). The Closure has a special behavior - “the last statement is a return value&rdquo - which means you should use “1;” (in the last line) instead of “return 1;

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

Unlike an anonymous function (and also as a counter to the aforementioned Arrays and Vectors), a closure is type safe. That means that autocomplete and type-checking will work in closures.

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

Step 13: Building the Query

Let's return to our unfinished closure. Add some code that will implement our "rules". This code will return a cut-down version of candidatesLongList that doesn't include any of our followers:

Then add another criterion:

Theoretically, the Collections language allows you to nest a lot of different conditions one by one. Let's add one more criterion (include users that have “Flash”, “ActionScript” or “Adobe” in their Biography field) using a regular expression:

Step 14: Obtaining the Result

Select the whole query and press Ctrl-Alt-V. A new variable will be introduced.

Now we are able to do whatever we want:

We would then copy the contents of result into candidatesShortList.

Step 15: Generating Pure AS3 Code

Build a module with Ctrl-F9 and take a look at the Output window. The generated pure AS3 code of Main() will look like this:

Seems a little bit unreadable, eh? Especially comparing with our DSL code:


The new Collections language extension allows you to:

  • improve your AS3 code making it more concise,
  • make your code more human-readable, and
  • facilitate turning your sequential code into parallel code.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.