View Source s2 (s2 v1.0.0)
Here lies s2, a wrapper around the Erlang's sets
module which gives you the {version, 2}
representation without having to worry about it. This library
exists because I was using elp during
Advent of Code and it kept complaining about using {version, 2}
for sets:new/1
and sets:from_list/1
.
The documentation says,
This new representation will become the default in future Erlang/OTP versions.
but it hasn't happened yet so either this library will be useful or it will push Erlang/OTP
to make it the default. Either way, he API surface is more similar to Haskell's
Data.Set
module
because of my own personal preferences. For reference, the {version, 2}
representation is just
a map where all the values are []
.
If you have any suggestions or find an issue please submit an issue on github
Summary
Functions
Given two sets, pair every element in the first set with every element in the second.
Remove an element from the set. It won't fail if the element doesn't exist.
Given two sets, return a new set with elements of the second removed from the first.
Return the empty set.
Keep elements in the set for which the predicate returns true
.
Given a binary operation on elements of the set and an accumulator, an initial accumulator, and a set collect the result of applying the binary operation to each element of the set starting from the initial accumulator.
Construct a set from a list of elements.
Add an element to a set.
Given two sets, take the set intersection of them.
Given a list of sets, take the set intersection of all sets.
Given two sets, determine if their intersection is the empty set.
Returns true
if the intersection is the empty set and false
otherwise.
Given an element and a set, if the element is contained in the set return
true
otherwise return false
.
Given a set, if it is the empty set return true
else return false
.
Given two sets, if every element of the first is contained in the second
and the sets are different return true
otherwise return false
.
Given two sets, if every element of the first is contained in the second
return true
otherwise return false
.
Given a unary function and a set, apply the unary function to each element of the set to form a new set. The input and output set may not be the same size.
partition(Pred, Set) -> {Keep, Discard}
Construct a set with a single element.
Given a set, return the number of elements in the set.
Given a set, return a list with all elements from the set.
Given two sets, return a new set with every element from both sets.
Given a list of sets, return a new set with every element from every set.
Types
-opaque set(Element)
As returned by empty/0
.
Functions
-spec cartesian_product(Set1, Set2) -> Set3 when Set1 :: set(Element1), Set2 :: set(Element2), Set3 :: set({Element1, Element2}).
Given two sets, pair every element in the first set with every element in the second.
1> Product = s2:cartesian_product(s2:from_list([1, 2]), s2:from_list([3, 4])).
#{{1,3} => [],{1,4} => [],{2,3} => [],{2,4} => []}
2> Product =:= s2:from_list([{1, 3}, {1, 4}, {2, 3}, {2, 4}]).
true
Remove an element from the set. It won't fail if the element doesn't exist.
1> Set = s2:from_list([1, 2, 3]).
#{1 => [],2 => [],3 => []}
2> s2:delete(1, Set).
#{2 => [],3 => []}
3> s2:delete(4, Set).
#{1 => [],2 => [],3 => []}
-spec difference(Set1, Set2) -> Set3 when Set1 :: set(Element), Set2 :: set(Element), Set3 :: set(Element).
Given two sets, return a new set with elements of the second removed from the first.
1> s2:difference(s2:from_list([1, 2, 3]), s2:empty()).
#{1 => [],2 => [],3 => []}
2> s2:difference(s2:from_list([1, 2, 3]), s2:from_list([1])).
#{2 => [],3 => []}
-spec empty() -> set(_).
Return the empty set.
1> s2:empty().
#{}
-spec filter(Pred, Set1) -> Set2 when Pred :: fun((Element) -> boolean()), Set1 :: set(Element), Set2 :: set(Element).
Keep elements in the set for which the predicate returns true
.
1> S = s2:from_list([1, 2, 3, 4]).
#{1 => [],2 => [],3 => [],4 => []}
2> s2:filter(fun (X) -> X >= 2 end, S).
#{2 => [], 3 => [], 4 => []}
-spec fold(Function, Acc0, Set) -> Acc1 when Function :: fun((Element, AccIn) -> AccOut), Set :: set(Element), Acc0 :: Acc, Acc1 :: Acc, AccIn :: Acc, AccOut :: Acc.
Given a binary operation on elements of the set and an accumulator, an initial accumulator, and a set collect the result of applying the binary operation to each element of the set starting from the initial accumulator.
1> S = s2:from_list([1, 2, 3, 4]).
#{1 => [],2 => [],3 => [],4 => []}
2> s2:fold(fun (Elem, Acc) -> Elem + Acc end, 0, S).
10
-spec from_list(List) -> Set when List :: [Element], Set :: set(Element).
Construct a set from a list of elements.
1> s2:from_list([1, 2, 3]).
#{1 => [], 2 => [], 3 => []}
Add an element to a set.
1> s2:insert(1, s2:singleton(2)).
#{1 => [],2 => []}
-spec intersection(Set1, Set2) -> Set3 when Set1 :: set(Element), Set2 :: set(Element), Set3 :: set(Element).
Given two sets, take the set intersection of them.
1> s2:intersection(s2:singleton(1), s2:singleton(1)).
#{1 => []}
2> s2:intersection(s2:singleton(1), s2:singleton(2)).
#{}
Given a list of sets, take the set intersection of all sets.
1> s2:intersections([s2:singleton(1), s2:singleton(1)]).
#{1 => []}
2> s2:intersections([s2:singleton(1), s2:singleton(2)]).
#{}
Given two sets, determine if their intersection is the empty set.
Returns true
if the intersection is the empty set and false
otherwise.
1> s2:is_disjoint(s2:singleton(1), s2:singleton(2)).
true
2> s2:is_disjoint(s2:from_list([1, 2]), s2:singleton(2)).
false
Given an element and a set, if the element is contained in the set return
true
otherwise return false
.
1> s2:is_element(1, s2:singleton(1)).
true
2> s2:is_element(1, s2:singleton(2)).
false
Given a set, if it is the empty set return true
else return false
.
1> s2:is_empty(s2:empty()).
true
2> s2:is_empty(s2:singleton(1)).
false
Given two sets, if every element of the first is contained in the second
and the sets are different return true
otherwise return false
.
1> s2:is_proper_subset(s2:singleton(1), s2:singleton(1)).
true
2> s2:is_proper_subset(s2:from_list([1, 2]), s2:from_list([1, 2])).
false
3> s2:is_proper_subset(s2:from_list([1, 2]), s2:from_list([1, 2, 3])).
true
Given two sets, if every element of the first is contained in the second
return true
otherwise return false
.
1> s2:is_subset(s2:singleton(1), s2:singleton(1)).
true
2> s2:is_subset(s2:from_list([1, 2]), s2:from_list([1, 2])).
true
3> s2:is_subset(s2:from_list([1, 2]), s2:from_list([1, 2, 3])).
true
-spec map(Fun, Set1) -> Set2 when Fun :: fun((Element1) -> Element2), Set1 :: set(Element1), Set2 :: set(Element2).
Given a unary function and a set, apply the unary function to each element of the set to form a new set. The input and output set may not be the same size.
1> s2:map(fun (X) -> X + 1 end, s2:from_list([1, 2, 3])).
#{2 => [],3 => [],4 => []}
2> s2:map(fun (_X) -> 1 end, s2:from_list([1, 2, 3])).
#{1 => []}
-spec partition(Pred, Set1) -> {Set2, Set2} when Pred :: fun((Element) -> boolean()), Set1 :: set(Element), Set2 :: set(Element).
partition(Pred, Set) -> {Keep, Discard}
Similar to filter/2
but it returns a Tuple
of two elements. The first
contains elements where the predicate evaluates to true
. The second contains
elements where the predicate evaluates to false
.
-spec singleton(Elem) -> Set when Elem :: Element, Set :: set(Element).
Construct a set with a single element.
1> s2:singleton(1).
#{1 => []}
-spec size(Set) -> non_neg_integer() when Set :: set(_).
Given a set, return the number of elements in the set.
1> s2:size(s2:empty()).
0
2> s2:size(s2:from_list([1, 2])).
2
-spec to_list(Set) -> List when Set :: set(Element), List :: [Element].
Given a set, return a list with all elements from the set.
1> s2:to_list(s2:empty()).
[]
2> s2:to_list(s2:singleton(1)).
[1]
3> s2:to_list(s2:from_list([1, 2, 3])).
[1,2,3]
-spec union(Set1, Set2) -> Set3 when Set1 :: set(Element), Set2 :: set(Element), Set3 :: set(Element).
Given two sets, return a new set with every element from both sets.
1> s2:union(s2:singleton(1), s2:singleton(2)).
#{1 => [],2 => []}
Given a list of sets, return a new set with every element from every set.
1> s2:unions([s2:singleton(1), s2:singleton(2), s2:singleton(3)]).
#{1 => [],2 => [],3 => []}