Vector
Growable generic array
Example
function : Example() ~ Nil {
# insert elements
vector := Collection.Vector->New()<IntRef>;
vector->AddBack(4);
vector->AddBack(1);
vector->AddBack(5);
vector->AddBack(1);
vector->AddBack(0);
# remove last item
vector->RemoveBack();
# get size
vector->Size()->PrintLine();
# get elements
(vector->Get(0) + vector->Get(1))->PrintLine();
# print all items with a loop
each(item := vector) {
item->PrintLine();
};
# print all items with a function
vector->Each(Show(IntRef) ~ Nil);
}
function : Show(value : IntRef) ~ Nil {
value->PrintLine();
}Operations
- New
- AddBack
- All
- Any
- Compress
- Count
- Each
- Empty
- Filter
- FindFirst
- FindIndex
- First
- Get
- IsEmpty
- Last
- Limit
- Map
- Reduce
- Remove
- RemoveBack
- Reverse
- Set
- Size
- Swap
- ToArray
- ToString
- Zip
AddBack #
Adds a value to the end
method : public : AddBack(value:H) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to append |
Example
v := Collection.Vector->New()<String>;
v->AddBack("first");
v->AddBack("second");
v->Size()->PrintLine(); # 2All #
Checks if all elements match the predicate
method : public : All(f:(H)~Bool) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Bool | predicate function |
Return
| Type | Description |
|---|---|
| Bool | true if all elements match, false otherwise |
Example
v := Collection.Vector->New()<String>;
v->AddBack("hello"); v->AddBack("world");
v->All(\(s : String) ~ Bool { return s->Size() > 3; })->PrintLine(); # trueAny #
Checks if any element matches the predicate
method : public : Any(f:(H)~Bool) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Bool | predicate function |
Return
| Type | Description |
|---|---|
| Bool | true if any element matches, false otherwise |
Example
v := Collection.Vector->New()<String>;
v->AddBack("hello"); v->AddBack("world");
v->Any(\(s : String) ~ Bool { return s->StartsWith("h"); })->PrintLine(); # trueCompress #
Compresses the Vector freeing unused memory
method : public : Compress() ~ NilExample
v := Collection.Vector->New()<String>;
v->AddBack("x"); v->AddBack("y");
v->Compress();
v->Size()->PrintLine(); # 2Count #
Counts elements matching the predicate
method : public : Count(f:(H)~Bool) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Bool | predicate function |
Return
| Type | Description |
|---|---|
| Int | count of matching elements |
Example
v := Collection.Vector->New()<String>;
v->AddBack("apple"); v->AddBack("apricot"); v->AddBack("banana");
v->Count(\(s : String) ~ Bool { return s->StartsWith("a"); })->PrintLine(); # 2Each #
Function called for each element
method : public : Each(f:(H)~Nil) ~ Vector<H>Parameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Nil | function called |
Example
v := Collection.Vector->New()<String>;
v->AddBack("a"); v->AddBack("b");
v->Each(\(s : String) ~ Nil { s->PrintLine(); });Empty #
Clears the vector
method : public : Empty() ~ NilExample
v := Collection.Vector->New()<String>;
v->AddBack("x");
v->Empty();
v->IsEmpty()->PrintLine(); # trueFilter #
Uses the given function to filter out values
method : public : Filter(f:(H)~Bool) ~ Vector<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 |
|---|---|
| Vector<H> | filtered vector |
Example
v := Collection.Vector->New()<String>;
v->AddBack("apple"); v->AddBack("banana"); v->AddBack("avocado");
filtered := v->Filter(\(s : String) ~ Bool { return s->StartsWith("a"); });
filtered->Size()->PrintLine(); # 2FindFirst #
Finds the first element matching the predicate
method : public : FindFirst(f:(H)~Bool) ~ HParameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Bool | predicate function |
Return
| Type | Description |
|---|---|
| H | first matching element, or Nil if not found |
Example
v := Collection.Vector->New()<String>;
v->AddBack("apple"); v->AddBack("banana");
v->FindFirst(\(s : String) ~ Bool { return s->StartsWith("b"); })->PrintLine(); # bananaFindIndex #
Finds the index of the first element matching the predicate
method : public : FindIndex(f:(H)~Bool) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Bool | predicate function |
Return
| Type | Description |
|---|---|
| Int | index of first matching element, or -1 if not found |
Example
v := Collection.Vector->New()<String>;
v->AddBack("apple"); v->AddBack("banana");
v->FindIndex(\(s : String) ~ Bool { return s->StartsWith("b"); })->PrintLine(); # 1First #
Gets the first value
method : public : First() ~ HReturn
| Type | Description |
|---|---|
| H | first value, or Nil if not set |
Example
v := Collection.Vector->New()<String>;
v->AddBack("first"); v->AddBack("second");
v->First()->PrintLine(); # firstGet #
Gets an indexed value
method : public : Get(index:Int) ~ HParameters
| Name | Type | Description |
|---|---|---|
| index | Int | index |
Return
| Type | Description |
|---|---|
| H | value, or Nil if invalid index |
Example
v := Collection.Vector->New()<String>;
v->AddBack("a"); v->AddBack("b");
v->Get(0)->PrintLine(); # a
v->Get(1)->PrintLine(); # bIsEmpty #
Checks to see if the vector is empty
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if empty, false otherwise |
Example
v := Collection.Vector->New()<String>;
v->IsEmpty()->PrintLine(); # true
v->AddBack("x");
v->IsEmpty()->PrintLine(); # falseLast #
Gets the last value
method : public : Last() ~ HReturn
| Type | Description |
|---|---|
| H | last value, or Nil if not set |
Example
v := Collection.Vector->New()<String>;
v->AddBack("a"); v->AddBack("b");
v->Last()->PrintLine(); # bLimit #
Returns a limited list
method : public : Limit(l:Int) ~ Vector<H>Parameters
| Name | Type | Description |
|---|---|---|
| l | Int | limit |
Return
| Type | Description |
|---|---|
| Vector<H> | limited list |
Example
v := Collection.Vector->New()<String>;
v->AddBack("a"); v->AddBack("b"); v->AddBack("c");
first2 := v->Limit(2);
first2->Size()->PrintLine(); # 2Map #
Maps the given function to each value in the vector
method : public : Map(f:(H)~H) ~ Vector<H>Parameters
| Name | Type | Description |
|---|---|---|
| f | (H)~H | function to apply |
Return
| Type | Description |
|---|---|
| Vector<H> | newly calculated vector |
Example
v := Collection.Vector->New()<String>;
v->AddBack("hello"); v->AddBack("world");
upper := v->Map(\(s : String) ~ String { return s->ToUpper(); });
upper->Get(0)->PrintLine(); # HELLONew # constructor
Default constructor
New()Example
v := Collection.Vector->New()<String>;
v->AddBack("hello");
v->Size()->PrintLine(); # 1New # constructor
Copy constructor
New(values:H[])Parameters
| Name | Type | Description |
|---|---|---|
| values | H[] | values to copy |
Example
arr := String->New[2];
arr[0] := "a"; arr[1] := "b";
v := Collection.Vector->New(arr)<String>;
v->Size()->PrintLine(); # 2New # constructor
Copy constructor
New(values:Vector<H>)Parameters
| Name | Type | Description |
|---|---|---|
| values | Vector<H> | values to copy |
Example
src := Collection.Vector->New()<String>;
src->AddBack("x");
copy := Collection.Vector->New(src)<String>;
copy->Get(0)->PrintLine(); # xReduce #
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 value |
Example
v := Collection.Vector->New()<String>;
v->AddBack("a"); v->AddBack("b"); v->AddBack("c");
joined := v->Reduce("", \(acc : String, s : String) ~ String { return acc + s; });
joined->PrintLine(); # abcRemove #
Removes an indexed value
method : public : Remove(i:Int) ~ HParameters
| Name | Type | Description |
|---|---|---|
| i | Int | index |
Return
| Type | Description |
|---|---|
| H | value |
Example
v := Collection.Vector->New()<String>;
v->AddBack("a"); v->AddBack("b"); v->AddBack("c");
v->Remove(1)->PrintLine(); # b
v->Size()->PrintLine(); # 2RemoveBack #
Removes the last value
method : public : RemoveBack() ~ HReturn
| Type | Description |
|---|---|
| H | value |
Example
v := Collection.Vector->New()<String>;
v->AddBack("a"); v->AddBack("b");
v->RemoveBack()->PrintLine(); # b
v->Size()->PrintLine(); # 1Reverse #
Reverses element order
method : public : Reverse() ~ Vector<H>Return
| Type | Description |
|---|---|
| Vector<H> | reversed vector, if the vector is empty or hold 1 item then the original list is returned |
Example
v := Collection.Vector->New()<String>;
v->AddBack("a"); v->AddBack("b"); v->AddBack("c");
rev := v->Reverse();
rev->Get(0)->PrintLine(); # cSet #
Sets an indexed value
method : public : Set(value:H, index:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| value | H | value |
| index | Int | index |
Example
v := Collection.Vector->New()<String>;
v->AddBack("old");
v->Set("new", 0)->PrintLine(); # true
v->Get(0)->PrintLine(); # newSize #
Size of vector
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | size of vector |
Example
v := Collection.Vector->New()<String>;
v->AddBack("a"); v->AddBack("b");
v->Size()->PrintLine(); # 2Swap #
Swap two values in the vector
method : public : Swap(a:Int, b:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| a | Int | first value |
| b | Int | second value |
Return
| Type | Description |
|---|---|
| Bool | true if values were swapped |
Example
v := Collection.Vector->New()<String>;
v->AddBack("a"); v->AddBack("b");
v->Swap(0, 1)->PrintLine(); # true
v->Get(0)->PrintLine(); # bToArray #
Converts the vector into an object array
method : public : ToArray() ~ H[]Return
| Type | Description |
|---|---|
| H[] | object array |
Example
v := Collection.Vector->New()<String>;
v->AddBack("a"); v->AddBack("b");
arr := v->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
v := Collection.Vector->New()<String>;
v->AddBack("a"); v->AddBack("b");
v->ToString()->PrintLine(); # [a,b]Zip # function
Zip two vectors together
function : Zip() ~ Vector<Collection.Tuple.Pair<H,H>>Parameters
| Name | Type | Description |
|---|---|---|
Return
| Type | Description |
|---|---|
| Vector<CollectionTuplePair<H,H>> | list of pairs |
Example
a := Collection.Vector->New()<String>;
a->AddBack("x"); a->AddBack("y");
b := Collection.Vector->New()<String>;
b->AddBack("1"); b->AddBack("2");
zipped := Collection.Vector->Zip(a, b)<String>;
zipped->Get(0)->GetFirst()->PrintLine(); # xZip # function
Zip three vectors together
function : Zip() ~ Vector<Collection.Tuple.Triplet<H,H,H>>Parameters
| Name | Type | Description |
|---|---|---|
Return
| Type | Description |
|---|---|
| Vector<CollectionTupleTriplet<H,H,H>> | list of triplets |
Example
a := Collection.Vector->New()<String>;
a->AddBack("x");
b := Collection.Vector->New()<String>;
b->AddBack("y");
c := Collection.Vector->New()<String>;
c->AddBack("z");
zipped := Collection.Vector->Zip(a, b, c)<String>;
zipped->Size()->PrintLine(); # 1Zip # function
Zip four vectors together
function : Zip() ~ Vector<Collection.Tuple.Quartet<H,H,H,H>>Parameters
| Name | Type | Description |
|---|---|---|
Return
| Type | Description |
|---|---|
| Vector<CollectionTupleQuartet<H,H,H,H>> | list of quartets |
Example
a := Collection.Vector->New()<String>;
a->AddBack("p");
b := Collection.Vector->New()<String>;
b->AddBack("q");
c := Collection.Vector->New()<String>;
c->AddBack("r");
d := Collection.Vector->New()<String>;
d->AddBack("s");
zipped := Collection.Vector->Zip(a, b, c, d)<String>;
zipped->Size()->PrintLine(); # 1