Map.S(3)
π Map.S(3) β OCaml library, bestie edition π
okay so hi, this is the manual page for Map.S in the OCaml library β and yes itβs giving βoutput signature of the functor Map.Makeβ main-character energy. Letβs get into it π
πΈ NAME πΈ
Map.S β Output signature of the functor Map.Make.
π§ Module type π§
Module type Map.S
Module type S
= sig end
π Documentation π
Module type S
= sig end
Output signature of the functor Map.Make . periodt π
πΊοΈ 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. literally the blank slate β¨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 (the result of the function is then physically equal tom). Otherwise, the previous binding ofkeyinmdisappears. no cap, that physical-equality bit is the vibe check π
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 inmand[data]otherwise. π
Since 5.1.
val 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 ofywhereyisf (find_opt key m), the binding ofkeyis added, removed or updated. IfyisNone, the binding is removed if it exists; otherwise, ifyisSome zthenkeyis associated tozin the resulting map. Ifkeywas already bound inmto a value that is physically equal toz,mis returned unchanged (the result of the function is then physically equal tom). π₯
Since 4.06.
val singleton : key -> 'a -> 'a tβsingleton x yreturns the one-element map that contains a bindingyforx. π
Since 3.12.
val remove : key -> 'a t -> 'a tβremove x mreturns a map containing the same bindings asm, except forxwhich is unbound in the returned map. Ifxwas not inm,mis returned unchanged (the result of the function 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.12.
val 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, wheref' _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β return the number of bindings of a map. π’
Since 3.12.
π Bindings π
val bindings : 'a t -> (key * 'a) listβ return 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.12.
val min_binding : 'a t -> key * 'aβ return the binding with the smallest key in a given map (with respect to theOrd.compareordering), or raiseNot_foundif the map is empty. π₯Ί
Since 3.12.
val min_binding_opt : 'a t -> (key * 'a) optionβ return the binding with the smallest key in the given map (with respect to theOrd.compareordering), orNoneif the map is empty. π
Since 4.05.
val 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.12.
val 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.05.
val choose : 'a t -> key * 'aβ return one binding of the given map, or raiseNot_foundif the map is empty. Which binding is chosen is unspecified, but equal bindings will be chosen for equal maps. π²
Since 3.12.
val choose_opt : 'a t -> (key * 'a) optionβ return 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.05.
val 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) m will return the first binding k, v of m where Ord.compare k x >= 0 (intuitively: k >= x ), or raise Not_found if x is greater than any element of m .
Since 4.05.
val 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.05.
val 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.05.
val 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 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 of the function is then physically equal tom) π
Before4.03 β Physical equality was not ensured.
Since 3.12.
val 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.12.
val 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.5.
val 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.12.
val 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.1.
val 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.1.
val to_seq : 'a t -> (key * 'a) Seq.tβ iterate on the whole map, in ascending order of keys. π
Since 4.07.
val to_rev_seq : 'a t -> (key * 'a) Seq.tβ iterate on the whole map, in descending order of keys. π¦
Since 4.12.
val 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.07.
val add_seq : (key * 'a) Seq.t -> 'a t -> 'a tβ add the given bindings to the map, in order. π
Since 4.07.
val of_seq : (key * 'a) Seq.t -> 'a tβ build a map from the given bindings. π«Ά
Since 4.07.
π cute little sign-off: this iconic page was brought to you by OCamldoc, dated 2026-06-22 β and thatβs the whole manual, no crumbs, 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