Skip to content

Collections

Kofifus edited this page Dec 17, 2022 · 28 revisions

Data versions of common collections with added features.

The collections are similar to the .NET immutable containers however they have value semantics and therefore qualify as Data.

In addition these collections have extra features that makes them more convenient to use. In particular they use operator overloading for basic operations (add/remove etc) and additional constructors, and have easier indexing operators.

Each container is available in two forms:

  • A plain form Lst<T>, Set<T>, OrderedSet<T>, Map<TKey, TValue>, OrderedMap<TKey, TValue>, Que<T>, Arr<T>
  • A base form LstBase<TDerived, T>, SetBase<TDerived, T>, OrderedSetBase<TDerived, T>, MapBase<TDerived, TKey, TValue>, OrderedMapBase<TDerived, TKey, TValue>, QueBase<TDerived, T>, ArrBase<TDerived, T>

Deriving from a base form of the collection allows the creation of a new unique type, that is if MyLst<T> derives from LstBase<MyLst, T> then MyLst<T> and Lst<T> are two different types but otherwise have the same interface. This can greatly enhance type safety.

For methods that return an element in the collection or a mutation of the collection, F prefers returning null to throwing exceptions where possible.

Note that value semantics mean that equality checks (ie using ==) can be expensive.

Lst<T>

Wrapper around ImmutableList

Lst() -> creates and empty Lst
Lst(params T?[] items) -> creates a Lst from a list of values (ie new Lst<int>(1, 2, 3))

bool Equals(Lst<T>? obj) -> value equality (compare all values)
int GetHashCode() -> value hash code (uses all values)

string ToString() -> comma separated dump of values

static Lst<T> operator +(Lst<T> o, T v) -> add item (ie lst+v or lst+=v)
static Lst<T> operator +(Lst<T> o, Lst<T> v items) -> add items (ie, lst += lst2)
static Lst<T> operator -(Lst<T> o, T v) -> remove item (ie lst-v or lst-=v)
static Lst<T> operator -(Lst<T> o, Lst<T> v items) -> remove items (ie, lst -= lst2)
static Lst<T> operator -(Lst<T> o, Predicate<T> match) -> remove matching (ie, lst -= (v => v.Name="John"))

T? this[int index] -> get value (ie, lst[5])
    returns null if not found (useful when T is a non-nullable reference type)

bool TryGetValue(int index, [MaybeNullWhen(false)] out T value) -> get value (ie, var ok=lst.Get(5, out var v)),
    use this if T is not a non-nullable reference type (ie long or Manager?)

T? Find(Predicate<T> match) -> find value     returns null if not found (useful when T is a non-nullable reference type)

bool Find(Predicate<T> match, [MaybeNullWhen(false)] out T value)) -> find value
    use this if T is not a non-nullable reference type (ie long or Manager?)

T? FindLast(Predicate<T> match) -> find value     returns null if not found (useful when T is a non-nullable reference type)

bool FindLast(Predicate<T> match, [MaybeNullWhen(false)] out T value)) -> find value
    use this if T is not a non-nullable reference type (ie long or Manager?)

The rest of the methods are identical to the ones in the underlying ImmutableList.

Set<T>

Wrapper around ImmutableHashSet

Set() -> creates and empty Set
Set(params T?[] items) -> creates a Set from a list of values (ie new Set<int>(1, 2, 3))

bool Equals(Set<T>? obj) -> value equality (compare all values)
int GetHashCode() -> value hash code (uses all values)

string ToString() -> comma separated dump of values

static Set<T> operator +(Set<T> o, T v) -> add item (ie set+v or set+=v)
static Set<T> operator +(Set<T> o, Set<T> items) -> add items (ie, set += set2)
static Set<T> operator -(Set<T> o, T v) -> remove item (ie set-v or set-=v)
static Set<T> operator -(Set<T> o, Set<T> items) -> remove items (ie, set -= set2)
static Set<T> operator -(Set<T> o, Predicate<T> match) -> remove matching (ie, set -= (v => v.Name="John"))

