Generic collections library: Vector (dynamic array), List (linked list), Map (ordered binary tree, O(log n)), Hash (hash table), Set, HashSet, SetHash, MultiMap, MultiHash, Stack, Queue, and Pair. Provides typed iteration, sorting, filtering, and functional operations (Reduce, Any, All). Compile with -lib gen_collect.
Map
Balanced ordered tree of 'Compare' keys and 'Base' values
Example
function : Example() ~ Nil {
# insert elements
map := Collection.Map->New()<IntRef, String>;
map->Insert(415, "San Francisco");
map->Insert(510, "Oakland");
map->Insert(925, "East Bay");
# get size
map->Size()->PrintLine();
# get value by key
map->Find(510)->PrintLine();
# get key/values
key_values := map->GetKeyValues()<Pair<IntRef, String>>;
each(key_value := key_values) {
key_value->GetFirst()->PrintLine();
};
# get values
values := map->GetValues()<String>;
each(value := values) {
value->PrintLine();
};
# check for key
map->Has(408)->PrintLine();
}Operations
Dict # function
Converts a vector pairs to a map
function : Dict(pairs:Vector<Collection.Tuple.Pair<K,V>>) ~ Map<K,V>Parameters
| Name | Type | Description |
|---|---|---|
| pairs | Vector<CollectionTuplePair<K,V>> | vector of pairs |
Return
| Type | Description |
|---|---|
| Map<K,V> | mapped pairs |
Example
pairs := Collection.Vector->New()<Collection.Tuple.Pair<String, IntRef>>;
pairs->AddBack(Collection.Tuple.Pair->New("one", IntRef->New(1))<String, IntRef>);
pairs->AddBack(Collection.Tuple.Pair->New("two", IntRef->New(2))<String, IntRef>);
map := Collection.Map->Dict(pairs)<String, IntRef>;
map->Find("one")->PrintLine(); # 1Each #
Function called for each element
method : public : Each(f:(K,V)~Nil) ~ Map<K,V>Parameters
| Name | Type | Description |
|---|---|---|
| f | (K,V)~Nil | function called |
Example
map := Collection.Map->New()<String, IntRef>;
map->Insert("a", IntRef->New(1)); map->Insert("b", IntRef->New(2));
map->Each(\(String, IntRef) ~ Nil : (k, v) => "{$k}={$v}"->PrintLine());Empty #
Clears the map
method : public : Empty() ~ NilExample
map := Collection.Map->New()<String, IntRef>;
map->Insert("a", IntRef->New(1));
map->Empty();
map->IsEmpty()->PrintLine(); # trueFilter #
Uses the given function to filter out values
method : public : Filter(f:(K)~Bool) ~ Map<K,V>Parameters
| Name | Type | Description |
|---|---|---|
| f | (K)~Bool | function to use a filter. If the function evaluates to true the value is added to the collection. |
Return
| Type | Description |
|---|---|
| Map<K,V> | filtered map |
Example
map := Collection.Map->New()<String, IntRef>;
map->Insert("a", IntRef->New(1)); map->Insert("b", IntRef->New(2));
filtered := map->Filter(\(String) ~ Bool : (k) => k->Equals("a"))<String, IntRef>;
filtered->Size()->PrintLine(); # 1Find #
Searches for a value in a map
method : public : Find(key:K) ~ VParameters
| Name | Type | Description |
|---|---|---|
| key | K | search key |
Return
| Type | Description |
|---|---|
| V | found value, Nil if not found |
Example
map := Collection.Map->New()<String, IntRef>;
map->Insert("pi", IntRef->New(3));
map->Find("pi")->PrintLine(); # 3
map->Find("e")->PrintLine(); # (Nil)GetKeyValues #
Get a collection of key-value pairs
method : public : GetKeyValues() ~ Collection.Vector<Pair<K,V>>Return
| Type | Description |
|---|---|
| CollectionVector<Pair<K,V>> | vector of key-value pairs |
Example
map := Collection.Map->New()<String, IntRef>;
map->Insert("a", IntRef->New(1)); map->Insert("b", IntRef->New(2));
kvs := map->GetKeyValues()<Collection.Pair<String, IntRef>>;
each(kv := kvs) { "{$kv->GetFirst()}={$kv->GetSecond()}"->PrintLine(); }GetKeys #
Get a collection of keys
method : public : GetKeys() ~ Collection.Vector<K>Return
| Type | Description |
|---|---|
| CollectionVector<K> | vector of keys |
Example
map := Collection.Map->New()<String, IntRef>;
map->Insert("x", IntRef->New(10)); map->Insert("y", IntRef->New(20));
keys := map->GetKeys()<String>;
each(k := keys) { k->PrintLine(); }GetValues #
Gets a collection of values
method : public : GetValues() ~ Collection.Vector<V>Return
| Type | Description |
|---|---|
| CollectionVector<V> | vector of values |
Example
map := Collection.Map->New()<String, IntRef>;
map->Insert("p", IntRef->New(3)); map->Insert("q", IntRef->New(4));
values := map->GetValues()<IntRef>;
each(v := values) { v->PrintLine(); }Has #
Checks for a value in a map
method : public : Has(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | search key |
Return
| Type | Description |
|---|---|
| Bool | true if found, false otherwise |
Example
map := Collection.Map->New()<String, IntRef>;
map->Insert("cat", IntRef->New(1));
map->Has("cat")->PrintLine(); # true
map->Has("dog")->PrintLine(); # falseInsert #
Inserts a value into the map
method : public : Insert(key:K, value:V) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| key | K | key |
| value | V | value |
Example
map := Collection.Map->New()<String, IntRef>;
map->Insert("city", IntRef->New(42));
map->Find("city")->PrintLine(); # 42IsEmpty #
Checks to see if the map is empty
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if empty, false otherwise |
Example
map := Collection.Map->New()<String, IntRef>;
map->IsEmpty()->PrintLine(); # true
map->Insert("x", IntRef->New(9));
map->IsEmpty()->PrintLine(); # falseNew # constructor
Default constructor
New()Example
map := Collection.Map->New()<String, IntRef>;
map->Insert("alpha", IntRef->New(1));
map->Size()->PrintLine(); # 1Reduce #
Uses the given function to reduce the values
method : public : Reduce(a:K, f:(K,K)~K) ~ KParameters
| Name | Type | Description |
|---|---|---|
| a | K | initial value (i.e. accumulator) |
| f | (K,K)~K | function to use a reduce |
Return
| Type | Description |
|---|---|
| K | reduced value |
Example
map := Collection.Map->New()<String, IntRef>;
map->Insert("a", IntRef->New(0)); map->Insert("b", IntRef->New(0));
concat := map->Reduce("", \(String, String) ~ String : (acc, k) => "{$acc}{$k}");
concat->PrintLine(); # abRemove #
Removes a value from the map
method : public : Remove(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | key for value to remove |
Example
map := Collection.Map->New()<String, IntRef>;
map->Insert("tmp", IntRef->New(0));
map->Remove("tmp")->PrintLine(); # true
map->Has("tmp")->PrintLine(); # falseSize #
Size of queue
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | size of queue |
Example
map := Collection.Map->New()<String, IntRef>;
map->Insert("a", IntRef->New(1)); map->Insert("b", IntRef->New(2));
map->Size()->PrintLine(); # 2ToString #
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
map := Collection.Map->New()<String, IntRef>;
map->Insert("a", IntRef->New(1)); map->Insert("b", IntRef->New(2));
map->ToString()->PrintLine(); # [(a:1),(b:2)]