Class Map<TK, TV>
- Namespace
- Map
- Assembly
- Map.dll
public sealed class Map<TK, TV> : IReadOnlyDictionary<TK, TV>, IReadOnlyCollection<KeyValuePair<TK, TV>>, IEnumerable<KeyValuePair<TK, TV>>, IEnumerable where TK : notnull
Type Parameters
TKTV
- Inheritance
-
Map<TK, TV>
- Implements
-
IReadOnlyDictionary<TK, TV>IReadOnlyCollection<KeyValuePair<TK, TV>>IEnumerable<KeyValuePair<TK, TV>>
- Inherited Members
Constructors
Map()
public Map()
Map(IEqualityComparer<TK>)
public Map(IEqualityComparer<TK> comparer)
Parameters
comparerIEqualityComparer<TK>
Fields
Empty
public static readonly Map<TK, TV> Empty
Field Value
- Map<TK, TV>
Properties
Count
Gets the number of elements in the collection.
public int Count { get; }
Property Value
- int
The number of elements in the collection.
IsEmpty
public bool IsEmpty { get; }
Property Value
this[TK]
Gets the element that has the specified key in the read-only dictionary.
public TV this[TK key] { get; }
Parameters
keyTKThe key to locate.
Property Value
- TV
The element that has the specified key in the read-only dictionary.
Exceptions
- ArgumentNullException
keyis null.- KeyNotFoundException
The property is retrieved and
keyis not found.
Keys
public MapKeyCollection<TK, TV> Keys { get; }
Property Value
- MapKeyCollection<TK, TV>
Values
public MapValueCollection<TK, TV> Values { get; }
Property Value
- MapValueCollection<TK, TV>
Methods
Add(TK, TV)
public Map<TK, TV> Add(TK key, TV value)
Parameters
keyTKvalueTV
Returns
- Map<TK, TV>
ContainsKey(TK)
Determines whether the read-only dictionary contains an element that has the specified key.
public bool ContainsKey(TK key)
Parameters
keyTKThe key to locate.
Returns
- bool
true if the read-only dictionary contains an element that has the specified key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.
Equals(Map<TK, TV>?)
public bool Equals(Map<TK, TV>? other)
Parameters
otherMap<TK, TV>
Returns
Equals(object?)
Determines whether the specified object is equal to the current object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current object.
Returns
Exists(Func<TK, TV, bool>)
Determines whether the map contains elements that satisfy the specified predicate.
public bool Exists(Func<TK, TV, bool> pred)
Parameters
Returns
- bool
trueif the map contains an element that satisfies the condition; otherwise,false.
Filter(Func<TV, TV, bool>)
Filters the map, retaining only elements that satisfy the specified predicate.
public Map<TK, TV> Filter(Func<TV, TV, bool> action)
Parameters
Returns
- Map<TK, TV>
A new Map<TK, TV> containing the elements that satisfy the condition.
FindKey(Func<TK, bool>)
Finds the first key in the map that satisfies the specified predicate.
public TK FindKey(Func<TK, bool> pred)
Parameters
Returns
- TK
The key that satisfies the condition.
Exceptions
- KeyNotFoundException
Thrown if no key satisfies the condition.
FoldKeys<TState>(TState, Func<TState, TK, TState>)
Aggregates the keys of the map using the specified function.
public TState FoldKeys<TState>(TState seed, Func<TState, TK, TState> action)
Parameters
seedTStateThe initial accumulator value.
actionFunc<TState, TK, TState>A function to aggregate the state and each key.
Returns
- TState
The final accumulated state.
Type Parameters
TStateThe type of the accumulator state.
FoldValues<TState>(TState, Func<TState, TV, TState>)
Aggregates the values of the map using the specified function.
public TState FoldValues<TState>(TState seed, Func<TState, TV, TState> action)
Parameters
seedTStateThe initial accumulator value.
actionFunc<TState, TV, TState>A function to aggregate the state and each value.
Returns
- TState
The final accumulated state.
Type Parameters
TStateThe type of the accumulator state.
ForEach(Action<TK, TV>)
Executes the specified action on each element of the map.
public void ForEach(Action<TK, TV> action)
Parameters
actionAction<TK, TV>The action to execute on each key-value pair.
Get(TK)
public TV Get(TK key)
Parameters
keyTK
Returns
- TV
GetEnumerator()
public MapEnumerator<TK, TV> GetEnumerator()
Returns
- MapEnumerator<TK, TV>
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
Iter(Func<TK, TV, bool>)
Executes a delegate on the elements of the map
public bool Iter(Func<TK, TV, bool> action)
Parameters
Returns
- bool
True if the iteration completed all elements, or False if aborted early.
MapCar<NK, NV>(Func<TK, TV, (NK, NV)>, IEqualityComparer<NK>?)
Transforms the elements of the map using the specified function.
public Map<NK, NV> MapCar<NK, NV>(Func<TK, TV, (NK, NV)> action, IEqualityComparer<NK>? comparer = null)
Parameters
actionFunc<TK, TV, (NK, NV)>A function to transform each key-value pair.
comparerIEqualityComparer<NK>An optional equality comparer for the new keys.
Returns
- Map<NK, NV>
A new Map<TK, TV> containing the transformed elements.
Type Parameters
NKThe type of the new keys.
NVThe type of the new values.
Merge(Map<TK, TV>)
Merges another map into this one. Defaults to 'Prefer Right' where values in 'other' overwrite this map.
public Map<TK, TV> Merge(Map<TK, TV> other)
Parameters
otherMap<TK, TV>
Returns
- Map<TK, TV>
Merge(Map<TK, TV>, Func<TK, TV, TV, TV>?)
Merges another map into this one using a conflict resolution strategy.
public Map<TK, TV> Merge(Map<TK, TV> other, Func<TK, TV, TV, TV>? conflictResolver)
Parameters
otherMap<TK, TV>The other map to merge.
conflictResolverFunc<TK, TV, TV, TV>An optional thunk called when keys conflict: (key, leftValue, rightValue) => resolvedValue. Pass null to default to picking the right value (other overwrites this).
Returns
- Map<TK, TV>
Mutate(Action<TransientMap<TK, TV>>)
Creates a transient version of the map, applies the specified mutation action, and returns an immutable map.
public Map<TK, TV> Mutate(Action<TransientMap<TK, TV>> action)
Parameters
actionAction<TransientMap<TK, TV>>The action to apply to the transient map.
Returns
- Map<TK, TV>
A new Map<TK, TV> with the mutations applied.
Remove(TK)
public Map<TK, TV> Remove(TK key)
Parameters
keyTK
Returns
- Map<TK, TV>
ToTransient()
public TransientMap<TK, TV> ToTransient()
Returns
- TransientMap<TK, TV>
TryGetValue(TK, out TV)
Gets the value that is associated with the specified key.
public bool TryGetValue(TK key, out TV value)
Parameters
keyTKThe key to locate.
valueTVWhen this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the
valueparameter. This parameter is passed uninitialized.
Returns
- bool
true if the object that implements the IReadOnlyDictionary<TKey, TValue> interface contains an element that has the specified key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.