Map

Standard library for map operations.

Functions in this module operate on type Map<K, V>, which represents a hash map (using a hash array mapped trie) with O(log n) put(), lookup()/get(), and remove(). Literal maps can be created with the syntax { k1 => v1, k2 => v2, ... }, where k1, k2, etc. are keys and v1, v2, etc. are the associated values, respectively.

To import all names from this module, use:

import Map (*)

With no import, you can still access anything with the prefix Map., like Map.get_or.

Index

NameTypeDescription

get(m, k)

(Map<K, V>, K) -> V

Returns the value associated with key k in the map m.

lookup(m, k)

(Map<K, V>, K) -> Option<V>

If key k is in the map m, returns Some(v), where v is the value associated with k.

key?(m, k)

(Map<K, V>, K) -> Bool

Returns true if the key k is in map m.

remove(m, k)

(Map<K, V>, K) -> Map<K, V>

Removes the key k and its associated value from m, returning a new map.

length(sized)

Map<K, V> -> Int

Returns the length of sized.

empty?(sized)

Map<K, V> -> Bool

Returns whether sized is empty.

concat(a, b)

(Map<K, V>, Map<K, V>) -> Map<K, V>

Concatenates a and b together, equivalent to a ++ b.

concat_all(l)

[Map<K, V>] -> Map<K, V>

Concatenates all elements in the list l together.

map(container, f)

(Map<K, V>, (K, V) -> (L, M)) -> Map<L, M>

Calls f for each element in container, collecting the results into a new container.

get_or(m, k, default)

(Map<K, V>, K, V) -> V

Returns the value associated with key k in map m, or default if k isn't in m.

keys(m)

Map<K, V> -> [K]

Returns a list of keys in m.

values(m)

Map<K, V> -> [V]

Returns a list of values in m

update(m, k, f)

(Map<K, V>, K, V -> V) -> Map<K, V>

Updates the value associated with the existing key k in map m using the function f, returning a new map.

upsert(m, k, default, f)

(Map<K, V>, K, V, V -> V) -> Map<K, V>

Updates or inserts a value associated with key k in map m using the function f, returning a new map.

merge(a, b)

(Map<K, V>, Map<K, V>) -> Map<K, V>

Merges maps a and b together, returning a new map.

with(m, keys)

(Map<K, V>, [K]) -> Map<K, V>

Filters m to only have keys in keys, returning a new map.

without(m, keys)

(Map<K, V>, [K]) -> Map<K, V>

Filters m to not have any keys in keys, returning a new map.

fold(collection, init, f)

(Map<K, V>, F, (F, (K, V)) -> F) -> F

Computes a single value by applying the combining function f to each element of the given collection, starting with the initial value init.

filter(collection, f)

(Map<K, V>, (K, V) -> Bool) -> Map<K, V>

Calls f for each element in collection, returning a new collection that only contains the elements for which f returns true.

find(collection, f)

(Map<K, V>, (K, V) -> Bool) -> Option<(K, V)>

Returns the first element in collection for which f returns true.

filter_map(collection, f)

(Map<K, V>, (K, V) -> Option<(L, M)>) -> Map<L, M>

Calls f for each element in collection, returning a new collection that contains every e such that f returned Some(e).

fold_map(collection, init, f)

(Map<K, V>, F, (F, (K, V)) -> (F, (L, M))) -> (F, Map<L, M>)

Computes a new collection and a single value by applying the combining function f to each element of the given collection, starting with the initial value init.

put(collection, elem)

(Map<K, V>, (K, V)) -> Map<K, V>

Puts elem into collection, returning a new collection.

delete(collection, elem)

(Map<K, V>, (K, V)) -> Map<K, V>

Removes elem from collection, returning a new collection.

contains?(collection, elem)

(Map<K, V>, (K, V)) -> Bool

Returns true if collection contains elem.

all?(collection, f)

(Map<K, V>, (K, V) -> Bool) -> Bool

Calls f for each element in collection, and returns true if f returns true for every element.

any?(collection, f)

(Map<K, V>, (K, V) -> Bool) -> Bool

Calls f for each element in collection, and returns true if f returns true for at least one element.

to_list(a)

Map<K, V> -> [(K, V)]

Converts a to a list.

to_set(a)

Map<K, V> -> Set<(K, V)>

Converts a to a set.

Functions

get : (Map<K, V>, K) -> V
get(m, k)

Returns the value associated with key k in the map m. If k isn't in m, raises BadKey.

This function is part of the Base module, meaning it's a builtin that's in the global namespace, so it doesn't need to be imported.

