APIDict
Dict
Immutable dictionaries ordered by key.
A Dict k v maps keys of any Ord type to values. Operations that find a
key need Ord k; the rest never compare keys and take no constraint.
Traversals visit keys in ascending order, except foldr, which descends,
and callbacks run in that order.
size and isEmpty are constant time; get, member, insert,
remove, and update are logarithmic; keys, values, toList, the
folds, and map are linear; fromList is n log n. An update shares every
node off its search path rather than copying the dictionary.
Dict
type Dict k vSource
A dictionary from keys k to values v. Its representation is private.
Dictionaries are equal when they hold the same entries, whatever order
they were built in, and display their entries in key order.
import Dict exposing (Dict)
none : Dict Int String
none = Dict.empty
Dict.fromList [(2, "b"), (1, "a")] == Dict.fromList [(1, "a"), (2, "b")]
show (Dict.fromList [(2, "b"), (1, "a")]) == "Dict [1 = \"a\", 2 = \"b\"]"
show none == "Dict []"
Instances:
(Eq k, Eq v) => Eq (Dict k v)
(Show k, Show v) => Show (Dict k v)
empty
empty : Dict a bSource
The dictionary with no entries.
foldl
foldl : ((a, b) -> c ->{e} c) -> c -> Dict a b ->{e} cSource
Combines the entries into one value in ascending key order: combine
receives each (key, value) pair and the result so far.
import Dict
Dict.foldl { (k, v) acc -> acc ++ k ++ v } "" (Dict.fromList [("b", "2"), ("a", "1")]) == "a1b2"
foldr
foldr : ((a, b) -> c ->{e} c) -> c -> Dict a b ->{e} cSource
Combines the entries into one value in descending key order: combine
receives each (key, value) pair and the result so far.
import Dict
Dict.foldr { (k, _) acc -> [k | acc] } [] (Dict.fromList [(2, "b"), (1, "a")]) == [1, 2]
fromList
fromList : Ord a => List (a, b) -> Dict a bSource
The dictionary of the given entries. When a key repeats, the later entry’s value wins and the first entry’s key is kept.
import Dict
Dict.fromList [(1, "one"), (1, "uno")] == Dict.fromList [(1, "uno")]
get
get : Ord a => a -> Dict a b -> Maybe bSource
The value stored under key, or Nothing when it is absent.
import Dict
Dict.get 1 (Dict.fromList [(1, "one")]) == Just "one"
Dict.get 2 (Dict.fromList [(1, "one")]) == Nothing
insert
insert : Ord a => a -> b -> Dict a b -> Dict a bSource
The dictionary with value stored under key, replacing any value it had.
A key already present keeps its original key; only an Ord that calls
distinguishable keys equivalent can tell.
import Dict
Dict.get 1 (Dict.insert 1 "uno" (Dict.fromList [(1, "one")])) == Just "uno"
isEmpty
isEmpty : Dict a b -> BoolSource
Whether the dictionary has no entries.
keys
keys : Dict a b -> List aSource
The keys in ascending order.
import Dict
Dict.keys (Dict.fromList [(2, "b"), (1, "a")]) == [1, 2]
map
map : ((a, b) ->{e} c) -> Dict a b ->{e} Dict a cSource
Replaces every value with transform of its (key, value) pair, keeping
the keys. transform runs in ascending key order.
import Dict
Dict.map { (k, v) -> k * v } (Dict.fromList [(2, 10), (3, 10)]) == Dict.fromList [(2, 20), (3, 30)]
member
member : Ord a => a -> Dict a b -> BoolSource
Whether key has an entry.
import Dict
Dict.member 1 (Dict.fromList [(1, "one")])
remove
remove : Ord a => a -> Dict a b -> Dict a bSource
The dictionary without an entry for key; unchanged when it has none.
import Dict
Dict.remove 1 (Dict.fromList [(1, "one"), (2, "two")]) == Dict.fromList [(2, "two")]
singleton
singleton : a -> b -> Dict a bSource
The dictionary with one entry.
import Dict
Dict.singleton 1 "one" == Dict.insert 1 "one" Dict.empty
Dict.size (Dict.singleton 1 "one") == 1
size
size : Dict a b -> IntSource
The number of entries, in constant time.
toList
toList : Dict a b -> List (a, b)Source
The entries in ascending key order.
import Dict
Dict.toList (Dict.fromList [(2, "b"), (1, "a")]) == [(1, "a"), (2, "b")]
update
update : Ord a => a -> (Maybe b ->{e} Maybe b) -> Dict a b ->{e} Dict a bSource
Changes the entry for key through alter, which receives its current
value, or Nothing, and answers the new one: Nothing removes a present
key and leaves an absent one absent. alter runs exactly once.
import Dict
increment : Maybe Int -> Maybe Int
increment count = Just (Maybe.withDefault 0 count + 1)
Dict.update "a" increment (Dict.fromList [("a", 1)]) == Dict.fromList [("a", 2)]
Dict.update "b" increment (Dict.fromList [("a", 1)]) == Dict.fromList [("a", 1), ("b", 1)]
Dict.update "a" { _ -> Nothing } (Dict.fromList [("a", 1)]) == Dict.empty
values
values : Dict a b -> List bSource
The values in ascending order of their keys.
import Dict
Dict.values (Dict.fromList [(2, "b"), (1, "a")]) == ["a", "b"]
