MoreLabels.Map.Make(3)
πΈ MoreLabels.Map.Make πΈ
okay bestie, you're looking at the manual page for MoreLabels.Map.Make from the OCaml library β and yes, it's giving iconic functor energy π
π NAME π
MoreLabels.Map.Make β Functor building an implementation of the map structure given a totally ordered type.
π¦ Module π¦
Module MoreLabels.Map.Make
β¨ Documentation β¨
Module Make
: (Ord : OrderedType) -> sig end
Functor building an implementation of the map structure given a totally ordered type. Literally the blueprint for making a map module, no cap π«Ά
Parameters:
"Ord"βMoreLabels.Map.OrderedType
πΊοΈ 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. A whole lot of nothing, but make it type-safe β¨val add : key:key -> data:'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. Old binding? Gone. Periodt π
Before 4.03, physical equality was not ensured.
val add_to_list : key:key -> data:'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:key -> f:('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. Tiny map, main character energy π
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). β¨
Before 4.03, physical equality was not ensured.
val merge : f:(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 : f:(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β return the number of bindings of a map. Counting era π―
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 toMoreLabels.Map.Make. Sorted and slaying β¨
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. Option vibes πΈ
Since 4.05
val max_binding : 'a t -> key * 'aβ same asMoreLabels.Map.S.min_binding, but returns the binding with the largest key in the given map. Big key energy π
Since 3.12
val max_binding_opt : 'a t -> (key * 'a) optionβ same asMoreLabels.Map.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. Mystery pick, but consistent π
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. Soft-launch searching πΈ
Since 4.05
val find_first : f:(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 : f:(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 : f:(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 : f:(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 : f:(key:key -> data:'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. Walk through it, bestie β¨val fold : f:(key:key -> data:'a -> 'acc -> 'acc) -> 'a t -> init:'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 it up, iconic behavior π
πͺ Transforming πͺ
val map : f:('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. Glow-up for every value πval mapi : f:(key -> 'a -> 'b) -> 'a t -> 'b tβ same asMoreLabels.Map.S.map, but the function receives as arguments both the key and the associated value for each binding of the map. Key AND value? Obsessed πval filter : f:(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). Keep only the ones that pass the vibe check β
Before 4.03, physical equality was not ensured.
Since 3.12
val filter_map : f:(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 : f:(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. Split into yes and no, literally π
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. Three-part reveal β¨
Since 3.12
β Predicates and comparisons β
val is_empty : 'a t -> boolβ test whether a map is empty or not. Empty check, no cap π«Άval is_singleton : 'a t -> boolβ test whether a map has exactly one element or not. Solo map check π
Since 5.5
val mem : key -> 'a t -> boolβmem x mreturnstrueifmcontains a binding forx, andfalseotherwise. Is it in there or not? πval equal : cmp:('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. Twin check πval compare : cmp:('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. Ranking era πval for_all : f:(key -> 'a -> bool) -> 'a t -> boolβfor_all ~f mchecks if all the bindings of the map satisfy the predicatef. Everyone has to pass β
Since 3.12
val exists : f:(key -> 'a -> bool) -> 'a t -> boolβexists ~f mchecks if at least one binding of the map satisfies the predicatef. Just one is enough, periodt π
Since 3.12
π Converting π
val to_list : 'a t -> (key * 'a) listβto_list misMoreLabels.Map.S.bindings m. Map to list, easy π
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). Last one wins, no cap π
Since 5.1
val to_seq : 'a t -> (key * 'a) Seq.tβ iterate on the whole map, in ascending order of keys. Sequence time β
Since 4.07
val to_rev_seq : 'a t -> (key * 'a) Seq.tβ iterate on the whole map, in descending order of keys. Reverse sequence, still slaying π₯
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. Start fromkand go up π¦
Since 4.07
val add_seq : (key * 'a) Seq.t -> 'a t -> 'a tβ okay so this one is literally βadd the given bindings to the map, in orderβ π . You bring the sequence of key/value pairs, it does the lilβ insertion dance, and you get an'a tback. Since 4.07val of_seq : (key * 'a) Seq.t -> 'a tβ bestie, this is the build-a-map-from-the-given-bindings button β¨. Hand it a(key * 'a) Seq.t, receive an'a t, no cap. Since 4.07
and thatβs the page, bestie π«Ά β the footer says OCamldoc, dated 2026-06-22, for MoreLabels.Map.Make(3). iconic.
π¦ 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