The rest of the methods are identical to the ones in the underlying ImmutableHashSet.

OrderedSet<T>

Wrapper around SortedImmutableHashSet

Similar to Set except that it is sorted and have the WithComparer method of SortedImmutableHashSet

Map<TKey,TValue>

Wrapper around ImmutableDictionary

Map() -> creates an empty Map Map(TKey key, TValue val) -> create a map with a single key/value Map(params (TKey k, TValue v)?[] items) -> creates a Map from a list of values (ie new Map<int, DateTime>((5, DateTime.Now), (8, DateTime.UtcNow)))

bool Equals(Lst<T>? obj) -> value equality (compare all values)
int GetHashCode() -> value hash code (uses all values)

string ToString() -> comma separated dump of values

static Map<T> operator +(Map<T> o, (TKey key, TValue val) vt) -> add item (ie Map+(k,v) or Map+=(k,v))
static Map<T> operator +(Map<T> o, IEnumerable<ValueTuple<TKey, TValue>> pairs) -> add items (ie, Map+= map2)
static Map<T> operator -(Map<T> o, TKey key) -> remove item by key (ie Map-key or Map-=key)
static Map<T> operator -(Map<T> o, IEnumerable<TKey> keys) -> remove keys(ie, Map-= map2.Keys)
static Map<T> operator -(Map<T> o, Func<TKey, TValue, bool> match) -> remove matching (ie, Map-= ((k, v) => v.Name="John"))

TValue? this[TKey key] -> get value at key (ie, map[ket])
    returns null if not found (useful when TValue is a non-nullable reference type)

The rest of the methods are identical to the ones in the underlying ImmutableDictionary.

OrderedMap<TKey,TValue>

Wrapper around SortedImmutableDictionary

Similar to Map except that it is sorted and have the WithComparers method of SortedImmutableMap

Que<T>

Wrapper around ImmutableQueue

Que() -> creates and empty Que
Que(params T?[] items) -> creates a Que from a list of values (ie new Que<int>(1, 2, 3))

bool Equals(Que<T>? obj) -> value equality (compare all values)
int GetHashCode() -> value hash code (uses all values)

string ToString() -> comma separated dump of values

static Que<T> operator +(Que<T> o, T v) -> enque an item (ie que+v or que+=v)

T? Peek() -> peek
    returns null if queue is empty (useful when T is a non-nullable reference type)

bool TryPeek([MaybeNullWhen(false)] out T value) -> peek
    use this if T is not a non-nullable reference type (ie long or Manager?)

The following methods are identical to the ones in the underlying ImmutableQueue.

Arr<T>

Wrapper around ImmutableArray

Arr() -> creates and empty Arr Arr(params T?[] v) -> creates a Arr from another collection

bool Equals(Lst<T>? obj) -> value equality (compare all values)
int GetHashCode() -> value hash code (uses all values)

string ToString() -> comma separated dump of values

static Arr<T> operator +(Arr<T> o, T v) -> add item (ie arr+v or arr+=v)
static Arr<T> operator +(Arr<T> o, IEnumerable<T> items) -> add items (ie, arr+= arr)
static Arr<T> operator -(Arr<T> o, T v) -> remove item (ie arr-v or arr-=v)
static Arr<T> operator -(Arr<T> o, IEnumerable<T> items) -> remove items (ie, arr -= arr2)
static Arr<T> operator -(Arr<T> o, Predicate<T> match) -> remove matching (ie, arr -= (v => v.Name="John"))

T? this[int index] -> get value (ie, lst[5])
    returns null if not found (useful when T is a non-nullable reference type)

bool TryGetValue(int index, [MaybeNullWhen(false)] out T value) -> get value (ie, var ok=lst.Get(5, out var v)),
    use this if T is not a non-nullable reference type (ie long or Manager?)

The rest of the methods are identical to the ones in the underlying ImmutableArray.

Clone this wiki locally