assert get({ "bar" => 3.7, "baz" => 1 }, "baz") == 1
assert get({ 'h' => false }, 'h') == false
lookup : (Map<K, V>, K) -> Option<V>
lookup(m, k)

If key k is in the map m, returns Some(v), where v is the value associated with k. Otherwise, returns None.

This function is part of the Base module, meaning it's a builtin that's in the global namespace, so it doesn't need to be imported.

assert lookup({ "bar" => 3.7, "baz" => 1 }, "foo") == None
assert lookup({ 'h' => false }, 'h') == Some(false)
assert lookup({}, true) == None
key? : (Map<K, V>, K) -> Bool
key?(m, k)

Returns true if the key k is in map m.

This function is part of the Base module, meaning it's a builtin that's in the global namespace, so it doesn't need to be imported.

assert key?({ "bar" => 3.7, "baz" => 1 }, "baz")
assert !key?({ 'h' => false }, 'g')
assert !key?({}, true)
remove : (Map<K, V>, K) -> Map<K, V>
remove(m, k)

Removes the key k and its associated value from m, returning a new map. m is not modified.

This function is part of the Base module, meaning it's a builtin that's in the global namespace, so it doesn't need to be imported.

assert remove({ "bar" => 3.7, "baz" => 1 }, "baz") == { "bar" => 3.7 }
assert remove({ 'h' => false }, 'h') == {}
assert remove({ 'h' => false }, 'g') == { 'h' => false }
assert remove({}, true) == {}
get_or : (Map<K, V>, K, V) -> V
get_or(m, k, default)

Returns the value associated with key k in map m, or default if k isn't in m.

assert get_or({ "bar" => 3.7, "baz" => 1 }, "foo", 5.02) == 5.02
assert get_or({ true => 'h' }, true, 'i') == 'h'
assert get_or({}, 1, [false]) == [false]
keys : Map<K, V> -> [K]
keys(m)

Returns a list of keys in m.

let ks = keys({ "bar" => 3.7, "baz" => 1 })
// order is undefined
assert ks == ["bar", "baz"] || ks == ["baz", "bar"]

assert keys({ true => 'h' }) == [true]
assert keys({}) == []
values : Map<K, V> -> [V]
values(m)

Returns a list of values in m

let vs = values({ "bar" => 3.7, "baz" => 1 })
// order is undefined
assert vs == [3.7, 1] || vs == [1, 3.7]

assert values({ true => 'h' }) == ['h']
assert values({}) == []
update : (Map<K, V>, K, V -> V) -> Map<K, V>
update(m, k, f)

Updates the value associated with the existing key k in map m using the function f, returning a new map. f accepts the current value associated with k and returns the new value. If k isn't in m, raises BadKey. Use upsert() if you want to handle the case where k isn't in m. Note that m isn't modified.

assert update({ "bar" => 3.7, "baz" => 1 }, "bar", |a| a * 2) ==
  { "bar" => 7.4, "baz" => 1 }
assert update({ true => 'h' }, true, |ch| to_int(ch) + 1 |> to_char) ==
  { true => 'i' }
upsert : (Map<K, V>, K, V, V -> V) -> Map<K, V>
upsert(m, k, default, f)

Updates or inserts a value associated with key k in map m using the function f, returning a new map. f accepts the current value associated with k and returns the new value. If key k isn't in m, sets the associated value to default. If you know key k is in m, use update(). Note that m isn't modified.

assert upsert({ "bar" => 3.7, "baz" => 1 }, "foo", 0, |a| a * 2) ==
  { "foo" => 0, "bar" => 3.7, "baz" => 1 }
assert upsert({ "bar" => 3.7, "baz" => 1 }, "bar", 0, |a| a * 2) ==
  { "bar" => 7.4, "baz" => 1 }

let f = |ch| to_int(ch) + 1 |> to_char
assert upsert({ true => 'h' }, true, 'a', f) == { true => 'i' }
merge : (Map<K, V>, Map<K, V>) -> Map<K, V>
merge(a, b)

Merges maps a and b together, returning a new map. If a key exists in both a and b, the value in b takes precedence. This is an alias for concat() and is equivalent to a ++ b.

assert merge({ "foo" => 7.5 }, { "bar" => 3.7, "baz" => 1 }) ==
  { "foo" => 7.5, "bar" => 3.7, "baz" => 1 }
assert merge({ true => 'i', false => 'y' }, { true => 'h' }) ==
  { true => 'h', false => 'y' }
