TextMap

A HashMap with Text keys

TODO: Write an example

public func new<A>() : TextMap<A>

Constructs an empty TextMap.

If you'd like to specify the initial capacity yourself you can call the TextMap constructor instead.

public func fromIter<A>(iter : Iter.Iter<(Text, A)>) : TextMap<A>

Constructs a TextMap from an iterator.

import TextMap "mo:text-map/TextMap";
let map = TextMap.fromIter<Nat>(
    [("A", 1), ("B", 2), ("C", 3)].vals()
);
assert(map.toText(Nat.toText) == "{ A => 1; B => 2; C => 3; }")

class TextMap<A>(initialCapacity : Nat)

public func size() : Nat

public func put(key : Text, value : A)

Inserts a value into the TextMap. Existing entries with the same key are overriden.

import TextMap "mo:text-map/TextMap";
let map = TextMap.new<Nat>();

map.put("A", 10);
assert(map.toText(Nat.toText) == "{ A => 10; }");

map.put("A", 20);
assert(map.toText(Nat.toText) == "{ A => 20; }");

public func replace(key : Text, value : A) : ?A

Inserts a value into the TextMap. If there's an existing value with the same key, returns that value otherwise null.

import TextMap "mo:text-map/TextMap";
let map = TextMap.new<Nat>();

let old1 = map.replace("A", 10);
assert(old1 == null);
assert(map.toText(Nat.toText) == "{ A => 10; }");

let old2 = map.replace("A", 20);
assert(old2 == ?10);
assert(map.toText(Nat.toText) == "{ A => 20; }");

public func get(key : Text) : ?A

Gets the value at the given key. Returns null if the value can't be found.

let map = TextMap.new<Nat>();

let notFound = map.get("A");
assert(notFound == null);

map.put("A", 10);
let found = map.get("A",);
assert(found == ?10);

public func delete(key : Text)

Removes the given key from the map. Does nothing if the key didn't exist.

public func contains(key : Text) : Bool

Checks whether the given key is present in the map.

public func entries() : Iter.Iter<(Text, A)>

Iterates over all key value pairs in this map.

Careful! This Iterator is invalidated when its underlying TextMap is modified.

public func toArray() : [(Text, A)]

Returns an Array of all key value pairs contained in this map.

public func clone() : TextMap<A>

Creates a copy of this map. Modifying the copy does not modify this instance.

public func toText(toTextA : A -> Text) : Text

Primarily useful for debugging

A hashmap with Text keys.

public func djb2(t : Text) : Nat32