glib/map

Maps are a structure similar to dict in that they map keys to values. Duplicate keys cannot exist and a key can map to at most one value

The Keys are strings, values can be any type but values must be of the same type

Maps are unordered

Types

pub opaque type Map(value)

Functions

pub fn clear(previous_map: Map(a)) -> Map(a)

Creates a new empty map with the same sizing/loading properties as the passed map

pub fn contains_key(map: Map(a), key: String) -> Bool

Returns the existence in the map for the stored key

Examples

new() |> put("key", "value") |> contains_key("key")
// -> True
new() |> put("key", "value") |> contains_key("non-existent")
// -> False
pub fn entries(map: Map(a)) -> List(#(String, a))

Returns a list of the tuples #(key, value) contained in the map

Examples

new() |> entries
// -> []
new() |> put("key", "value") |> entries
// -> [#("key", "value")]
pub fn full_count(map: Map(a)) -> Int

Returns a count of the number of entries in the map This is used for testing to compare against the cached size Performs a full iteration of the list incrementing for all Some(_) entries

pub fn get(map: Map(a), key: String) -> Option(a)

Retrieves the option wrapped value from the map for the stored key

The key may not exist so then None is returned

Examples

new() |> put("key", "value") |> get("key")
// -> Ok("value")
new() |> put("key", "value") |> get("non-existent")
// -> None
pub fn is_empty(map: Map(a)) -> Bool

Determines whether the map is empty, i.e. contains no key/values

Examples

new() |> is_empty
// -> True
new() |> put("key", "value") |> is_empty
// -> False
pub fn keys(map: Map(a)) -> List(String)

Returns a list of the keys contained in the map

Examples

new() |> keys
// -> []
new() |> put("key", "value") |> keys
// -> ["key"]
pub fn list_size(map: Map(a)) -> Int

Returns the internal storage size of the map This is mainly for testing use

pub fn new() -> Map(a)

Creates an empty map The size and loading factor are set to the default The size is the starting size for the list that contains the values The loading factor is the value 0 -> 1 that determines when the list is resized. This is the percentage of the backing list that is filled. For example, if the loading factor was 0.5 then when one half of the backing list is populated, the next addition to the Map will trigger a resize to ensure the map has usable space

pub fn new_with_size(size: Int) -> Map(a)

Creates an empty map with specified size The loading factor is set to default

pub fn new_with_size_and_load(size: Int, load: Float) -> Map(a)

Creates an empty map with specified size and loading factor load is a value 0->1 (non-inclusive) which specifies a percentage (e.g. 0.5 is 50%) at which point the backing list is resized This should be kept around 0.6-0.8 to avoid either excessive resizing or excessive key hash collisions

pub fn put(map: Map(a), key: String, value: a) -> Map(a)

Inserts a value into the map with the given key

Will replace value if key already exists

Examples

new() |> put("key", 999) |> to_string(fn(i) { int.to_string(i) })
// -> {"key":999}
new() |> put("key", 999) |> put("key2", 111) |> to_string(fn(i) { int.to_string(i) })
// -> {"key":999, "key2":111}
new() |> put("key", 999) |> put("key", 123) |> to_string(fn(i) { int.to_string(i) })
// -> {"key":123}
pub fn remove(map: Map(a), key: String) -> #(Option(a), Map(a))

Removes the specified key from the map and returns a tuple containing the removed option wrapped value or None and the altered map without the specified key

Examples

new() |> put("key", "value") |> remove("key")
// -> #(Some("value"), {})
new() |> put("key", "value") |> remove("non-existent")
// -> #(None, {"key": "value"})
pub fn size(map: Map(a)) -> Int

Determines the size of the map, i.e. the number of key/values

Examples

new() |> size
// -> 0
new() |> put("key", "value") |> size
// -> 1
pub fn to_string(
  map: Map(a),
  value_to_string: fn(a) -> String,
) -> String

Returns a string representation of the passed map Requires a value_to_string fn to generate the output

Examples

new() |> to_string(fn(v) { v })
// -> {}
new() |> put("key", "value") |> to_string(fn(v) { v })
// -> {"key": "value"}
pub fn values(map: Map(a)) -> List(a)

Returns a list of the values contained in the map

Examples

new() |> values
// -> []
new() |> put("key", "value") |> values
// -> ["value"]
Search Document