Misc.Stdlib.String.Set(3)
π Misc.Stdlib.String.Set π
okay so bestie, welcome to the Misc.Stdlib.String.Set page β¨ β this one lives in the OCaml library manual, and it's literally the string-specialised set module, no cap. It's giving "ordered collections of strings that behave" π. Buckle up, we're going through every single val, and yes, I mean every one π«Ά.
πΈ NAME πΈ
Misc.Stdlib.String.Set β no description π (the manpage really said "figure it out yourself", iconic honestly)
π§ Module π§
Module Misc.Stdlib.String.Set
β¨ Documentation β¨
Module Set : sig end
So the whole thing is a Set module signature, and inside it there are tidy little themed clusters. Let's go section by section, bestie π.
π Sets π
type eltβ the type of the set elements. iykyk, this is the thing you're putting inside the set π₯type tβ the type of sets themselves. the main character type πval empty : tβ the empty set. the blank canvas, the clean slate, 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 (the result of the function is then physically equal tos). Note the caveat below, bestie, it's important:
Before 4.03, physical equality was not ensured. π°οΈ
val singleton : elt -> tβsingleton xreturns the one-element set containing onlyx. just one (1) little guy, and honestly? she ate πΈval remove : elt -> t -> tβremove x sreturns a set containing all elements ofs, exceptx. Ifxwas not ins,sis returned unchanged (the result of the function is then physically equal tos). Same lore asadd:
Before 4.03, physical equality was not ensured. π°οΈ
val union : t -> t -> tβ set union. combine era π«Άval inter : t -> t -> tβ set intersection. the overlap, the "we have so much in common" moment πval disjoint : t -> t -> boolβ test if two sets are disjoint. literally zero chemistry, nothing shared π
Since 4.08
val diff : t -> t -> tβ set difference:diff s1 s2contains the elements ofs1that are not ins2. it's the "you're not invited to my birthday" operation πval cardinal : t -> intβ return the number of elements of a set. the headcount, the attendance list π
π¦ 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. sorted, organised, never chaotic β¨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 one π₯Ί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 vibe, but in a cuteoptionwrapper π
Since 4.05
val max_elt : t -> eltβ same asSet.S.min_elt, but returns the largest element of the given set. big energy πval max_elt_opt : t -> elt optionβ same asSet.S.min_elt_opt, but returns the largest element of the given set. big energy,optionedition π₯
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. so like, don't get attached to which one shows up π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. theoptionversion of a surprise π
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. the "do you have this in my size" of functions ποΈ
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. (note: decreasing here, bestie, don't mix it up π)
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. one at a time, in order, like a well-behaved queue πval fold : (elt -> 'acc -> 'acc) -> t -> 'acc -> 'accβfold f s initcomputes(f xN ... (f x2 (f x1 init))...), wherex1 ... xNare the elements ofs, in increasing order. the classic accumulation ritual β¨
π₯ 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 (the result of the function is then physically equal tos). the glow-up filter, only the yeses survive π
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. 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. a little triptych, we love a structured answer π
β Predicates and comparisons β
val is_empty : t -> boolβ test whether a set is empty or not. vibe check: is there anything in here π«val is_singleton : t -> boolβ test whether a set has exactly one element or not. one (1) element, no more, no less π₯Ί
Since 5.5
val mem : elt -> t -> boolβmem x stests whetherxbelongs to the sets. are you on the list or not πval equal : t -> t -> boolβequal s1 s2tests whether the setss1ands2are equal, that is, contain equal elements. twin behavior πΈval compare : t -> t -> intβ total ordering between sets. Can be used as the ordering function for doing sets of sets. sets inside sets, it's giving inception πval subset : t -> t -> boolβsubset s1 s2tests whether the sets1is a subset of the sets2. is every one of mine also one of yours π«Άval for_all : (elt -> bool) -> t -> boolβfor_all f schecks if all elements of the set satisfy the predicatef. unanimous or nothing β¨val exists : (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 sisSet.S.elements s. just a rebrand, same slay π
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. so use it, but like, watch out for the dupes π
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. start here and go up β¬οΈ
Since 4.07
val to_seq : t -> elt Seq.tβ iterate on the whole set, in ascending order. all of it, smallest to biggest πΈ
Since 4.07
val to_rev_seq : t -> elt Seq.tβ iterate on the whole set, in descending order. all of it, biggest to smallest, reverse chronology π
Since 4.12
val add_seq : elt Seq.t -> t -> tβ add the given elements to the set, in order. pouring them in one by one π«
Since 4.07
val of_seq : elt Seq.t -> tβ build a set from the given elements. from sequence to set, the glow-up pipeline β¨
Since 4.07
π«Ά ok that's the whole page, bestie π«Ά
That's literally everything β every val, every Since tag, every "physical equality was not ensured" warning, nothing skipped, periodt π
.
And a lil' sign-off: this page was generated by OCamldoc and it's dated 2026-06-22, and it's the Misc.Stdlib.String.Set(3) page, in case you need to cite your sources for the group chat ππ.
ngl, go write some sets now β you've got this ππ
π¦ 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