Skip to content
Dima Kruk edited this page Jan 23, 2013 · 32 revisions

The Collections language is designed to extend the basic functionality of ActionScript3, adding a new functionality inspired by modern languages like Scala, Groovy or Ruby. The language extension Collections supports two types: List and Map. Map is quite similar to a trivial Dictionary that holds a number of useful methods, that are specified below.

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

###Basic Operation with Lists

###Binary Operations with Lists

###Closures Operations with Lists

###Closures Operations with List's element

###Basic Operations with Maps

##Creating an instance of Collections

You can initialize a new instance:

// a new list
var myList :list<int> = new list<int>{0, 1, 1, 2, 3, 5, 8, 13};
//  a new map
var myMap :map<String, int> = new map<String, int>{"Hello" = 1, "World" = 2};
// a new map (more compact syntax)
var myMap :map<String, int> = { 
  first :1,
  second :2,
  third :3 
};

or convert it from an existing array or vector:

var myArray :Array = new Array(0, 1, 1, 2, 3, 5, 8, 13);
myList = myArray.toList;
var myVector :Vector.<int> = new <int>[0, 1, 1, 2, 3, 5, 8, 13];
myList = myVector.toList;

##Methods Chaining Support

Yes, the Collections language really supports methods chaining, i.e. a sequential invoking multiple method calls.

// methods chaining with collections language
var l :list<int> = new list<int>{1, 1, 2, 3, 5, 8, 13}; 
l.where{it => it > 5 && it % 2 == 0} 
  .select{it => it.toFixed(2)} 
  .select{it => "number " + it} 
  .forEach{it => trace it};

In our sample we’ve created a list, which can contain only int values.

  1. Then, we select only those that fulfill 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. You can find all the concerning information in this article.

Same actions without Collections language (pure AS3).

var a :Array = new Array(1, 1, 2, 3, 5, 8, 13); 
var b :Array = new Array(); 
var c :Array = new Array(); 
var d :Array = new Array();

for (var i :int = 0;i < a.length;i++) { 
  if (a[i] > 5 && i[i] % 2 == 0) { 
    b.push(a[i]); 
  } 
} 
for (var j :int = 0;j < b.length;j++) { 
  c.push(j.toFixed); 
} 
for (var k :int = 0;k < c.length;k++) { 
  d.push("number" + j); 
} 
for (var m :int = 0;m < d.length;m++) { 
  trace m; 
}

##Basic Operation with Lists ###.add( element ) :T

Add an element to the list.

// add an element a to myList
myList.add(a);

###.addAll( list ) :list<T>

Add a list of elements to the current list.

// add a, b and c to myList
myList.addAll(new list<int>{a, b, c});

###.addFirst( element ) :T

Add an element's a first element to the list.

// add an element to q as the first one in myList
myList.addFirst(q); 

###.clear :list<T>

Clear all elements of the list.

myList.clear;

###.contains( element ) :Boolean

Test if the list contains an element from the argument.

// test if myList contains a
myList.contains(a);

###.containsAll( list ) :Boolean

Test if the current list contains elements from the argument.

// test if myList contains a, b and c
myList.containsAll(new list<int>{a, b, c});

###.copy :list<T>

Duplicate the list.

myList.copy;

###.cut( numberOfElements ) :list<T>

Get elements from the start, excluding a number of elements from the end.

// get myList, excepting n elements from the end
myList.cut(n);

###.distinct :list<T>

Get all unique elements from the list.

myList.distinct;

###.first :T

Get the first element from the list.

myList.first;

###.get( index ) :T

Get an element by index.

// get an element of myList with index n
myList.get(n);

###.head( index ) :list<T>

Get a number of starting elements from the list specified in the argument.

// get the first n elements of myList
myList.head(n);

###.indexOf( element ) :int

Get the index of an element on the current list specified in the argument.

// get the index of the element n in myList
myList.indexOf(n);

###.insert( index, element ) :T

Insert the specified element at the specified position of the current list.

// insert n at position m to myList
myList.insert(m, n);

###.isEmpty :Boolean

Test if the list is empty.

myList.isEmpty;

###.isNotEmpty :Boolean

Test if the list is not empty.

myList.isNotEmpty;

###.join( delimiter ) :String

