MoreLabels.Set.Make(3)
๐ MoreLabels.Set.Make(3) ๐
okay bestie, welcome to the MoreLabels.Set.Make page from the OCaml library manual. this one is lowkey a functor era and it's giving "make your own set type" energy. let's go ๐ซถ
โจ NAME โจ
MoreLabels.Set.Make โ bestie, it's a functor that builds you an implementation of the set structure, and it just needs a totally ordered type to do it. that's it. that's the whole vibe. ๐
๐ฆ Module ๐ฆ
Module MoreLabels.Set.Make
๐ธ Documentation ๐ธ
Module Make
: (Ord : OrderedType) -> sig end
so, Make is a functor building an implementation of the set structure given a totally ordered type. fr fr, that's the entire deal.
Parameters:
"Ord"โ which isMoreLabels.Set.OrderedType. that's your totally-ordered-type plug-in, iykyk ๐
๐ Sets ๐
type eltโ the type of the set elements.type tโ the type of sets. your actual set type, iconic.
val empty : tโ the empty set. the blank canvas. the "no thoughts head empty" of sets ๐ซงval add : elt -> t -> tโadd x sreturns a set containing all elements ofs, plusx. ifxwas already ins,sis returned unchanged โ and the result of the function is then physically equal tos. slay, no wasted allocation ๐
Before 4.03: physical equality was not ensured. so like, old OCaml said "maybe", and we don't stan that anymore ๐
val singleton : elt -> tโsingleton xreturns the one-element set containing onlyx. main character energy, it's all about them โจval remove : elt -> t -> tโremove x sreturns a set containing all elements ofs, exceptx. ifxwas not ins,sis returned unchanged โ result physically equal tos, in that case. so we're not allocating for nothing ๐
Before 4.03: physical equality was not ensured. same lore as before ๐ซ
val union : t -> t -> tโ set union. combine the friend groups ๐val inter : t -> t -> tโ set intersection. only the mutuals ๐val disjoint : t -> t -> boolโ test if two sets are disjoint. like, do they share zero elements? yes/no. Since 4.08val diff : t -> t -> tโ set difference:diff s1 s2contains the elements ofs1that are not ins2. the "unfollowed back" of sets ๐ญval cardinal : t -> intโ return the number of elements of a set. the headcount ๐งฎ
๐ Elements ๐
val elements : t -> elt listโ return the list of all elements of the given set. the returned list is sorted in increasing order with respect to the orderingOrd.compare, whereOrdis the argument given toMoreLabels.Set.Make. sorted, bestie, always sorted โจval min_elt : t -> eltโ return the smallest element of the given set (with respect to theOrd.compareordering), or raiseNot_foundif the set is empty. the littlest guy ๐ฅบval min_elt_opt : t -> elt optionโ return the smallest element of the given set (with respect to theOrd.compareordering), orNoneif the set is empty. same thing but the politeoptionversion ๐ Since 4.05val max_elt : t -> eltโ same asMoreLabels.Set.S.min_elt, but returns the largest element of the given set. big boss energy ๐val max_elt_opt : t -> elt optionโ same asMoreLabels.Set.S.min_elt_opt, but returns the largest element of the given set. Since 4.05val choose : t -> eltโ return one element of the given set, or raiseNot_foundif the set is empty. which element is chosen is unspecified, but equal elements will be chosen for equal sets. so it's a mystery pick, but a consistent mystery pick, and we respect that ๐ฒ โ ngl, "unspecified which one" is highkey important, so don't go depending on it being a particular element, ok?val choose_opt : t -> elt optionโ return one element of the given set, orNoneif the set is empty. which element is chosen is unspecified, but equal elements will be chosen for equal sets. Since 4.05
๐ฆ Searching ๐ฆ
val find : elt -> t -> eltโfind x sreturns the element ofsequal tox(according toOrd.compare), or raiseNot_foundif no such element exists. note bestie: it gives you back the element in the set, not the one you passed in ๐ Since 4.01val find_opt : elt -> t -> elt optionโfind_opt x sreturns the element ofsequal tox(according toOrd.compare), orNoneif no such element exists. Since 4.05val find_first : f:(elt -> bool) -> t -> eltโfind_first ~f s, wherefis a monotonically increasing function, returns the lowest elementeofssuch thatf e, or raisesNot_foundif no such element exists.
for example, find_first (fun e -> Ord.compare e x >= 0) s will return the first element e of s where Ord.compare e x >= 0 (intuitively: e >= x), or raise Not_found if x is greater than any element of s. it's giving lower bound, and we're obsessed ๐ Since 4.05
val find_first_opt : f:(elt -> bool) -> t -> elt optionโfind_first_opt ~f s, wherefis a monotonically increasing function, returns an option containing the lowest elementeofssuch thatf e, orNoneif no such element exists. Since 4.05val find_last : f:(elt -> bool) -> t -> eltโfind_last ~f s, wherefis a monotonically decreasing function, returns the highest elementeofssuch thatf e, or raisesNot_foundif no such element exists. yeah it's decreasing here, don't mix them up bestie, that's a whole trap ๐ Since 4.05val find_last_opt : f:(elt -> bool) -> t -> elt optionโfind_last_opt ~f s, wherefis a monotonically decreasing function, returns an option containing the highest elementeofssuch thatf e, orNoneif no such element exists. Since 4.05
โญ Traversing โญ
val iter : f:(elt -> unit) -> t -> unitโiter ~f sappliesfin turn to all elements ofs. the elements ofsare presented tofin increasing order with respect to the ordering over the type of the elements. in order, always ๐ซval fold : f:(elt -> 'acc -> 'acc) -> t -> init:'acc -> 'accโfold ~f s ~initcomputes(f xN ... (f x2 (f x1 init))...), wherex1 ... xNare the elements ofs, in increasing order. the way that nesting goes, it's honestly art ๐จ
๐ง Transforming ๐ง
val map : f:(elt -> elt) -> t -> tโmap ~f sis the set whose elements aref a0,f a1...f aN, wherea0,a1...aNare the elements ofs.
the elements are passed to f in increasing order with respect to the ordering over the type of the elements.
if no element of s is changed by f, s is returned unchanged. (if each output of f is physically equal to its input, the returned set is physically equal to s.) zero-waste glow-up โจ Since 4.04
val filter : f:(elt -> bool) -> t -> tโfilter ~f sreturns the set of all elements insthat satisfy predicatef. if every element inssatisfiesf,sis returned unchanged (the result of the function is then physically equal tos). nothing dropped, nothing reallocated ๐
Before 4.03: physical equality was not ensured. yeah yeah, same note ๐
val filter_map : f:(elt -> elt option) -> t -> tโfilter_map ~f sreturns the set of allvsuch thatf x = Some vfor some elementxofs.
for example,
filter_map (fun n -> if n mod 2 = 0 then Some (n / 2) else None) s
is the set of halves of the even elements of s. halve the evens, yeet the odds, iconic behaviour tbh โ๏ธ
if no element of s is changed or dropped by f (if f x = Some x for each element x), then s is returned unchanged: the result of the function is then physically equal to s. Since 4.11
val partition : f:(elt -> bool) -> t -> t * tโpartition ~f sreturns a pair of sets(s1, s2), wheres1is the set of all the elements ofsthat satisfy the predicatef, ands2is the set of all the elements ofsthat do not satisfyf. two friend groups, no drama ๐ธval split : elt -> t -> t * bool * tโsplit x sreturns a triple(l, present, r), wherelis the set of elements ofsthat are strictly less thanx;ris the set of elements ofsthat are strictly greater thanx;presentisfalseifscontains no element equal tox, ortrueifscontains an element equal tox. three-for-one, we love a triple ๐
๐ Predicates and comparisons ๐
val is_empty : t -> boolโ test whether a set is empty or not. vibe check on the emptiness ๐ซงval is_singleton : t -> boolโ test whether a set has exactly one element or not. solo-set check ๐ค Since 5.5val mem : elt -> t -> boolโmem x stests whetherxbelongs to the sets. are they in the group chat? ๐val equal : t -> t -> boolโequal s1 s2tests whether the setss1ands2are equal, that is, contain equal elements.val compare : t -> t -> intโ total ordering between sets. can be used as the ordering function for doing sets of sets. yes bestie, sets of sets, it's recursion all the way down ๐val subset : t -> t -> boolโsubset s1 s2tests whether the sets1is a subset of the sets2.val for_all : f:(elt -> bool) -> t -> boolโfor_all ~f schecks if all elements of the set satisfy the predicatef. every single one, no exceptions โval exists : f:(elt -> bool) -> t -> boolโexists ~f schecks if at least one element of the set satisfies the predicatef. just one is enough, we're not picky ๐
๐ฅ Converting ๐ฅ
val to_list : t -> elt listโto_list sisMoreLabels.Set.S.elements s. that's it, that's the whole function, it's a rebrand ๐ Since 5.1val of_list : elt list -> tโof_list lcreates a set from a list of elements. this is usually more efficient than foldingaddover the list, except perhaps for lists with many duplicated elements. so use it, unless your list is like 90% dupes ๐ซ Since 4.02val to_seq_from : elt -> t -> elt Seq.tโto_seq_from x siterates on a subset of the elements ofsin ascending order, fromxor above. Since 4.07val to_seq : t -> elt Seq.tโ iterate on the whole set, in ascending order. โฌ๏ธ Since 4.07val to_rev_seq : t -> elt Seq.tโ iterate on the whole set, in descending order. โฌ๏ธ Since 4.12val add_seq : elt Seq.t -> t -> tโ add the given elements to the set, in order. Since 4.07val of_seq : elt Seq.t -> tโ build a set from the given elements. Since 4.07
and that's the whole page, bestie!! ๐ you ate that. tysm for reading, ily, and remember: sorted, specified, and slaying ๐
sign-off szn: this lil doc was generated by OCamldoc, dated 2026-06-22, for the page MoreLabels.Set.Make(3). bye bestie, go build a set ๐ฆโจ
๐ฆ 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