List
Generic list
Example
function : Example() ~ Nil {
# insert elements
list := Collection.List->New()<FloatRef>;
list->AddBack(33.3);
list->AddFront(66.6);
list->AddBack(99.9);
# get size
list->Size()->PrintLine();
# get first and last item
list->Front()->PrintLine();
list->Back()->PrintLine();
# iterate
while(list->More()) {
list->Get()->PrintLine();
list->Next();
};
# iterate backward
backwards := list->BackwardIterator()<FloatRef>;
while(backwards->More()) {
backwards->Get()->PrintLine();
backwards->Next();
};
}Operations
- New
- AddBack
- AddFront
- Back
- BackwardIterator
- Each
- Empty
- Filter
- Forward
- ForwardIterator
- Front
- Get
- Insert
- IsBack
- IsEmpty
- IsFront
- Limit
- Map
- More
- Next
- Previous
- Reduce
- Remove
- RemoveBack
- RemoveFront
- Rest
- Rewind
- Size
- ToArray
- ToString
AddBack # native
Adds a value to the end
method : public : native : AddBack(value:H) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to append |
Example
list := Collection.List->New()<String>;
list->AddBack("first");
list->AddBack("second");
list->Back()->PrintLine(); # secondAddFront # native
Adds a value to the front
method : public : native : AddFront(value:H) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to prepend |
Example
list := Collection.List->New()<String>;
list->AddBack("second");
list->AddFront("first");
list->Front()->PrintLine(); # firstBack #
Returns the last element in the list
method : public : Back() ~ HReturn
| Type | Description |
|---|---|
| H | last element in the list, Nil if the list is empty |
Example
list := Collection.List->New()<String>;
list->AddBack("first");
list->AddBack("last");
list->Back()->PrintLine(); # lastBackwardIterator #
Instance of a backward iterator
method : public : BackwardIterator() ~ BackwardIterator<H>Return
| Type | Description |
|---|---|
| BackwardIterator<H> | Backward iterator |
Example
list := Collection.List->New()<String>;
list->AddBack("x");
list->AddBack("y");
it := list->BackwardIterator()<String>;
while(it->More()) {
it->Get()->PrintLine();
it->Next();
};Each #
Function called for each element
method : public : Each(f:(H)~Nil) ~ List<H>Parameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Nil | function called |
Example
list := Collection.List->New()<String>;
list->AddBack("a");
list->AddBack("b");
list->Each(\(s : String) ~ Nil { s->PrintLine(); });Empty #
Clears the list
method : public : Empty() ~ NilExample
list := Collection.List->New()<String>;
list->AddBack("x");
list->Empty();
list->IsEmpty()->PrintLine(); # trueFilter #
Uses the given function to filter out values
method : public : Filter(f:(H)~Bool) ~ List<H>Parameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Bool | function to use a filter. If the function evaluates to true the value is added to the collection. |
Return
| Type | Description |
|---|---|
| List<H> | filtered list |
Example
list := Collection.List->New()<String>;
list->AddBack("apple");
list->AddBack("banana");
list->AddBack("apricot");
filtered := list->Filter(\(s : String) ~ Bool { return s->StartsWith("a"); });
filtered->Size()->PrintLine(); # 2Forward #
Moves the pointer to the end of the list
method : public : Forward() ~ NilExample
list := Collection.List->New()<String>;
list->AddBack("a");
list->AddBack("b");
list->Forward();
list->Get()->PrintLine(); # bForwardIterator #
Instance of a forward iterator
method : public : ForwardIterator() ~ ForwardIterator<H>Return
| Type | Description |
|---|---|
| ForwardIterator<H> | forward iterator |
Example
list := Collection.List->New()<String>;
list->AddBack("x");
list->AddBack("y");
it := list->ForwardIterator()<String>;
while(it->More()) {
it->Get()->PrintLine();
it->Next();
};Front #
Returns the first element in the list
method : public : Front() ~ HReturn
| Type | Description |
|---|---|
| H | first element in the list, Nil if the list is empty |
Example
list := Collection.List->New()<String>;
list->AddBack("first");
list->AddBack("last");
list->Front()->PrintLine(); # firstGet #
Gets the value that's currently pointed to
method : public : Get() ~ HReturn
| Type | Description |
|---|---|
| H | value value |
Example
list := Collection.List->New()<String>;
list->AddBack("hello");
list->Rewind();
list->Get()->PrintLine(); # helloInsert # native
Inserts a value into the list based upon the pointer location
method : public : native : Insert(value:H) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to insert |
Example
list := Collection.List->New()<String>;
list->AddBack("a");
list->AddBack("c");
list->Rewind();
list->Insert("b");
list->Size()->PrintLine(); # 3IsBack #
Checks to see if the pointer is at the end of the list
method : public : IsBack() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if pointer is at the end of the list, false otherwise |
Example
list := Collection.List->New()<String>;
list->AddBack("a");
list->AddBack("b");
list->Forward();
list->IsBack()->PrintLine(); # trueIsEmpty #
Checks to see if the list is empty
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if empty, false otherwise |
Example
list := Collection.List->New()<String>;
list->IsEmpty()->PrintLine(); # true
list->AddBack("x");
list->IsEmpty()->PrintLine(); # falseIsFront #
Checks to see if the pointer is at the front of the list
method : public : IsFront() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if pointer is at the front of the list, false otherwise |
Example
list := Collection.List->New()<String>;
list->AddBack("a");
list->AddBack("b");
list->Rewind();
list->IsFront()->PrintLine(); # trueLimit #
Returns a limited list
method : public : Limit(l:Int) ~ List<H>Parameters
| Name | Type | Description |
|---|---|---|
| l | Int | limit |
Return
| Type | Description |
|---|---|
| List<H> | limited list |
Example
list := Collection.List->New()<String>;
list->AddBack("a");
list->AddBack("b");
list->AddBack("c");
first2 := list->Limit(2);
first2->Size()->PrintLine(); # 2Map #
Maps the given function to each value in the list
method : public : Map(f:(H)~H) ~ List<H>Parameters
| Name | Type | Description |
|---|---|---|
| f | (H)~H | function to apply |
Return
| Type | Description |
|---|---|
| List<H> | newly calculated list |
Example
list := Collection.List->New()<String>;
list->AddBack("hello");
list->AddBack("world");
upper := list->Map(\(s : String) ~ String { return s->ToUpper(); });
upper->Size()->PrintLine(); # 2More #
Checks to see the pointer can be advanced
method : public : More() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if pointer can be advanced, false otherwise |
Example
list := Collection.List->New()<String>;
list->AddBack("x");
list->Rewind();
while(list->More()) {
list->Get()->PrintLine();
list->Next();
};New # constructor
Default constructor
New()Example
list := Collection.List->New()<String>;
list->AddBack("hello");
list->Size()->PrintLine(); # 1Next #
Advances the pointer
method : public : Next() ~ NilExample
list := Collection.List->New()<String>;
list->AddBack("a");
list->AddBack("b");
list->Rewind();
list->Next();
list->Get()->PrintLine(); # bPrevious #
Retreats the pointer
method : public : Previous() ~ NilExample
list := Collection.List->New()<String>;
list->AddBack("a");
list->AddBack("b");
list->Forward();
list->Previous();
list->Get()->PrintLine(); # aReduce #
Uses the given function to reduce the values
method : public : Reduce(a:H, f:(H,H)~H) ~ HParameters
| Name | Type | Description |
|---|---|---|
| a | H | initial value (i.e. accumulator) |
| f | (H,H)~H | function to use a reduce |
Return
| Type | Description |
|---|---|
| H | reduced vector |
Example
list := Collection.List->New()<String>;
list->AddBack("a");
list->AddBack("b");
list->AddBack("c");
joined := list->Reduce("", \(acc : String, s : String) ~ String { return acc + s; });
joined->PrintLine(); # abcRemoveBack #
Removes the last value from the list
method : public : RemoveBack() ~ NilExample
list := Collection.List->New()<String>;
list->AddBack("a");
list->AddBack("b");
list->RemoveBack();
list->Back()->PrintLine(); # aRemoveFront #
Removes the first value from the list
method : public : RemoveFront() ~ NilExample
list := Collection.List->New()<String>;
list->AddBack("a");
list->AddBack("b");
list->RemoveFront();
list->Front()->PrintLine(); # bRest #
List of all but first element
method : public : Rest() ~ List<H>Return
| Type | Description |
|---|---|
| List<H> | all but first element |
Example
list := Collection.List->New()<String>;
list->AddBack("a");
list->AddBack("b");
list->AddBack("c");
rest := list->Rest();
rest->Size()->PrintLine(); # 2
rest->Front()->PrintLine(); # bRewind #
Moves the pointer to the start of the list
method : public : Rewind() ~ NilExample
list := Collection.List->New()<String>;
list->AddBack("a");
list->AddBack("b");
list->Forward();
list->Rewind();
list->Get()->PrintLine(); # aSize #
Size of list
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | size of list |
Example
list := Collection.List->New()<String>;
list->AddBack("a");
list->AddBack("b");
list->Size()->PrintLine(); # 2ToArray #
Converts the list into an object array
method : public : ToArray() ~ H[]Return
| Type | Description |
|---|---|
| H[] | object array |
Example
list := Collection.List->New()<String>;
list->AddBack("a");
list->AddBack("b");
arr := list->ToArray();
arr[0]->PrintLine(); # aToString #
Formats the collection into a string. If an element implements the 'Stringify' interface, it's 'ToString()' is called.
method : public : ToString() ~ StringReturn
| Type | Description |
|---|---|
| String | string representation |
Example
list := Collection.List->New()<String>;
list->AddBack("foo");
list->AddBack("bar");
list->ToString()->PrintLine(); # [foo,bar]