assert merge({ @hi => 'h' }, { @hi => 'i', @hey => 'y' }) ==
  { @hi => 'i', @hey => 'y' }
with : (Map<K, V>, [K]) -> Map<K, V>
with(m, keys)

Filters m to only have keys in keys, returning a new map. m is not modified.

assert with({ "bar" => 3.7, "baz" => 1 }, ["foo"]) == {}
assert with({ "bar" => 3.7, "baz" => 1 }, ["foo", "bar"]) ==
  { "bar" => 3.7 }
assert with({ true => 'h' }, [true]) == { true => 'h' }
assert with({ true => 'h' }, []) == {}
without : (Map<K, V>, [K]) -> Map<K, V>
without(m, keys)

Filters m to not have any keys in keys, returning a new map. m is not modified.

assert without({ "bar" => 3.7, "baz" => 1 }, ["foo"]) ==
  { "bar" => 3.7, "baz" => 1 }
assert without({ "bar" => 3.7, "baz" => 1 }, ["foo", "bar"]) ==
  { "baz" => 1 }
assert without({ true => 'h' }, [true]) == {}
assert without({ true => 'h' }, []) == { true => 'h' }

Implementations

impl Sized for Map<K, V>

The following functions are from the Sized interface.

length : Map<K, V> -> Int
length(sized)

Returns the length of sized. See the full description in the Sized interface.

empty? : Map<K, V> -> Bool
empty?(sized)

Returns whether sized is empty. See the full description in the Sized interface.

impl Concat for Map<K, V>

The following functions are from the Concat interface.

concat : (Map<K, V>, Map<K, V>) -> Map<K, V>
concat(a, b)

Concatenates a and b together, equivalent to a ++ b. See the full description in the Concat interface.

concat_all : [Map<K, V>] -> Map<K, V>
concat_all(l)

Concatenates all elements in the list l together. See the full description in the Concat interface.

impl Mappable for Map

The following functions are from the Mappable interface.

map : (Map<K, V>, (K, V) -> (L, M)) -> Map<L, M>
map(container, f)

Calls f for each element in container, collecting the results into a new container. See the full description in the Mappable interface.

impl Collection for Map

The following functions are from the Collection interface.

fold : (Map<K, V>, F, (F, (K, V)) -> F) -> F
fold(collection, init, f)

Computes a single value by applying the combining function f to each element of the given collection, starting with the initial value init. See the full description in the Collection interface.

filter : (Map<K, V>, (K, V) -> Bool) -> Map<K, V>
filter(collection, f)

Calls f for each element in collection, returning a new collection that only contains the elements for which f returns true. See the full description in the Collection interface.

find : (Map<K, V>, (K, V) -> Bool) -> Option<(K, V)>
find(collection, f)

Returns the first element in collection for which f returns true. See the full description in the Collection interface.

filter_map : (Map<K, V>, (K, V) -> Option<(L, M)>) -> Map<L, M>
filter_map(collection, f)

Calls f for each element in collection, returning a new collection that contains every e such that f returned Some(e). See the full description in the Collection interface.

fold_map : (Map<K, V>, F, (F, (K, V)) -> (F, (L, M))) -> (F, Map<L, M>)
fold_map(collection, init, f)

Computes a new collection and a single value by applying the combining function f to each element of the given collection, starting with the initial value init. See the full description in the Collection interface.

put : (Map<K, V>, (K, V)) -> Map<K, V>
put(collection, elem)

Puts elem into collection, returning a new collection. See the full description in the Collection interface.

delete : (Map<K, V>, (K, V)) -> Map<K, V>
delete(collection, elem)

Removes elem from collection, returning a new collection. See the full description in the Collection interface.

contains? : (Map<K, V>, (K, V)) -> Bool
contains?(collection, elem)

Returns true if collection contains elem. See the full description in the Collection interface.

all? : (Map<K, V>, (K, V) -> Bool) -> Bool
all?(collection, f)

Calls f for each element in collection, and returns true if f returns true for every element. See the full description in the Collection interface.

any? : (Map<K, V>, (K, V) -> Bool) -> Bool
any?(collection, f)

Calls f for each element in collection, and returns true if f returns true for at least one element. See the full description in the Collection interface.

impl ToList for Map

The following functions are from the ToList interface.

to_list : Map<K, V> -> [(K, V)]
to_list(a)

Converts a to a list. See the full description in the ToList interface.

impl ToSet for Map

The following functions are from the ToSet interface.

to_set : Map<K, V> -> Set<(K, V)>
to_set(a)

Converts a to a set. See the full description in the ToSet interface.