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 type key to 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 m hands you a map with all the same bindings as m, plus a binding of key to data. if key was already bound in m to a value that is physically equal to data, you just get m right back unchanged (the result is physically equal to m, no cap 🫢). otherwise the old binding of key in m? 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 m is m with key mapped to l, where l is data :: Map.find key m if key was bound in m, 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 m gives you a map with the same bindings as m, except for the binding of key. depends on the value of y, where y is f (find_opt key m):
    • if y is None, the binding gets removed if it exists;
    • otherwise if y is Some z, then key is associated to z in the resulting map. and if key was already bound in m to a value physically equal to z, m comes back unchanged (physically equal to m, fr fr). πŸ’–
since 4.06
  • val singleton : key -> 'a -> 'a t β€” singleton x y returns that one-element map holding the binding y for x. one key, one value, main character energy πŸ‘‘
since 3.12
  • val remove : key -> 'a t -> 'a t β€” remove x m returns a map with the same bindings as m, except x, which is unbound in the result. if x wasn't in m at all, m comes back unchanged (physically equal to m). ✨
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 m2 computes a map whose keys are a subset of the keys of m1 and of m2. whether each such binding exists, and what value it has, is decided by f. talking find_opt-wise, you get find_opt x (merge f m1 m2) = f x (find_opt x m1) (find_opt x m2) for any key x β€” provided that f 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 m2 computes a map whose keys are a subset of the keys of m1 and of m2. when the same binding shows up in both args, f combines them. it's literally a special case of merge: union f m1 m2 is equivalent to merge f' m1 m2, where
    • f' _key None None = None
    • f' _key (Some v) None = Some v
    • f' _key None (Some v) = Some v
    • f' 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 ordering Ord.compare, where Ord is the argument you handed to MoreLabels.Map.Make. πŸ¦‹
since 3.12
  • val min_binding : 'a t -> key * 'a β€” returns the binding with the smallest key in the given map (again, per Ord.compare ordering), or raises Not_found if 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 (per Ord.compare ordering), or None if the map is empty. the gentler version, no exceptions thrown 🌸
since 4.05
  • val max_binding : 'a t -> key * 'a β€” same as MoreLabels.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 as MoreLabels.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 raises Not_found if 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, or None if 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 m returns the current value of x in m, or raises Not_found if there's no binding for x. πŸ’…
  • val find_opt : key -> 'a t -> 'a option β€” find_opt x m returns Some v if the current value of x in m is v, or None if there's no binding for x. no drama, just options 🫢
since 4.05
  • val find_first : f:(key -> bool) -> 'a t -> key * 'a β€” find_first ~f m, where f is a monotonically increasing function, returns the binding of m with the lowest key k such that f k, or raises Not_found if no such key exists. for example, find_first (fun k -> Ord.compare k x >= 0) m returns the first binding k, v of m where Ord.compare k x >= 0 (intuitively: k >= x), or raises 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, where f is a monotonically increasing function, returns an option containing the binding of m with the lowest key k such that f k, or None if no such key exists. ✨
since 4.05
  • val find_last : f:(key -> bool) -> 'a t -> key * 'a β€” find_last ~f m, where f is a monotonically decreasing function, returns the binding of m with the highest key k such that f k, or raises Not_found if no such key exists. πŸ”₯
since 4.05
  • val find_last_opt : f:(key -> bool) -> 'a t -> (key * 'a) option β€” find_last_opt ~f m, where f is a monotonically decreasing function, returns an option containing the binding of m with the highest key k such that f k, or None if no such key exists. ✨
since 4.05

🚢 Traversing

  • val iter : f:(key:key -> data:'a -> unit) -> 'a t -> unit β€” iter ~f m applies f to all bindings in map m. f receives the key as first argument and the associated value as second argument. bindings get passed to f in 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 ~init computes (f kN dN ... (f k1 d1 init)...), where k1 ... kN are the keys of all bindings in m (in increasing order) and d1 ... dN are the associated data. the way that folds inward is so satisfying ngl πŸŒ€

πŸ¦‹ Transforming

  • val map : f:('a -> 'b) -> 'a t -> 'b t β€” map ~f m returns a map with the same domain as m, where the associated value a of all bindings of m has been replaced by the result of applying f to a. bindings get passed to f in increasing order with respect to the ordering over the type of the keys. ✨
  • val mapi : f:(key -> 'a -> 'b) -> 'a t -> 'b t β€” same as MoreLabels.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 m returns the map with all the bindings in m that satisfy predicate f. if every single binding in m satisfies f, m is returned unchanged (result physically equal to m). πŸ’…
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 m applies f to every binding of m 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. it's giving glow-up ✨

since 4.11
  • val partition : f:(key -> 'a -> bool) -> 'a t -> 'a t * 'a t β€” partition ~f m returns a pair of maps (m1, m2), where m1 contains all the bindings of m that satisfy predicate f, and m2 is the map with all the bindings of m that do not satisfy f. split 'em up bestie πŸŽ€
since 3.12
  • val split : key -> 'a t -> 'a t * 'a option * 'a t β€” split x m returns a triple (l, data, r), where l is the map with all bindings of m whose key is strictly less than x; r is the map with all bindings of m whose key is strictly greater than x; and data is None if m has no binding for x, or Some v if m binds v to x. 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 m returns true if m contains a binding for x, and false otherwise. πŸ”
  • val equal : cmp:('a -> 'a -> bool) -> 'a t -> 'a t -> bool β€” equal ~cmp m1 m2 tests whether the maps m1 and m2 are equal, like, contain equal keys and associate them with equal data. cmp is 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 m checks if all the bindings of the map satisfy predicate f. everyone has to pass, no exceptions πŸ’―
since 3.12
  • val exists : f:(key -> 'a -> bool) -> 'a t -> bool β€” exists ~f m checks if at least one binding of the map satisfies predicate f. one is enough ✨
since 3.12

πŸ”„ Converting

  • val to_list : 'a t -> (key * 'a) list β€” to_list m is MoreLabels.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 bs adds the bindings of bs to the empty map, in list order (if a key is bound twice in bs, 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 m iterates on a subset of the bindings of m, in ascending order of keys, from key k or 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