Map.Make(3)
π Map.Make(3) β OCaml library, bestie edition π
okay so this is a page from the OCaml library manual β Map.Make(3), right at the top, before we even get into the actual functor tea. Consider this the cover of the album, the intro track, the little vibe-setting moment before the real content shows up. π
πΈ NAME πΈ
Bestie, listen. Here's the whole one-line summary of what this thing is, no cap:
A functor that builds an implementation of the map structure, given a totally ordered type. That's it, that's the whole pitch β¨ β a functor is the recipe-builder, the map structure is what gets cooked up, and the totally ordered type is the ingredient you have to bring yourself. iykyk π
π¦ Module π¦
The module is literally, exactly:
Module Map.Make
That's the name, spelled the same way every time, lowercase ake and everything β Map.Make, not Map.make, not Map.MAKES, we are not doing that π. Copy it character for character and you're golden. β
π heads up bestie: this page is long, so it's split into parts β part 1 is right here, so don't go thinking the story ends. more incoming π«Ά
πΊοΈ Maps πΊοΈ
Okay so the map vibe starts here, bestie. π
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 canvas. π¨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. π
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. One is the loneliest number but she ate. π
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. Counting queen behavior. π’
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. Main character energy but make it unspecified. π
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. The classic βwhere is sheβ moment. π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) mwill return the first bindingk, vofmwhereOrd.compare k x >= 0(intuitively:k >= x), or raiseNot_foundifxis greater than any element ofm. π¦
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. Fold her, sheβs iconic. π
π¦ 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 v is None then k is not in the result,
- -if f k v is Some v' then the binding (k, v') is in the output map.
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. Vibe check: 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
Thatβs the whole Map.Make(3) glow-up, bestie π
β official source/version/date tea: OCamldoc, 2026-06-22, Map.Make(3). Now go make some maps that absolutely ate. ππ
π¦ 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