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
Name | Type | Description |
---|---|---|
(Map<K, V>, K) -> V | Returns the value associated with key | |
(Map<K, V>, K) -> Option<V> | If key | |
(Map<K, V>, K) -> Bool | Returns true if the key | |
(Map<K, V>, K) -> Map<K, V> | Removes the key | |
Map<K, V> -> Int | Returns the length of | |
Map<K, V> -> Bool | Returns whether | |
(Map<K, V>, Map<K, V>) -> Map<K, V> | Concatenates | |
[Map<K, V>] -> Map<K, V> | Concatenates all elements in the list | |
(Map<K, V>, (K, V) -> (L, M)) -> Map<L, M> | Calls | |
(Map<K, V>, K, V) -> V | Returns the value associated with key | |
Map<K, V> -> [K] | Returns a list of keys in | |
Map<K, V> -> [V] | Returns a list of values in | |
(Map<K, V>, K, V -> V) -> Map<K, V> | Updates the value associated with the existing key | |
(Map<K, V>, K, V, V -> V) -> Map<K, V> | Updates or inserts a value associated with key | |
(Map<K, V>, Map<K, V>) -> Map<K, V> | Merges maps | |
(Map<K, V>, [K]) -> Map<K, V> | Filters | |
(Map<K, V>, [K]) -> Map<K, V> | Filters | |
(Map<K, V>, F, (F, (K, V)) -> F) -> F | Computes a single value by applying the combining function | |
(Map<K, V>, (K, V) -> Bool) -> Map<K, V> | Calls | |
(Map<K, V>, (K, V) -> Bool) -> Option<(K, V)> | Returns the first element in | |
(Map<K, V>, (K, V) -> Option<(L, M)>) -> Map<L, M> | Calls | |
(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 | |
(Map<K, V>, (K, V)) -> Map<K, V> | Puts | |
(Map<K, V>, (K, V)) -> Map<K, V> | Removes | |
(Map<K, V>, (K, V)) -> Bool | Returns true if | |
(Map<K, V>, (K, V) -> Bool) -> Bool | Calls | |
(Map<K, V>, (K, V) -> Bool) -> Bool | Calls | |
Map<K, V> -> [(K, V)] | Converts | |
Map<K, V> -> Set<(K, V)> | Converts |
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