Page
Library
Module
Module type
Parameter
Class
Class type
Source
Standard.SetA collection of unique values
This functor lets you describe the type of Maps a little more concisely.
A Set can be constructed using one of the specialised functions available in the Set.Int, Set.String or Set.Poly sub-modules.
You can create sets of custom data types which conform to the Comparator.S signature by using empty, singleton, ofList or ofArray.
val empty :
(module Standard__.Core.Comparator.S
with type identity = 'identity
and type t = 'a) ->
('a, 'identity) tA set with nothing in it.
val singleton :
(module Standard__.Core.Comparator.S
with type identity = 'identity
and type t = 'a) ->
'a ->
('a, 'identity) tval ofArray :
(module Standard__.Core.Comparator.S
with type identity = 'identity
and type t = 'a) ->
'a array ->
('a, 'identity) tval ofList :
(module Standard__.Core.Comparator.S
with type identity = 'identity
and type t = 'a) ->
'a list ->
('a, 'identity) tInsert a value into a set.
Examples
Set.add (Set.Int.ofList [1; 2]) 3 |> Set.toList = [1; 2; 3]Set.add (Set.Int.ofList [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.ofList [1; 2]) 2 |> Set.toList = [1] let originalSet = Set.Int.ofList [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.ofList ["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.ofList ["Ant"; "Bat"; "Cat"] in
numbers.Set.?{"Emu"} = falseval length : (_, _) t -> intDetermine the number of elements in a set.
Examples
Set.length (Set.Int.ofList [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.ofList [1; 3; 4; 8]) = Some 4Set.find ~f:Int.isOdd (Set.Int.ofList [0; 2; 4; 8]) = NoneSet.find ~f:Int.isEven Set.Int.empty = Noneval isEmpty : (_, _) t -> boolCheck if a set is empty.
Examples
Set.isEmpty (Set.Int.empty) = trueSet.isEmpty (Set.Int.singleton 4) = falseval any : ('value, _) t -> f:('value -> bool) -> boolDetermine if f returns true for any values in a set.
Examples
Set.any (Set.Int.ofArray [|2;3|]) ~f:Int.isEven = trueSet.any (Set.Int.ofList [1;3]) ~f:Int.isEven = falseSet.any (Set.Int.ofList []) ~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.ofArray [|2;4|]) = trueSet.all ~f:Int.isEven (Set.Int.ofLis [2;3]) = falseSet.all ~f:Int.isEven Set.Int.empty = trueReturns a new set with the values from the first set which are not in the second set.
Examples
Set.difference (Set.Int.ofList [1;2;5]) (Set.Int.ofList [2;3;4]) |> Set.toList = [1;5]Set.difference (Set.Int.ofList [2;3;4]) (Set.Int.ofList [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.ofList [1;2;5]) (Set.Int.ofList [2;3;4]) |> Set.toList= [2]Get the union of two sets. Keep all values.
Examples
Set.union (Set.Int.ofList [1;2;5]) (Set.Int.ofList [2;3;4]) |> Set.toList = [1;2;3;4;5]Keep elements that f returns true for.
Examples
Set.filter (Set.Int.ofList [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.ofList [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.ofList [1;2;3;4]) = 24val forEach : ('a, _) t -> f:('a -> unit) -> unitRuns a function f against each element of the set.
module Poly : sig ... endConstruct sets which can hold any data type using the polymorphic compare function.