Map.Makemodule Ord : OrderedTypemodule M : sig ... endThese 4.05 functions provide compatibility with earlier versions of the standard library. If Prelude is linked with 4.05 or later, the official functions will be used instead.
val string_of_binding :
?sep:string ->
key:('a -> string) ->
value:('b -> string) ->
('a * 'b) ->
string(string_of_binding ~key ~value (k,v)) is a string representation of the binding of k to v, separated by sep (default: " -> "), where the string representation of each key is given by ~key and the string representation of each value is given by ~value.
(to_string ?(sep=", ") f t) returns a string representation of the map t, where the string representation of each key is given by ~key and the string representation of each value is given by ~value.
(print ?sep ~key ~value t) is (to_string ?sep ~key ~value t |> print_endline).
(random ?size ~key ~value ()) is a random map of size (size ()) (default: < 100), and each binding is given by (key (), value ()).
cmp is Ord.compare and is the comparison function that implements this map.
(lookup key) is like find but returns the complete binding (key,value).
This is useful when Ord.compare maps many keys to one, e.g. (let compare = String.(on compare lowercase_ascii)).
(replace k v t) replaces the binding for k in t with a new binding (k,v).
(of_list t alist) adds all the bindings in alist to t (which could be empty).
(adjoin vadd empty) returns a function (f k v m) that adjoins v to a collection of values associated with key k. vadd and empty might be Lists.cons and [] for list accumulators, or S.add and S.empty for some set S.
See Multiset for common situations.
Examples:
accumulate the case-insensitive spellings of words:
module M = Map.Make (String)
let each k = M.adjoin cons [] (String.lowercase_ascii k) k in
List.foldr each M.empty (split "Foo Bar bAR FOO") |> M.to_list
= [[("bar", ["Bar"; "bAR"]); ("foo", ["Foo"; "FOO"])]]separate out the even versus the odd numbers in xs (indexed under 0 for even, 1 for odd):
foldr (fun x m -> adjoin cons [] (abs (x mod 2)) x m) empty xs (classify f vadd empty) is a classifying adjoiner.
module M = Map.Make (struct type t = bool let compare = compare end)
M.(foldr (classify Int.even cons []) empty (1--10) |> to_list)
= [(false, [1; 3; 5; 7; 9]); (true, [2; 4; 6; 8; 10])](incr ?z ?i k m) is (adjoin (+) z k i m), i.e. incr generates a function for counting instances of items (k) in an (int t) (m).
~z is the "zero" for the counters (default: 0); ~i is the increment (default: 1).
Count the chars in a string:
String.foldr M.incr "abbcccd" M.empty |> M.to_list =
[('a', 1); ('b', 2); ('c', 3); ('d', 1)] The most efficient way to access the bindings of a map in key order is to use fold, but sometimes access is required in a non-fold context. OCaml's map doesn't provide this access directly, so these functions do it by calling partition, so each call to one of these functions is proportional to the size of the map.
(neighbors k m) is the pair (prev,next) where prev and next are, respectively, options carrying the binding prior to and succeeding the key k in the map m, in Ord.compare order.
If k is the first key in m (i.e. would be returned by min_binding), then prev is None. Likewise, if k is the last key in the map (i.e. would be returned by max_binding), then next is None.
Otherwise, prev and next are of the form (Some (k',v)), where (k',v) is the appropriate binding.
(prev k m) is (neighbors k m |> fst), the binding prior to k in map m.
(prev_key k m) is just the key part of the binding prior to k in map m.
(next k m) is (neighbors k m |> snd), the binding succeding k in map m.
(next_key k m) is just the key part of the binding succeeding k in map m.
val values : 'a t -> 'a list(values m) is the values in the map m as a list, in Ord.compare key-order (not value-order).