Join all the elements of the list to a string (separated with s string delimiter).

myList.join(",");

###.last :T

Get the last element of the list.

myList.last;

###.page( fromElement, toElement ) :list<T>

Get the elements of the current list from the first specified element to the second specified element.

// get a subsequence from element m to element n
myList.page(m, n);

###.remove( element ) :T

Remove the element from the list.

myList.remove(k);

###.removeAll( list ) :list<T>

Remove elements from the list.

// remove a, b and c from myList
myList.removeAll(new list<int>{a, b, c}); 

###.removeAt( index ) :T

Remove an element at some index from the list.

// remove the element stored at the index n of myList
myList.removeAt(n); 

###.removeFirst :T

Remove the first element from the list.

myList.removeFirst;

###.removeHead( length ) :list<T>

Remove a specified number of starting elements from the current list.

// remove the first n elements from myList
myList.removeHead(n);

###.removeLast :T

Remove the last element from the list.

myList.removeLast;

###.removeTail( length ) :list<T>

Remove a specified number of ending elements from the current list.

// remove the last m elements from myList
myList.removeTail(m);

###.reverse :list<T>

Create a list with elements in the reverse order.

myList.reverse;

###.set( index, element ) :T

Set the specified element to the specified index.

// Set the element n at the index m
myList.set(m, n);

###.size :int

Get the size of the list.

myList.size;

###.skip( numberOfElements ) :list<T>

Skip the specified number of elements from the beginning of the current list.

// skip m elements from the beginning
myList.skip(m);

###.subList( fromIndex, toIndex ) :list<T>

Create a list containing all the elements between the specified indexes of the current list.

// get all elements from index m to index n of myList
myList.subList(m, n); 

###.tail( numberOfElements ) :list<T>

Get the specified number of last elements of the current list.

// get the last n elements from the list
myList.tail (n);

###.toArray :Array

Convert the list to array.

myList.toArray;

###.toVector :Vector<T>

Convert the list to vector.

myList.toVector;

##Binary Operations with Lists

###.concat( list ) :list<T>

Produce a list concatenation.

myList.concat(new list<int>{a, b, c});

###.disjunction( list ) :list<T>

Produce an exclusive disjunction of the specified arguments with the current list.

// disjoint a, b and c from myList
myList.disjunction(new list<int>{a, b, c});

###.except( list ) :list<T>

Produce a new list containing the elements of the current list excluding specified element.

// get all elements of mylist excluding a, b and c
myList.except(new list<int>{a, b, c});

###.intersect( list ) :list<T>

Produce an intersection of the current list with the specified elements.

// intersect myList with a, b and c
myList.intersect(new list<int>{a, b, c}); 

###.union( list ) :list<T>

Produce a combining of the current list with the specified elements.

// combine myList and a, b and c
myList.union(new list<int>{a, b, c}); 

##Closures Operations with Lists

###.all( condition( element ) ) :Boolean

Check if all elements of the list match the condition.

// return true if all elements of myList are greater than a
myList.all{it => it > a};

###.any( condition( element ) ) :Boolean

Check if any element of the list matches the condition.

// return true if any element of myList equals to b
myList.any{it => it == b};

###.fill( yield( element ) ) :void

Fill the list with values.

// fill myList with a, b and c
myList.fill{=>
  yield a; 
  yield b; 
  yield c; 
};

###.findFirst( condition( element ) ) :T

Find the first element of the list, that matches the condition.

// the first element of myList that is less than c
myList.findFirst{any => any < c};

###.findLast( condition( element ) ) :T

Find the last element of the list, that matches the condition.

// the last element of myList that is less than c
myList.findLast{any => any < c};

###.foldLeft( operation( element ), seed )

Perform left folding.

var myList :list<int> = new list<int>{1, 2, 3, 4, 5}; 
myList.foldLeft({it, s => it * s}, 2); // assert 240

###.foldRight( operation( element ), seed )

Perform right folding.

var myList :list<int> = new list<int>{1, 2, 3, 4, 5}; 
myList.foldRight({it, s => it + s}, 10); // assert 25

###.forEach( operation( element ) ) :void

Execute an operation for each element.

// trace each element
myList.forEach{element => trace element};

###.reduceLeft( operation( elementA, elementB ) )

Perform left reduction.

