Misc.Stdlib.String.Map(3)
π Misc.Stdlib.String.Map(3) β OCaml library π
okay bestie, this is the Misc.Stdlib.String.Map(3) page from the OCaml library manual, and we are gonna get through it together. It's giving very "module docs but make it cute" energy. β¨
πΈ NAME πΈ
Misc.Stdlib.String.Map β no description. Literally no description, not me being lazy π
π¦ Module π¦
Module Misc.Stdlib.String.Map
π Documentation π
Module Map : sig end
πΊοΈ Maps
type keyβ the type of the map keys. πtype !+'a tβ the type of maps from typekeyto type'a.val empty : 'a tβ the empty map. So empty, so iconic π«Άval add : key -> 'a -> 'a t -> 'a tβadd key data mreturns a map containing the same bindings asm, plus a binding ofkeytodata. Ifkeywas already bound inmto a value that is physically equal todata,mis returned unchanged (and the result is then physically equal tom). Otherwise, the previous binding ofkeyinmdisappears. Before4.03 physical equality was not ensured.val add_to_list : key -> 'a -> 'a list t -> 'a list tβadd_to_list key data mismwithkeymapped tolsuch thatlisdata :: Map.find key mifkeywas bound inm, and[data]otherwise. Since 5.1val update : key -> ('a option -> 'a option) -> 'a t -> 'a tβupdate key f mreturns a map containing the same bindings asm, except for the binding ofkey. Depending on the value ofy, whereyisf (find_opt key m), the binding ofkeyis added, removed, or updated. IfyisNone, the binding is removed if it exists; otherwise, ifyisSome z, thenkeyis associated tozin the resulting map. Ifkeywas already bound inmto a value that is physically equal toz,mis returned unchanged (the result is then physically equal tom). Since 4.06val singleton : key -> 'a -> 'a tβsingleton x yreturns the one-element map that contains a bindingyforx. Since 3.12val remove : key -> 'a t -> 'a tβremove x mreturns a map containing the same bindings asm, except forx, which is unbound in the returned map. Ifxwas not inm,mis returned unchanged (the result is then physically equal tom). Before4.03 physical equality was not ensured.val merge : (key -> 'a option -> 'b option -> 'c option) -> 'a t -> 'b t -> 'c tβmerge f m1 m2computes a map whose keys are a subset of the keys ofm1and ofm2. The presence of each such binding, and the corresponding value, is determined with the functionf. In terms of thefind_optoperation, we havefind_opt x (merge f m1 m2) = f x (find_opt x m1) (find_opt x m2)for any keyx, provided thatf x None None = None. Since 3.12val union : (key -> 'a -> 'a -> 'a option) -> 'a t -> 'a t -> 'a tβunion f m1 m2computes a map whose keys are a subset of the keys ofm1and ofm2. When the same binding is defined in both arguments, the functionfis used to combine them. This is a special case ofmerge:union f m1 m2is equivalent tomerge f' m1 m2, where:f' _key None None = Nonef' _key (Some v) None = Some vf' _key None (Some v) = Some vf' key (Some v1) (Some v2) = f key v1 v2
Since 4.03
val cardinal : 'a t -> intβ returns the number of bindings of a map. Since 3.12
π§· Bindings
val bindings : 'a t -> (key * 'a) listβ returns the list of all bindings of the given map. The returned list is sorted in increasing order of keys with respect to the orderingOrd.compare, whereOrdis the argument given toMap.Make. Since 3.12val min_binding : 'a t -> key * 'aβ returns the binding with the smallest key in a given map (with respect to theOrd.compareordering), or raisesNot_foundif the map is empty. Since 3.12val min_binding_opt : 'a t -> (key * 'a) optionβ returns the binding with the smallest key in the given map (with respect to theOrd.compareordering), orNoneif the map is empty. Since 4.05val max_binding : 'a t -> key * 'aβ same asMap.S.min_binding, but returns the binding with the largest key in the given map. Since 3.12val max_binding_opt : 'a t -> (key * 'a) optionβ same asMap.S.min_binding_opt, but returns the binding with the largest key in the given map. Since 4.05val choose : 'a t -> key * 'aβ returns one binding of the given map, or raisesNot_foundif the map is empty. Which binding is chosen is unspecified, but equal bindings will be chosen for equal maps. Since 3.12val choose_opt : 'a t -> (key * 'a) optionβ returns one binding of the given map, orNoneif the map is empty. Which binding is chosen is unspecified, but equal bindings will be chosen for equal maps. Since 4.05
π Searching
val find : key -> 'a t -> 'aβfind x mreturns the current value ofxinm, or raisesNot_foundif no binding forxexists.val find_opt : key -> 'a t -> 'a optionβfind_opt x mreturnsSome vif the current value ofxinmisv, orNoneif no binding forxexists. Since 4.05val find_first : (key -> bool) -> 'a t -> key * 'aβfind_first f m, wherefis a monotonically increasing function, returns the binding ofmwith the lowest keyksuch thatf k, or raisesNot_foundif no such key exists. For example,find_first (fun k -> Ord.compare k x >= 0) mwill return the first bindingk, vofmwhereOrd.compare k x >= 0(intuitively:k >= x), or raiseNot_foundifxis greater than any element ofm. Since 4.05val find_first_opt : (key -> bool) -> 'a t -> (key * 'a) optionβfind_first_opt f m, wherefis a monotonically increasing function, returns an option containing the binding ofmwith the lowest keyksuch thatf k, orNoneif no such key exists. Since 4.05val find_last : (key -> bool) -> 'a t -> key * 'aβfind_last f m, wherefis a monotonically decreasing function, returns the binding ofmwith the highest keyksuch thatf k, or raisesNot_foundif no such key exists. Since 4.05val find_last_opt : (key -> bool) -> 'a t -> (key * 'a) optionβfind_last_opt f m, wherefis a monotonically decreasing function, returns an option containing the binding ofmwith the highest keyksuch thatf k, orNoneif no such key exists. Since 4.05
πΆ Traversing
val iter : (key -> 'a -> unit) -> 'a t -> unitβiter f mappliesfto all bindings in mapm.freceives the key as first argument, and the associated value as second argument. The bindings are passed tofin increasing order with respect to the ordering over the type of the keys.val fold : (key -> 'a -> 'acc -> 'acc) -> 'a t -> 'acc -> 'accβfold f m initcomputes(f kN dN ... (f k1 d1 init)...), wherek1 ... kNare the keys of all bindings inm(in increasing order), andd1 ... dNare the associated data.
πͺ Transforming
val map : ('a -> 'b) -> 'a t -> 'b tβmap f mreturns a map with the same domain asm, where the associated valueaof all bindings ofmhas been replaced by the result of the application offtoa. The bindings are passed tofin increasing order with respect to the ordering over the type of the keys.val mapi : (key -> 'a -> 'b) -> 'a t -> 'b tβ same asMap.S.map, but the function receives as arguments both the key and the associated value for each binding of the map.val filter : (key -> 'a -> bool) -> 'a t -> 'a tβfilter f mreturns the map with all the bindings inmthat satisfy predicatef. If every binding inmsatisfiesf,mis returned unchanged (the result is then physically equal tom). Before4.03 physical equality was not ensured. Since 3.12val filter_map : (key -> 'a -> 'b option) -> 'a t -> 'b tβfilter_map f mapplies the functionfto every binding ofm, and builds a map from the results. For each binding(k, v)in the input map:- -if
f k visNonethenkis not in the result, - -if
f k visSome v'then the binding(k, v')is in the output map.
- -if
For example, the following function on maps whose values are lists:
filter_map
(fun _k li -> match li with [] -> None | _::tl -> Some tl)
m
drops all bindings of m whose value is an empty list, and pops the first element of each value that is non-empty. Since 4.11
val partition : (key -> 'a -> bool) -> 'a t -> 'a t * 'a tβpartition f mreturns a pair of maps(m1, m2), wherem1contains all the bindings ofmthat satisfy the predicatef, andm2is the map with all the bindings ofmthat do not satisfyf. Since 3.12val split : key -> 'a t -> 'a t * 'a option * 'a tβsplit x mreturns a triple(l, data, r), wherelis the map with all the bindings ofmwhose key is strictly less thanx;ris the map with all the bindings ofmwhose key is strictly greater thanx;dataisNoneifmcontains no binding forx, orSome vifmbindsvtox. Since 3.12
β Predicates and comparisons
val is_empty : 'a t -> boolβ test whether a map is empty or not.val is_singleton : 'a t -> boolβ test whether a map has exactly one element or not. Since 5.5val mem : key -> 'a t -> boolβmem x mreturnstrueifmcontains a binding forx, andfalseotherwise.val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> boolβequal cmp m1 m2tests whether the mapsm1andm2are equal, that is, contain equal keys and associate them with equal data.cmpis the equality predicate used to compare the data associated with the keys.val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> intβ total ordering between maps. The first argument is a total ordering used to compare data associated with equal keys in the two maps.val for_all : (key -> 'a -> bool) -> 'a t -> boolβfor_all f mchecks if all the bindings of the map satisfy the predicatef. Since 3.12val exists : (key -> 'a -> bool) -> 'a t -> boolβexists f mchecks if at least one binding of the map satisfies the predicatef. Since 3.12
π Converting
val to_list : 'a t -> (key * 'a) listβto_list misMap.S.bindings m. Since 5.1val of_list : (key * 'a) list -> 'a tβof_list bsadds the bindings ofbsto the empty map, in list order (if a key is bound twice inbsthe last one takes over). Since 5.1val to_seq : 'a t -> (key * 'a) Seq.tβ iterate on the whole map, in ascending order of keys. Since 4.07val to_rev_seq : 'a t -> (key * 'a) Seq.tβ iterate on the whole map, in descending order of keys. Since 4.12val to_seq_from : key -> 'a t -> (key * 'a) Seq.tβto_seq_from k miterates on a subset of the bindings ofm, in ascending order of keys, from keykor above. Since 4.07val add_seq : (key * 'a) Seq.t -> 'a t -> 'a tβ add the given bindings to the map, in order. Since 4.07val of_seq : (key * 'a) Seq.t -> 'a tβ build a map from the given bindings. Since 4.07
That's the whole page, bestie π. The footer says OCamldoc, dated 2026-06-22, for Misc.Stdlib.String.Map(3). Periodt. π
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms