MoreLabels.Map.S(3)
πΈ MoreLabels.Map.S(3) πΈ
okay so real talk, this is a page straight outta the OCaml library, manual section 3, and it's about <pink>MoreLabels.Map.S</pink> π (yes the title up top is literally MoreLabels.Map.S(3), folding it in here so you're not confused, bestie π«Ά).
β¨ NAME β¨
MoreLabels.Map.S β it's the output signature of the functor MoreLabels.Map.Make. that's it, that's the whole vibe π
π Module type π
okay so this bit is here to tell you the module type is literally named:
Module type MoreLabels.Map.S
so <cyan>Module type</cyan> MoreLabels.Map.S β no cap, that's the exact thing you're looking at, and it's giving very "you are here" energy on the map πΊοΈβ¨
hang tight bestie, more of this page is coming in part 2 π
πΊοΈ Maps
type keyβ the type of the map keys. that's the thing you look stuff up by, bestie πtype !+'a tβ the type of maps from typekeyto type'a. note the!+vibe, it's giving covariance and it's iconic β¨val empty : 'a tβ the empty map. the blank canvas. the fresh start. πval add : key:key -> data:'a -> 'a t -> 'a tβadd ~key ~data mhands you a map with all the same bindings asm, plus a binding ofkeytodata. ifkeywas already bound inmto a value that is physically equal todata, you just getmright back unchanged (the result is physically equal tom, no cap π«Ά). otherwise the old binding ofkeyinm? gone. disappears. bye. π
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 tol, wherelisdata :: Map.find key mifkeywas bound inm, and[data]otherwise. it's the "just push it onto the front" girlie πΈ
since 5.1
val update : key:key -> f:('a option -> 'a option) -> 'a t -> 'a tβupdate ~key ~f mgives you a map with the same bindings asm, except for the binding ofkey. depends on the value ofy, whereyisf (find_opt key m):- if
yisNone, the binding gets removed if it exists; - otherwise if
yisSome z, thenkeyis associated tozin the resulting map. and ifkeywas already bound inmto a value physically equal toz,mcomes back unchanged (physically equal tom, fr fr). π
- if
since 4.06
val singleton : key -> 'a -> 'a tβsingleton x yreturns that one-element map holding the bindingyforx. one key, one value, main character energy π
since 3.12
val remove : key -> 'a t -> 'a tβremove x mreturns a map with the same bindings asm, exceptx, which is unbound in the result. ifxwasn't inmat all,mcomes back unchanged (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. whether each such binding exists, and what value it has, is decided byf. talkingfind_opt-wise, you getfind_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. don't forget that proviso bestie π
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 shows up in both args,fcombines them. it's literally a special case of merge: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β returns how many bindings the map has. the headcount π
since 3.12
π Bindings
val bindings : 'a t -> (key * 'a) listβ returns the list of all the bindings of the given map. the list comes back sorted in increasing order of keys, per the orderingOrd.compare, whereOrdis the argument you handed toMoreLabels.Map.Make. π¦
since 3.12
val min_binding : 'a t -> key * 'aβ returns the binding with the smallest key in the given map (again, perOrd.compareordering), or raisesNot_foundif the map is empty. π
since 3.12
val min_binding_opt : 'a t -> (key * 'a) optionβ returns the binding with the smallest key in the given map (perOrd.compareordering), orNoneif the map is empty. the gentler version, no exceptions thrown πΈ
since 4.05
val max_binding : 'a t -> key * 'aβ same asMoreLabels.Map.S.min_binding, but it returns the binding with the largest key in the given map. π₯
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β returns one binding of the given map, or raisesNot_foundif the map is empty. which binding? unspecified, bestie π but equal bindings will be chosen for equal maps, so it's at least consistent.
since 3.12
val choose_opt : 'a t -> (key * 'a) optionβ returns one binding of the given map, orNoneif the map is empty. which binding? 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 there's no binding forx. πval find_opt : key -> 'a t -> 'a optionβfind_opt x mreturnsSome vif the current value ofxinmisv, orNoneif there's no binding forx. no drama, just options π«Ά
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) mreturns the first bindingk, vofmwhereOrd.compare k x >= 0(intuitively:k >= x), or raisesNot_foundifxis greater than any element ofm. β
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. bindings get passed tofin increasing order with respect to the ordering over the type of the keys. π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. the way that folds inward is so satisfying ngl π
π¦ Transforming
val map : f:('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 applyingftoa. bindings get passed tofin increasing order with respect to the ordering over the type of the keys. β¨val mapi : f:(key -> 'a -> 'b) -> 'a t -> 'b tβ same asMoreLabels.Map.S.map, but the function gets both the key and the associated value for each binding of the map as arguments. πval filter : f:(key -> 'a -> bool) -> 'a t -> 'a tβfilter ~f mreturns the map with all the bindings inmthat satisfy predicatef. if every single binding inmsatisfiesf,mis returned unchanged (result physically equal tom). π
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 mappliesfto every binding ofmand 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. it's giving glow-up β¨
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 predicatef, andm2is the map with all the bindings ofmthat do not satisfyf. split 'em up bestie π
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 bindings ofmwhose key is strictly less thanx;ris the map with all bindings ofmwhose key is strictly greater thanx; anddataisNoneifmhas no binding forx, orSome vifmbindsvtox. three-for-one deal π«Ά
since 3.12
β Predicates and comparisons
val is_empty : 'a t -> boolβ tests whether a map is empty or not. vibe check π³οΈval is_singleton : 'a t -> boolβ tests 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 : cmp:('a -> 'a -> bool) -> 'a t -> 'a t -> boolβequal ~cmp m1 m2tests whether the mapsm1andm2are equal, like, contain equal keys and associate them with equal data.cmpis the equality predicate used to compare the data associated with the keys. π―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. πval for_all : f:(key -> 'a -> bool) -> 'a t -> boolβfor_all ~f mchecks if all the bindings of the map satisfy predicatef. everyone has to pass, no exceptions π―
since 3.12
val exists : f:(key -> 'a -> bool) -> 'a t -> boolβexists ~f mchecks if at least one binding of the map satisfies predicatef. one is enough β¨
since 3.12
π Converting
val to_list : 'a t -> (key * 'a) listβto_list misMoreLabels.Map.S.bindings m. literally the same thing, just a cuter name π
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 inbs, the last one takes over β last one wins, periodt π).
since 5.1
val to_seq : 'a t -> (key * 'a) Seq.tβ iterates on the whole map, in ascending order of keys. π
since 4.07
val to_rev_seq : 'a t -> (key * 'a) Seq.tβ iterates 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β adds the given bindings to the map, in order. β
since 4.07
val of_seq : (key * 'a) Seq.t -> 'a tβ builds a map from the given bindings. ποΈ
since 4.07
and that's the whole thing, bestie, you're officially a Map girlie now π this page was served up by OCamldoc, dated 2026-06-22, and it lives at MoreLabels.Map.S(3) β go forth and find_opt responsibly π«Άβ¨
π¦ 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