var myList :list<int> = new list<int>{1, 2, 3, 4, 5};
myList.reduceLeft{a, b => a + b}; // assert 15

###.reduceRight( operation( elementA, elementB ) )

Perform right reduction.

var myList :list<int> = new list<int>{1, 2, 3, 4, 5}; 
myList.reduceRight{a, b => a * b}; // assert 120

###.removeWhere( condition( element ) ) :list<T>

Filter out the matched elements.

// removes all elements of myList with zero value
myList.removeWhere{it => it == 0};

###.select( operation( element ) ) :list<T'>

Modify each element and transform to another type when needed.

var myList :list<int> = new list<int>{1, 2, 3}; 
myList.select{it => "|" + it + "|"}; // assert ["|1|", "|2|", "|3|"]

###.selectMany( operation( element ) ) :list<T>

Transform each element to a list, resulting a plain list containing all elements.

var myList :list<int> = new list<int>{1, 2, 3}; 
myList.selectMany{it => new list<int>{1, 0}}; // assert [1, 0, 1, 0, 1, 0]

###.sort( operation( elementA, elementB ) ) :list<T>

Sort list.

var myList :list<int> = new list<int>{1, 2, 3}; 
myList = myList.sort{a, b => b - a}; // reverse sorting, i.e. result is 3, 2, 1

###.where( condition( element ) ) :list<T>

Include only matched element.

// include only elements less than b
myList.where{element => element < b};

###( numberOfTimes ).times( operation(counter ) )

Execute some action a few times.

var dump :String; 
(3).times({i => dump += "" + i}); // dump == "123"

##Closures Operations with List's element ###.index :int Get index of current element.

var c : int = 0; 
[1, 2, 3].toList<int>.forEach{it => assert it.index equals c++};

###.next :T Get next element from the list.

var c : int = 1; 
var list : list<int> = [1, 2, 3].toList<int>; 
list.forEach{it => 
    if (c != 2) { 
      assert it.next equals list[c++]; 
    } 
  };

###.hasNext :Boolean Test if the list has next element.

var c : int = 0; 
var list : list<int> = [1, 2, 3].toList<int>; 
list.forEach{it => 
    if (c++ != 2) { 
      assert it.hasNext; 
    } else { 
      assert !(it.hasNext); 
    } 
  };

###.previous :T Get previous element from the list.

var c : int = -1; 
var list : list<int> = [1, 2, 3].toList<int>; 
list.forEach{it => 
    if (c != -1) { 
      assert it.previous equals list[c]; 
    } 
    c++; 
  };

###.hasPrevious :Boolean Test if the list has previous element.

var c : int = 0; 
var list : list<int> = [1, 2, 3].toList<int>; 
list.forEach{it => 
    if (c++ != 0) { 
      assert it.hasPrevious; 
    } else { 
      assert !(it.hasPrevious); 
    } 
  };

##Basic Operations with Maps ###.keys :list<keyT>

Get all the keys from the map.

myMap.keys;

###.containsKey( key ) :Boolean

Test if the map contains the key “Hello”.

var myMap :map<String, int> = new map<String, int>{"Hello" = 1, "World" = 2}; 
myMap.containsKey("Good bye"); // false
myMap.containsKey("Hello"); // true

###.containsValue( value ) :Boolean

Test if the map contains the value 2.

var myMap :map<String, int> = new map<String, int>{"Hello" = 1, "World" = 2}; 
myMap.containsValue(4); // false
myMap.containsValue(2); // true

###.clear :map<keyT, valueT>

Clear all elements of the map.

myMap.clear;

###.putAll( map ) :void

Put to the current map all the elements of a map in parameter.

var myMap :map<String, int> = new map<String, int>{"Hello" = 1, "World" = 2};
// "Hello" = 1, "World" = 2, "Lorem" = 3, "Ipsum" = 4}
myMap.putAll(new map<String, int>{"Lorem" = 3, "Ipsum" = 4});

###.removeKey( key ) :valueT

Remove a single key from the map.

var myMap :map<String, int> = new map<String, int>{"Hello" = 1, "World" = 2}; 
myMap.removeKey("World"); //myMap={"Hello" = 1}

###.values :list<valueT>

Get all the values from the map.

myMap.values;
Clone this wiki locally