Library
Module
Module type
Parameter
Class
Class type
A Set
represents a unique collection of 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 the usage is so common the Set.Int
and Set.String
modules are available, offering a convenient way to construct sets.
For other data types you can use Set.Poly
which uses OCaml's polymorphic compare
function.
A 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) t
A set with nothing in it.
val singleton :
(module Standard__.Core.Comparator.S
with type identity = 'identity
and type t = 'a) ->
'a ->
('a, 'identity) t
val ofArray :
(module Standard__.Core.Comparator.S
with type identity = 'identity
and type t = 'a) ->
'a array ->
('a, 'identity) t
val ofList :
(module Standard__.Core.Comparator.S
with type identity = 'identity
and type t = 'a) ->
'a list ->
('a, 'identity) t
Insert 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 = newSet
val includes : ('a, _) t -> 'a -> bool
Determine if a value is in a set
Examples
Set.includes (Set.String.ofList ["Ant"; "Bat"; "Cat"]) "Bat" = true
val (.?{}) : ('element, _) t -> 'element -> bool
The 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"} = false
val length : (_, _) t -> int
Determine the number of elements in a set.
Examples
Set.length (Set.Int.ofList [1; 2; 3]) = 3
val find : ('value, _) t -> f:('value -> bool) -> 'value option
Returns, 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 4
Set.find ~f:Int.isOdd (Set.Int.ofList [0; 2; 4; 8]) = None
Set.find ~f:Int.isEven Set.Int.empty = None
val isEmpty : (_, _) t -> bool
Check if a set is empty.
Examples
Set.isEmpty (Set.Int.empty) = true
Set.isEmpty (Set.Int.singleton 4) = false
val any : ('value, _) t -> f:('value -> bool) -> bool
Determine if f
returns true for any
values in a set.
Examples
Set.any (Set.Int.ofArray [|2;3|]) ~f:Int.isEven = true
Set.any (Set.Int.ofList [1;3]) ~f:Int.isEven = false
Set.any (Set.Int.ofList []) ~f:Int.isEven = false
val all : ('value, _) t -> f:('value -> bool) -> bool
Determine if f
returns true for all
values in a set.
Examples
Set.all ~f:Int.isEven (Set.Int.ofArray [|2;4|]) = true
Set.all ~f:Int.isEven (Set.Int.ofLis [2;3]) = false
Set.all ~f:Int.isEven Set.Int.empty = true
Returns 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) -> '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.ofList [1;2;3;4]) = 24
val forEach : ('a, _) t -> f:('a -> unit) -> unit
Runs a function f
against each element of the set.
module Poly : sig ... end
Construct sets which can hold any data type using the polymorphic compare
function.