novah.set
Functions to work with persistent sets.
Literal sets can be created using #{}
.
Types
Declarations
new
visibility: public
type: {{ Equals a }} -> Option (a -> Int64) -> Set a
Creates a new set which uses the element equality function and the provided hash function, or the default object hash function.
This function should always be used to create new sets for types that have equality implementations that differ from its own
equals
function.
Note that Novah creates default, sensible equals
and hashCode
implementations for every type, so this function is only needed
for types where the equals
function and the Equals
type class implementation diverge.
add
visibility: public
type: a -> Set a -> Set a
Inserts this element in the set. Runtime: ~O(1)
remove
visibility: public
type: a -> Set a -> Set a
Removes this element from the set. Runtime: ~O(1)
size
visibility: public
type: Set a -> Int32
Returns the size of this set. Runtime: O(1)
isEmpty
visibility: public
type: Set a -> Boolean
Returns true if this set is empty. Runtime: O(1)
isNotEmpty
visibility: public
type: Set a -> Boolean
Returns true if this set is not empty. Runtime: O(1)
nth
visibility: public
type: Int32 -> Set a -> Option a
Returns some value at index for this set if it exists or none. Runtime: ~O(1)
randNth
visibility: public
type: Set a -> Option a
Returns a random element of this set if the set is not empty. Runtime: O(1)
union
visibility: public
type: Set a -> Set a -> Set a
Returns the union of these 2 sets.
This is the same as set1 ++ set2
.
difference
visibility: public
type: Set a -> Set a -> Set a
Returns all elements in set 1 that are not in set 2.
intersection
visibility: public
type: Set a -> Set a -> Set a
Returns the intersection between these 2 sets.
stream
visibility: public
type: Set a -> Stream a
Returns a stream for this set.
fromStream
visibility: public
type: Stream a -> Set a
Collects this stream into a set. Runtime: O(n)
forEach
visibility: public
type: Set a -> (a -> Unit) -> Unit
Runs function f for every element of this set, ignoring the result. Runtime: O(n)
forEachIndexed
visibility: public
type: Set a -> (Int32 -> a -> Unit) -> Unit
Runs function f for every index and element of this set, ignoring the result. Runtime: O(n)
map
visibility: public
type: (a -> b) -> Set a -> Set b
Maps function f over all elements of this set returning a set of the same size. Runtime: O(n)
filter
visibility: public
type: (a -> Boolean) -> Set a -> Set a
Returns an array of the elements that match the given predicate. Runtime: O(n)
find
visibility: public
type: (a -> Boolean) -> Set a -> Option a
Returns the first occurrence of elem in the set for which the predicate returns true. This function uses the set's equality function, set at creation time. Runtime: O(n)
foldl
visibility: public
type: (b -> a -> b) -> b -> Set a -> b
Reduces this set to a single value according to the reducing function. Goes from the left to the right. Runtime: O(n)
reduce
visibility: public
type: (a -> a -> a) -> Set a -> Option a
A specialized version of foldl
where the first element of the set is used as initial value.
Returns none if the set is empty
Runtime: O(n)
flatten
visibility: public
type: Set (Set a) -> Set a
Flattens a set of sets into a single set by concatenating them together. Runtime: O(n)
flatMap
visibility: public
type: (a -> Set b) -> Set a -> Set b
Like map, but the mapping function should return a set. Flattens the result. Runtime: O(n)
all
visibility: public
type: (a -> Boolean) -> Set a -> Boolean
Returns true if the predicate is true for all elements in this set. Runtime: O(n)
any
visibility: public
type: (a -> Boolean) -> Set a -> Boolean
Returns true if the predicate is true for any element in this set. Runtime: O(n)
count
visibility: public
type: (a -> Boolean) -> Set a -> Int32
Returns the number of elements matching the given predicate. Runtime: O(n)
min
visibility: public
type: {{ NumberOps a }} -> Set a -> Option a
Returns the smallest number in the set, if the list is not empty. Runtime: O(n)
minOr
visibility: public
type: {{ NumberOps a }} -> a -> Set a -> a
Returns the smallest number in the set, or the default value. Runtime: O(n)
minBy
visibility: public
type: {{ Ord b }} -> (a -> b) -> Set a -> Option a
Returns the smallest element in the set according to the given function, if the set is not empty. Runtime: O(n)
minOf
visibility: public
type: {{ NumberOps b }} -> (a -> b) -> Set a -> Option b
Returns the smallest element returned by applying the function to every element in the set, if the set is not empty. Runtime: O(n)
max
visibility: public
type: {{ NumberOps a }} -> Set a -> Option a
Returns the biggest number in the set, if the list is not empty. Runtime: O(n)
maxOr
visibility: public
type: {{ NumberOps a }} -> a -> Set a -> a
Returns the biggest number in the set, or the default value. Runtime: O(n)
maxBy
visibility: public
type: {{ Ord b }} -> (a -> b) -> Set a -> Option a
Returns the biggest element in the set according to the given function, if the set is not empty. Runtime: O(n)
maxOf
visibility: public
type: {{ NumberOps b }} -> (a -> b) -> Set a -> Option b
Returns the biggest element returned by applying the function to every element in the set, if the set is not empty. Runtime: O(n)
sum
visibility: public
type: {{ Plus a }} -> Set a -> a
Returns the sum of all elements in this set. Runtime: O(n)
sumOf
visibility: public
type: {{ Plus b }} -> (a -> b) -> Set a -> b
Returns the sum of all elements by first applying the function to every element in this set. Runtime: O(n)
product
visibility: public
type: {{ Mult a }} -> Set a -> a
Returns the product of all elements in this set. Runtime: O(n)
productOf
visibility: public
type: {{ Mult b }} -> (a -> b) -> Set a -> b
Returns the product of all elements by first applying the function to every element in this set. Runtime: O(n)
containsAll
visibility: public
type: List a -> Set a -> Boolean
Returns true if every element in the list is contained in the set. Runtime: O(n)
groupBy
visibility: public
type: (a -> b) -> Set a -> Map b (Set a)
Groups the elements of the set according to the the function. Ex:
groupBy isOdd #{1, 2, 3, 4, 5, 6}
// => { true: #{1, 3, 5}, false: #{2, 4, 6} }
fromList
visibility: public
type: List a -> Set a
Returns a set with the same elements as the list, with duplicates removed. Runtime: O(n)
toList
visibility: public
type: Set a -> List a
Returns a list of all elements in this set. Runtime: O(n)
withLinear
visibility: public
type: Set a -> (Set a -> b) -> Set a
Creates a linear (mutable) version of the given set and applies the function to it.
Returns a forked (persistent) version of the returned linear set.
This function should be used when lots of expensive mutating operations have to be run on a set
and can be seen as a builder for the resulting set.
All functions that alter sets like add
, remove
, etc.
will mutate a linear set.
Ex:
withLinear #{1, 2, 3} \set ->
add 4 set
remove 1 set
-> #{2, 3, 4}