Module Tablecloth.Set Source A collection of unique values
A Set represents a collection of unique values.
Set is an immutable data structure which means operations like Set.add and Set.remove do not modify the data structure, but return a new set with the desired changes.
Since sets of ints and strings are so common the specialised Set.Int and Set.String modules are available which offer a convenient way to construct new sets.
Custom data types can be used with sets as long as the module satisfies the Comparator.S interface.
module Point = struct
type t = int * int
let compare = Tuple2.compare ~f:Int.compare ~g:Int.compare
include Comparator.Make(struct
type nonrec t = t
let compare = compare
end)
end
let points : Set.Of(Point).t = Set.from_list (module Point) [(0, 0); (3, 4); (6, 7)]See the Comparator module for a more details.
Source module Of (M : sig ... end ) : sig ... end This functor lets you describe the type of Sets a little more concisely.
CreateYou can create a Set by providing a module conform to the Comparator.S signature by using empty , singleton , from_list or from_array .
Specialised versions of the empty , singleton , from_list and from_array functions available in the Set.Int and Set.String sub-modules.
Source val empty :
(module Tablecloth__.TableclothComparator.S
with type identity = 'identity
and type t = 'a ) ->
('a , 'identity ) t A set with nothing in it.
Often used as an initial value for functions like Array.fold
Examples
Array.fold
[|'m'; 'i'; 's'; 's'; 'i'; 's'; 's';'i';'p';'p';'i'|]
~initial:(Set.empty (module Char))
~f:Set.add
|> Set.to_array
= [|'i'; 'm'; 'p'; 's'|]Source val singleton :
(module Tablecloth__.TableclothComparator.S
with type identity = 'identity
and type t = 'a ) ->
'a ->
('a , 'identity ) t Create a set from a single Int .
Examples
Set.singleton (module Int) 7 |> Set.to_list = [7]Source val from_array :
(module Tablecloth__.TableclothComparator.S
with type identity = 'identity
and type t = 'a ) ->
'a array ->
('a , 'identity ) t Create a set from an Array .
Examples
Set.from_array (module String) [|"Ant"; "Bat"; "Bat"; "Goldfish"|] |> Set.to_array = [|"Ant";"Bat";"Goldfish"|]Source val from_list :
(module Tablecloth__.TableclothComparator.S
with type identity = 'identity
and type t = 'a ) ->
'a list ->
('a , 'identity ) t Create a set from a List .
Examples
Set.from_list (module Char) ['A'; 'B'; 'B'; 'G'] |> Set.to_list = ['A';'B';'G'] Basic operationsSource val add : ('a , 'id ) t -> value :'a -> ('a , 'id ) t Insert a value into a set.
Examples
Set.Int.from_list [1; 2] |> Set.add ~value:3 |> Set.to_list = [1; 2; 3]Set.Int.from_list [1; 2] |> Set.add ~value:2 |> Set.to_list = [1; 2]Source val remove : ('a , 'id ) t -> value :'a -> ('a , 'id ) t Remove a value from a set, if the set doesn't contain the value anyway, returns the original set.
Examples
Set.Int.from_list [1; 2] |> Set.remove ~value:2 |> Set.to_list = [1] let original_set = Set.Int.from_list [1; 2] in
let new_set = Set.remove ~value:3 original_set in
original_set = new_setSource val includes : ('a , _ ) t -> value :'a -> boolDetermine if a value is in a set.
Examples
Set.includes (Set.String.from_list ["Ant"; "Bat"; "Cat"]) ~value:"Bat" = trueSource val (.?{}) : ('element , _ ) t -> 'element -> boolThe index operator version of includes
Note Currently this is only supported by the OCaml syntax.
Examples
let animals = Set.String.from_list ["Ant"; "Bat"; "Cat"] in
animals.Set.?{"Emu"} = falseDetermine the number of elements in a set.
Examples
Set.length (Set.Int.from_list [1; 2; 3]) = 3Source val find : ('value , _ ) t -> f :('value -> bool) -> 'value optionReturns, as an Option , the first element for which f evaluates to true. If f doesn't return true for any of the elements find will return None.
Examples
Set.find ~f:Int.is_even (Set.Int.from_list [1; 3; 4; 8]) = Some 4Set.find ~f:Int.is_odd (Set.Int.from_list [0; 2; 4; 8]) = NoneSet.find ~f:Int.is_even Set.Int.empty = None QuerySource val is_empty : (_ , _ ) t -> boolCheck if a set is empty.
Examples
Set.is_empty (Set.Int.empty) = trueSet.is_empty (Set.Int.singleton 4) = falseSource val any : ('value , _ ) t -> f :('value -> bool) -> boolDetermine if f returns true for any values in a set.
Examples
Set.any (Set.Int.from_array [|2;3|]) ~f:Int.is_even = trueSet.any (Set.Int.from_list [1;3]) ~f:Int.is_even = falseSet.any (Set.Int.from_list []) ~f:Int.is_even = falseSource val all : ('value , _ ) t -> f :('value -> bool) -> boolDetermine if f returns true for all values in a set.
Examples
Set.all ~f:Int.is_even (Set.Int.from_array [|2;4|]) = trueSet.all ~f:Int.is_even (Set.Int.from_list [2;3]) = falseSet.all ~f:Int.is_even Set.Int.empty = true CombineSource val difference : ('a , 'id ) t -> ('a , 'id ) t -> ('a , 'id ) t Returns a new set with the values from the first set which are not in the second set.
Examples
Set.difference (Set.Int.from_list [1;2;5]) (Set.Int.from_list [2;3;4]) |> Set.to_list = [1;5]Set.difference (Set.Int.from_list [2;3;4]) (Set.Int.from_list [1;2;5]) |> Set.to_list = [3;4]Source val intersection : ('a , 'id ) t -> ('a , 'id ) t -> ('a , 'id ) t Get the intersection of two sets. Keeps values that appear in both sets.
Examples
Set.intersection (Set.Int.from_list [1;2;5]) (Set.Int.from_list [2;3;4]) |> Set.to_list= [2]Source val union : ('a , 'id ) t -> ('a , 'id ) t -> ('a , 'id ) t Get the union of two sets. Keep all values.
Examples
Set.union (Set.Int.from_list [1;2;5]) (Set.Int.from_list [2;3;4]) |> Set.to_list = [1;2;3;4;5]Source val filter : ('a , 'id ) t -> f :('a -> bool) -> ('a , 'id ) t Keep elements that f returns true for.
Examples
Set.filter (Set.Int.from_list [1;2;3]) ~f:Int.is_even |> Set.to_list = [2]Source val partition : ('a , 'id ) t -> f :('a -> bool) -> ('a , 'id ) t * ('a , 'id ) t Divide a set into two according to f. The first set will contain the values that f returns true for, values that f returns false for will end up in the second.
Examples
let numbers = Set.Int.from_list [1; 1; 5; 6; 5; 7; 9; 8] in
let (evens, odds) = Set.partition numbers ~f:Int.is_even in
Set.to_list evens = [6; 8];
Set.to_list odds = [1; 5; 7; 9]Source val fold : ('a , _ ) t -> initial :'b -> f :('b -> 'a -> 'b ) -> 'b Transform a set into a value which is result of running each element in the set through f, where each successive invocation is supplied the return value of the previous.
See Array.fold for a more in-depth explanation.
Examples
Set.fold ~f:( * ) ~initial:1 (Set.Int.from_list [1;2;3;4]) = 24Source val for_each : ('a , _ ) t -> f :('a -> unit) -> unitRuns a function f against each element of the set.
ConvertSource val to_array : ('a , _ ) t -> 'a arrayConverts a set into an Array
Source val to_list : ('a , _ ) t -> 'a listConverts a set into a List .
Construct sets which can hold any data type using the polymorphic compare function.