package tablecloth-native
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=0c71dd4035d6fa4978baabc3c932dba3
sha512=44ba09f1ff43e61403703cc244e91e0f8062bd9da998f031430d701a4de148b02878f7f881303f6ded261176f21926ab5ba00a313510ed8e2d2f252b3fd00054
doc/tablecloth-native/Tablecloth/Set/index.html
Module Tablecloth.Set
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 Int.compare Int.compare
include Comparator.Make(struct
type nonrec t = t
let compare = compare
end)
end
let points : Set.Of(Point).t = Set.fromList (module Points) [(0, 0); (3, 4); (6, 7)]See the Comparator module for a more details.
type ('a, 'id) t = ('a, 'id) Base.Set.tThis functor lets you describe the type of Sets a little more concisely.
Create
You can create a Set by providing a module conform to the Comparator.S signature by using empty, singleton, fromList or fromArray.
Specialised versions of the empty, singleton, fromList and fromArray functions available in the Set.Int and Set.String sub-modules.
val empty :
(module Tablecloth__.TableclothComparator.S
with type identity = 'identity
and type t = 'a) ->
('a, 'identity) tA 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'|]
~intial:(Set.empty (module Char))
~f:Set.add
|> Set.toArray
= [|'i'; 'm'; 'p'; 's'|] val singleton :
(module Tablecloth__.TableclothComparator.S
with type identity = 'identity
and type t = 'a) ->
'a ->
('a, 'identity) tval fromArray :
(module Tablecloth__.TableclothComparator.S
with type identity = 'identity
and type t = 'a) ->
'a array ->
('a, 'identity) tCreate a set from an Array
Examples
Set.fromArray (module String) [|"Ant"; "Bat"; "Bat"; "Goldfish"|] |> Set.toArray = [|"Ant";"Bat";"Goldfish"|]val from_array :
(module Tablecloth__.TableclothComparator.S
with type identity = 'identity
and type t = 'a) ->
'a array ->
('a, 'identity) tval fromList :
(module Tablecloth__.TableclothComparator.S
with type identity = 'identity
and type t = 'a) ->
'a list ->
('a, 'identity) tCreate a set from a List
Examples
Set.fromList (module Char) ['A'; 'B'; 'B'; 'G'] |> Set.toList = ['A';'B';'G']val from_list :
(module Tablecloth__.TableclothComparator.S
with type identity = 'identity
and type t = 'a) ->
'a list ->
('a, 'identity) tBasic operations
Insert a value into a set.
Examples
Set.add (Set.Int.fromList [1; 2]) 3 |> Set.toList = [1; 2; 3]Set.add (Set.Int.fromList [1; 2]) 2 |> Set.toList = [1; 2]Remove a value from a set, if the set doesn't contain the value anyway, returns the original set
Examples
Set.remove (Set.Int.fromList [1; 2]) 2 |> Set.toList = [1] let originalSet = Set.Int.fromList [1; 2] in
let newSet = Set.remove orignalSet 3 in
originalSet = newSetval includes : ('a, _) t -> 'a -> boolDetermine if a value is in a set
Examples
Set.includes (Set.String.fromList ["Ant"; "Bat"; "Cat"]) "Bat" = trueval (.?{}) : ('element, _) t -> 'element -> boolThe index operator version of includes
Note Currently this is only supported by the OCaml syntax.
Examples
let animals = Set.String.fromList ["Ant"; "Bat"; "Cat"] in
animals.Set.?{"Emu"} = falseval length : (_, _) t -> intDetermine the number of elements in a set.
Examples
Set.length (Set.Int.fromList [1; 2; 3]) = 3val 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.isEven (Set.Int.fromList [1; 3; 4; 8]) = Some 4Set.find ~f:Int.isOdd (Set.Int.fromList [0; 2; 4; 8]) = NoneSet.find ~f:Int.isEven Set.Int.empty = NoneQuery
val isEmpty : (_, _) t -> boolCheck if a set is empty.
Examples
Set.isEmpty (Set.Int.empty) = trueSet.isEmpty (Set.Int.singleton 4) = falseval is_empty : (_, _) t -> boolval any : ('value, _) t -> f:('value -> bool) -> boolDetermine if f returns true for any values in a set.
Examples
Set.any (Set.Int.fromArray [|2;3|]) ~f:Int.isEven = trueSet.any (Set.Int.fromList [1;3]) ~f:Int.isEven = falseSet.any (Set.Int.fromList []) ~f:Int.isEven = falseval all : ('value, _) t -> f:('value -> bool) -> boolDetermine if f returns true for all values in a set.
Examples
Set.all ~f:Int.isEven (Set.Int.fromArray [|2;4|]) = trueSet.all ~f:Int.isEven (Set.Int.fromLis [2;3]) = falseSet.all ~f:Int.isEven Set.Int.empty = trueCombine
Returns a new set with the values from the first set which are not in the second set.
Examples
Set.difference (Set.Int.fromList [1;2;5]) (Set.Int.fromList [2;3;4]) |> Set.toList = [1;5]Set.difference (Set.Int.fromList [2;3;4]) (Set.Int.fromList [1;2;5]) |> Set.toList = [3;4]Get the intersection of two sets. Keeps values that appear in both sets.
Examples
Set.intersection (Set.Int.fromList [1;2;5]) (Set.Int.fromList [2;3;4]) |> Set.toList= [2]Get the union of two sets. Keep all values.
Examples
Set.union (Set.Int.fromList [1;2;5]) (Set.Int.fromList [2;3;4]) |> Set.toList = [1;2;3;4;5]Transform
Keep elements that f returns true for.
Examples
Set.filter (Set.Int.fromList [1;2;3]) ~f:Int.isEven |> Set.toList = [2]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.fromList [1; 1; 5; 6; 5; 7; 9; 8] in
let (evens, odds) = Set.partition numbers ~f:Int.isEven in
Set.toList evens = [6; 8]
Set.toList odds = [1; 5; 7; 9]val fold : ('a, _) t -> initial:'b -> f:('b -> 'a -> 'b) -> 'bTransform 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.fromList [1;2;3;4]) = 24val forEach : ('a, _) t -> f:('a -> unit) -> unitRuns a function f against each element of the set.
val for_each : ('a, _) t -> f:('a -> unit) -> unitConvert
val to_array : ('a, _) t -> 'a arrayval to_list : ('a, _) t -> 'a listmodule Poly : sig ... endConstruct sets which can hold any data type using the polymorphic compare function.