ordered_dict
Types
pub opaque type OrderedDict(k, v)
pub type Upsert(k, v) {
Update(value: v, update: fn(v) -> OrderedDict(k, v))
Insert(insert: fn(UpsertIndex, v) -> OrderedDict(k, v))
}
Constructors
-
Update(value: v, update: fn(v) -> OrderedDict(k, v))
-
Insert(insert: fn(UpsertIndex, v) -> OrderedDict(k, v))
pub type UpsertIndex {
Start
End
Index(Int)
}
Constructors
-
Start
-
End
-
Index(Int)
Functions
pub fn backing_dict(o_dict: OrderedDict(a, b)) -> Dict(a, b)
Get the internal unordered dict
pub fn delete(
from o_dict: OrderedDict(a, b),
delete key: a,
) -> OrderedDict(a, b)
Creates a new dict from a given dict with all the same entries except for the one with a given key, if it exists.
Examples
from_list([#("a", 0), #("b", 1)]) |> delete("a")
// -> from_list([#("b", 1)])
from_list([#("a", 0), #("b", 1)]) |> delete("c")
// -> from_list([#("a", 0), #("b", 1)])
pub fn delete_at(
from o_dict: OrderedDict(a, b),
at index: Int,
) -> OrderedDict(a, b)
Creates a new dict from a given dict with all the same entries except for the one at a given index, if it exists.
Examples
from_list([#("a", 0), #("b", 1)]) |> delete_at(0)
// -> from_list([#("b", 1)])
from_list([#("a", 0), #("b", 1)]) |> delete_at(2)
// -> from_list([#("a", 0), #("b", 1)])
pub fn delete_at_with_key(
from o_dict: OrderedDict(a, b),
at index: Int,
for key: a,
) -> OrderedDict(a, b)
Creates a new dict from a given dict with all the same entries except for the one at a given index, if it exists and it is the specified key.
Examples
from_list([#("a", 0), #("b", 1)]) |> delete_at_with_key(0, "a")
// -> from_list([#("b", 1)])
from_list([#("a", 0), #("b", 1)]) |> delete_at_with_key(0, "b")
// -> from_list([#("a", 0), #("b", 1)])
from_list([#("a", 0), #("b", 1)]) |> delete_at_with_key(1, "a")
// -> from_list([#("a", 0), #("b", 1)])
from_list([#("a", 0), #("b", 1)]) |> delete_at_with_key(2, "a")
// -> from_list([#("a", 0), #("b", 1)])
pub fn drop(
from o_dict: OrderedDict(a, b),
drop disallowed_keys: List(a),
) -> OrderedDict(a, b)
Creates a new dict from a given dict with all the same entries except any with keys found in a given list.
Examples
from_list([#("a", 0), #("b", 1)]) |> drop(["a"])
// -> from_list([#("b", 1)])
from_list([#("a", 0), #("b", 1)]) |> drop(["c"])
// -> from_list([#("a", 0), #("b", 1)])
from_list([#("a", 0), #("b", 1)]) |> drop(["a", "b", "c"])
// -> from_list([])
pub fn each(
o_dict: OrderedDict(a, b),
fun: fn(a, b, Int) -> c,
) -> Nil
Calls a function for each key and value in order, discarding the return value.
Useful for producing a side effect for every item of a dict.
import gleam/io
let dict = from_list([#("a", "apple"), #("b", "banana"), #("c", "cherry")])
each(dict, fn(k, v) {
io.println(key <> " => " <> value)
})
// -> Nil
// a => apple
// b => banana
// c => cherry
The order of elements in the iteration is an implementation detail that should not be relied upon.
pub fn filter(
in o_dict: OrderedDict(a, b),
keeping predicate: fn(a, b, Int) -> Bool,
) -> OrderedDict(a, b)
Creates a new ordered dict from a given ordered dict, minus any entries that a
given function returns False
for.
Examples
from_list([#("a", 0), #("b", 1)])
|> filter(fn(key, value, _index) { value != 0 })
// -> from_list([#("b", 1)])
from_list([#("a", 0), #("b", 1)])
|> filter(fn(key, value, _index) { True })
// -> from_list([#("a", 0), #("b", 1)])
pub fn fold(
over o_dict: OrderedDict(a, b),
from initial: c,
with fun: fn(c, a, b, Int) -> c,
) -> c
Combines all entries into a single value by calling a given function on each one in order.
Examples
let dict = from_list([#("a", 1), #("b", 3), #("c", 9)])
fold(dict, 0, fn(accumulator, key, value, _) { accumulator + value })
// -> 13
import gleam/string
let dict = from_list([#("a", 1), #("b", 3), #("c", 9)])
fold(dict, "", fn(accumulator, key, value, _) {
string.append(accumulator, key)
})
// -> "abc"
pub fn from_list(entries: List(#(a, b))) -> OrderedDict(a, b)
Converts a list of 2-element tuples #(key, value)
to an ordered dict.
If two tuples have the same key the last one in the list will be the one that is present in the dict, and .
pub fn get(
from o_dict: OrderedDict(a, b),
for key: a,
) -> Result(b, Nil)
Fetches a value from a dict for a given key.
The dict may not have a value for the key, so the value is wrapped in a
Result
.
Examples
new() |> insert("a", 0) |> get("a")
// -> Ok(0)
new() |> insert("a", 0) |> get("b")
// -> Error(Nil)
pub fn get_at(
from o_dict: OrderedDict(a, b),
at index: Int,
) -> Result(#(a, b), Nil)
Fetches a #(key, value)
entry from a dict at a given index.
The dict may not have an entry for the index, so the entry is wrapped in a
Result
.
Examples
new() |> insert("a", 0) |> get_at(0)
// -> Ok(#("a",0))
new() |> insert("a", 0) |> get_at(1)
// -> Error(Nil)
pub fn get_index(
from o_dict: OrderedDict(a, b),
for key: a,
) -> Result(Int, Nil)
Fetches an index from a dict for a given key.
The dict may not have an index for the key, so the index is wrapped in a
Result
.
Examples
new() |> insert("a", "foo") |> get("a")
// -> Ok(0)
new() |> insert("a", "foo") |> get("b")
// -> Error(Nil)
pub fn get_key_at(
from o_dict: OrderedDict(a, b),
at index: Int,
) -> Result(a, Nil)
Fetches a key from a dict at a given index.
The dict may not have a key for the index, so the key is wrapped in a
Result
.
Examples
new() |> insert("a", 0) |> get_key_at(0)
// -> Ok("a")
new() |> insert("a", 0) |> get_key_at(1)
// -> Error(Nil)
pub fn get_value_at(
from o_dict: OrderedDict(a, b),
at index: Int,
) -> Result(b, Nil)
Fetches a value from a dict at a given index.
The dict may not have a value for the index, so the value is wrapped in a
Result
.
Examples
new() |> insert("a", 0) |> get_value_at(0)
// -> Ok(0)
new() |> insert("a", 0) |> get_value_at(1)
// -> Error(Nil)
pub fn has_key(o_dict: OrderedDict(a, b), key: a) -> Bool
Determines whether or not a value present in the dict for a given key.
Examples
new() |> insert_end("a", 0) |> has_key("a")
// -> True
new() |> insert_end("a", 0) |> has_key("b")
// -> False
pub fn insert(
into o_dict: OrderedDict(a, b),
at index: Int,
for key: a,
value val: b,
) -> OrderedDict(a, b)
Inserts a value into the dict with the given key and position.
If the dict already has a value for the given key then the value is replaced with the new value.
Examples
from_list([#("a", 0), #("b", 1)]) |> insert(1, "c", 2)
// -> from_list([#("a", 0), #("c", 2), #("b", 1)])
pub fn insert_end(
into o_dict: OrderedDict(a, b),
for key: a,
value val: b,
) -> OrderedDict(a, b)
Inserts a value into the dict with the given key as the last item.
If the dict already has a value for the given key then the value is replaced with the new value.
Examples
from_list([#("a", 0), #("b", 1)]) |> insert_end("c", 2)
// -> from_list([#("a", 0), #("b", 1), #("c", 2)])
pub fn is_empty(o_dict: OrderedDict(a, b)) -> Bool
Determines whether or not the dict is empty.
Examples
new() |> is_empty
// -> True
new() |> insert_end("b", 1) |> is_empty
// -> False
pub fn keys(o_dict: OrderedDict(a, b)) -> List(a)
Gets a list of all keys in a given ordered dict in order.
Examples
from_list([#("a", 0), #("b", 1)]) |> keys
// -> ["a", "b"]
pub fn map_values(
in o_dict: OrderedDict(a, b),
with fun: fn(a, b, Int) -> c,
) -> OrderedDict(a, c)
Updates all values in a given dict by calling a given function on each key, value and index.
Examples
from_list([#(3, 3), #(2, 4)])
|> map_values(fn(key, value, _index) { key * value })
// -> from_list([#(3, 9), #(2, 8)])
pub fn prepend(
into o_dict: OrderedDict(a, b),
for key: a,
value val: b,
) -> OrderedDict(a, b)
Inserts a value into the dict with the given key as the first item.
If the dict already has a value for the given key then the value is replaced with the new value.
Examples
from_list([#("a", 0), #("b", 1)]) |> prepend("c", 2)
// -> from_list([#("c", 2), #("a", 0), #("b", 1)])
pub fn reorder(
in o_dict: OrderedDict(a, b),
from old_index: Int,
to new_index: Int,
) -> OrderedDict(a, b)
Change the order of a specific item, shifting it from one index to another.
Examples
from_list([#("a", 0), #("b", 1), #("c", 2)]) |> reorder(2, 0)
// -> from_list([#("c", 2), #("a", 0), #("b", 1)])
The item will be removed and readded simultaneously, so the indices of all the items between old and new indices will shift by one.
from_list([#("a", 0), #("b", 1), #("c", 2), #("d", 3), #("e", 4), #("f", 5)]) |> reorder(1, 3)
// -> from_list([#("a", 0), #("c", 2), #("d", 3), #("b", 1), #("e", 4), #("f", 5)])
from_list([#("a", 0), #("b", 1), #("c", 2), #("d", 3), #("e", 4), #("f", 5)]) |> reorder(4, 2)
// -> from_list([#("a", 0), #("b", 1), #("e", 4), #("c", 2), #("d", 3), #("f", 5)])
pub fn size(o_dict: OrderedDict(a, b)) -> Int
Determines the number of key-value pairs in the dict. This function runs in constant time and does not need to iterate the dict.
Examples
new() |> size
// -> 0
new() |> insert_end("key", "value") |> size
// -> 1
pub fn take(
from o_dict: OrderedDict(a, b),
keeping desired_keys: List(a),
) -> OrderedDict(a, b)
Creates a new ordered dict from a given ordered dict, only including any entries for which the keys are in a given list.
Examples
from_list([#("a", 0), #("b", 1)])
|> take(["b"])
// -> from_list([#("b", 1)])
from_list([#("a", 0), #("b", 1)])
|> take(["a", "b", "c"])
// -> from_list([#("a", 0), #("b", 1)])
pub fn to_list(o_dict: OrderedDict(a, b)) -> List(#(a, b))
Converts the dict to an ordered list of 2-element tuples #(key, value)
,
one for each key-value pair in the dict.
Examples
Calling to_list
on an empty dict
returns an empty list.
new() |> to_list
// -> []
The tuples will be ordered as specified
new() |> insert_end("b", 1) |> insert_end("a", 0) |> insert_at(0, "c", 2) |> to_list
// -> [#("c", 2), #("b", 1), #("a", 0)]
pub fn to_list_indexed(
o_dict: OrderedDict(a, b),
) -> List(#(Int, a, b))
Converts the dict to an ordered list of 2-element tuples #(key, value)
,
one for each key-value pair in the dict.
Examples
Calling to_list
on an empty dict
returns an empty list.
new() |> to_list
// -> []
The tuples will be ordered as specified
new() |> insert_end("b", 1) |> insert_end("a", 0) |> insert_at(0, "c", 2) |> to_list
// -> [#("c", 2), #("b", 1), #("a", 0)]
pub fn upsert(
in o_dict: OrderedDict(a, b),
update key: a,
with fun: fn(Upsert(a, b)) -> OrderedDict(a, b),
) -> OrderedDict(a, b)
Creates a new dict with one entry inserted at the end or updated using a given function.
If there was not an entry in the dict for the given key then the function
gets Insert
as its argument, otherwise it gets Update(value)
.
Example
let dict = from_list([#("a", 0)])
let increment = fn(x) {
case x {
Update(i, update) -> update(i + 1)
Insert(insert) -> insert(End, 0)
}
}
upsert(dict, "a", increment)
// -> from_list([#("a", 1)])
upsert(dict, "b", increment)
// -> from_list([#("a", 0), #("b", 0)])