Collections

 

Declaration and Assignment

A collection is declared using square brackets:

Declaring a new collection
Person [] plist;

A collection can be populated by:

  • adding objects one at a time to an empty collection
  • calling a selector BIF to query data - see Querying Data for details
  • returning a subset from another collection - see subset function below
Assigning the result of the all selector to a new collection
Person [] plist = Person:all();

 

 

 

Collection Built-in Functions

These BIFs can be called on any variable that holds a collection instance.

BIFDescriptionExample
popRemoves and returns the first element from the collection.

Person p = plist.pop();

dropRemoves and returns the last element from the collection.Person p = plist.drop();
lengthReturns the current length (number of elements) in the list.

int len = plist.length();

firstReturns the first element in the list (without removing it).

Car c = person.cars.first();

lastReturns the last element in the list (without removing it).

Car c = person.cars.last();

appendAdds an element to the back of a list.

person.cars.append(c);

prependAdds an element to the front of the list.person.cars.prepend(c);
getReturns an element in the specific location in the list.

Person child = p.children.get(2);

addAdds an element to the specified position in the list.

p.children.add(1, Person:new());

removeRemoves the element at the specified position in the list.

p.children.remove(2);

sortAscSorts the list in ascending order.

numbers.sortAsc();

sortDescSorts the list in descending order.

numbers.sortDesc();

clearRemoves all elements from the list.

p.cars.clear();

notifyPrompts Helium to send notifications to the users associated with the objects in the collection, if the object declaration was annotated with @Role (function also available on singular object instances).

plist.notify(“description.key”, sms.content.key”, email.subj.key”, email.content.key”);

selectReturns a subset of the items in the original collection as a new collection. Takes selector BIFs as parameter. See Querying Data for more on selector BIFs.Person[] persons = Person:all();
Person[] subset = persons.select(equals(name, "A"));

 

 

 

Additional Mentions and References