Collections

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.

BIF

Description

Example

BIF

Description

Example

pop

Removes and returns the first element from the collection.

Person p = plist.pop();

drop

Removes and returns the last element from the collection.

Person p = plist.drop();

length

Returns the current length (number of elements) in the list.

int len = plist.length();

first

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

Car c = person.cars.first();

last

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

Car c = person.cars.last();

append

Adds an element to the back of a list.

person.cars.append(c);

prepend

Adds an element to the front of the list.

person.cars.prepend(c);

get

Returns an element in the specific location in the list.

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

add

Adds an element to the specified position in the list.

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

remove

Removes the element at the specified position in the list.

p.children.remove(2);

sortAsc

Sorts the list in ascending order.

numbers.sortAsc();

sortDesc

Sorts the list in descending order.

numbers.sortDesc();

clear

Removes all elements from the list.

p.cars.clear();

notify

Prompts 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”);

select

Returns 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