Set.S(3)
β¨ NAME β¨
okay bestie, this is Set.S(3) from the OCaml library manual, and it's literally the output signature of the functor Set.Make. It's giving "template for a set module" and we're obsessed. π
π§ Module type π§
This page documents Module type Set.S, no cap.
π¦ Documentation π¦
Module type S
= sig end
Output signature of the functor Set.Make.
π Sets π
type eltβ the type of the set elements, periodt.type tβ the type of sets. iconic behavior.val empty : tβ the empty set, obviously. π«Ά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. β οΈ Before 4.03, physical equality was not ensured.val singleton : elt -> tβsingleton xreturns the one-element set containing onlyx. main character set energy.val remove : elt -> t -> tβremove x sreturns a set containing all elements ofs, exceptx. Ifxwas not ins,sis returned unchanged, and the result of the function is then physically equal tos. β οΈ Before 4.03, physical equality was not ensured.val union : t -> t -> tβ Set union. slay.val inter : t -> t -> tβ Set intersection. also slay.val disjoint : t -> t -> boolβ Test if two sets are disjoint. β¨ Since 4.08.val diff : t -> t -> tβ Set difference:diff s1 s2contains the elements ofs1that are not ins2.val cardinal : t -> intβ Return the number of elements of a set. it's giving count.
πΈ 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 toSet.Make.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.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. β¨ Since 4.05.val max_elt : t -> eltβ Same asSet.S.min_elt, but returns the largest element of the given set.val max_elt_opt : t -> elt optionβ Same asSet.S.min_elt_opt, but returns the largest element of the given set. β¨ Since 4.05.val 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.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. β¨ Since 4.01.val find_opt : elt -> t -> elt optionβfind_opt x sreturns the element ofsequal tox(according toOrd.compare), orNoneif no such element exists. β¨ Since 4.05.val find_first : (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.
β¨ Since 4.05.
val find_first_opt : (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.05.val find_last : (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. β¨ Since 4.05.val find_last_opt : (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 : (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.val fold : (elt -> 'acc -> 'acc) -> t -> 'acc -> 'accβfold f s initcomputes
(f xN ... (f x2 (f x1 init))...)
where x1 ... xN are the elements of s, in increasing order.
π Transforming π
val map : (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.)
β¨ Since 4.04.
val filter : (elt -> bool) -> t -> tβfilter f sreturns the set of all elements insthat satisfy predicatef. If every element inssatisfiesf,sis returned unchanged, and the result of the function is then physically equal tos. β οΈ Before 4.03, physical equality was not ensured.val filter_map : (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.
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 : (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.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.
π Predicates and comparisons π
val is_empty : t -> boolβ Test whether a set is empty or not.val is_singleton : t -> boolβ Test whether a set has exactly one element or not. β¨ Since 5.5.val mem : elt -> t -> boolβmem x stests whetherxbelongs to the sets.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. iykyk.val subset : t -> t -> boolβsubset s1 s2tests whether the sets1is a subset of the sets2.val for_all : (elt -> bool) -> t -> boolβfor_all f schecks if all elements of the set satisfy the predicatef.val exists : (elt -> bool) -> t -> boolβexists f schecks if at least one element of the set satisfies the predicatef.
π Converting π
val to_list : t -> elt listβto_list sisSet.S.elements s. β¨ Since 5.1.val 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. β¨ Since 4.02.val 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.07.val to_seq : t -> elt Seq.tβ Iterate on the whole set, in ascending order. β¨ Since 4.07.val to_rev_seq : t -> elt Seq.tβ Iterate on the whole set, in descending order. β¨ Since 4.12.val add_seq : elt Seq.t -> t -> tβ Add the given elements to the set, in order. β¨ Since 4.07.val of_seq : elt Seq.t -> tβ Build a set from the given elements. β¨ Since 4.07.
byeee bestie π this page is from OCamldoc, dated 2026-06-22, and it's literally Set.S(3). mwah π«Ά
π¦ 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