Documentation
¶
Overview ¶
Package iterset is a set library based on maps and iterators.
Example (Difference) ¶
Update a slice by removing common adapted keys.
values := []string{"A", "B", "C"}
keys := []string{"d", "c", "b"}
// With no sets.
m := map[string]bool{}
for _, value := range values {
m[strings.ToLower(value)] = true
}
keys = slices.DeleteFunc(keys, func(key string) bool { return m[key] })
fmt.Println(keys)
// A typical `mapset` would have minimal impact on readability.
s := Set[string]()
for _, value := range values {
s.Add(strings.ToLower(value))
}
keys = slices.DeleteFunc(keys, s.Contains)
fmt.Println(keys)
// Whereas `iterset` can in-line the set construction.
v := IndexBy(slices.Values(values), strings.ToLower)
keys = slices.DeleteFunc(keys, v.Contains)
fmt.Println(keys)
Output: [d] [d] [d]
Example (Intersect) ¶
Intersect a map with a slice of keys, retaining original order.
data := map[string]int{"a": 0, "b": 1, "c": 2}
keys := []string{"d", "c", "b"}
// With no sets.
for _, key := range keys {
value, ok := data[key]
if ok {
fmt.Println(key, value)
}
}
// A typical `mapset` would copy `data`, and have no impact on readability.
s := Set(slices.Collect(maps.Keys(data))...)
for _, key := range keys {
if s.Contains(key) {
fmt.Println(key, data[key])
}
}
// Using an intersect method would also copy `keys`, and lose ordering.
// Whereas `iterset` methods have in-lined logic with zero copying and lazy iteration.
for key, value := range Cast(data).Intersect(slices.Values(keys)) {
fmt.Println(key, value)
}
Output: c 2 b 1 c 2 b 1 c 2 b 1
Example (Superset) ¶
Is one slice a superset of another?
left, right := []string{"a", "b", "c"}, []string{"b", "c", "d"}
// With no sets.
m := map[string]bool{}
for _, c := range left {
m[c] = true
}
isSuperset := true
for _, c := range right {
if !m[c] {
isSuperset = false
break
}
}
fmt.Println(isSuperset)
// Or in functional style.
fmt.Println(!slices.ContainsFunc(right, func(c string) bool { return !m[c] }))
// A typical `mapset` would copy both slices, which makes early exits irrelevant.
// Or it only solves half the problem.
s := Set(left...)
fmt.Println(!slices.ContainsFunc(right, func(c string) bool { return !s.Contains(c) }))
// Whereas `iterset` methods have in-lined logic with minimal copying and early exits.
fmt.Println(Set(left...).IsSuperset(slices.Values(right)))
Output: false false false false
Example (Unique) ¶
Remove duplicates, retaining original order.
values := []string{"a", "b", "a"}
// With no sets.
m := map[string]bool{}
keys := []string{}
for _, c := range values {
if !m[c] {
keys = append(keys, c)
}
m[c] = true
}
fmt.Println(keys)
// A typical `mapset` would either have no impact on readability, or lose ordering.
// Whereas `iterset` has this built-in with lazy iteration.
for c := range Unique(slices.Values(values)) {
fmt.Println(c)
}
// What if a `set` is still needed, in addition to ordering.
idx := Index(slices.Values(values))
fmt.Println(idx)
fmt.Println(Sorted(idx)) // keys sorted by value
Output: [a b] a b map[a:0 b:1] [a b]
Index ¶
- func Compact[K comparable](keys iter.Seq[K]) iter.Seq2[K, int]
- func CompactBy[K comparable, V any](values iter.Seq[V], key func(V) K) iter.Seq2[K, []V]
- func CompareValues[K comparable, V cmp.Ordered](m map[K]V) func(K, K) int
- func Difference[K comparable](keys iter.Seq[K], seqs ...iter.Seq[K]) iter.Seq[K]
- func Equal[K comparable](keys, seq iter.Seq[K]) bool
- func EqualCounts[K comparable](keys, seq iter.Seq[K]) bool
- func GoIter[V any](seq iter.Seq[V], size int) iter.Seq[V]
- func Intersect[K comparable](keys iter.Seq[K], seqs ...iter.Seq[K]) iter.Seq[K]
- func IsDisjoint[K comparable](keys, seq iter.Seq[K]) bool
- func IsEmpty[V any](seq iter.Seq[V]) bool
- func IsSubset[K comparable](keys, seq iter.Seq[K]) bool
- func Keys[K, V any](seq iter.Seq2[K, V]) iter.Seq[K]
- func Max[K any, V cmp.Ordered](seq iter.Seq2[K, V]) []K
- func Min[K any, V cmp.Ordered](seq iter.Seq2[K, V]) []K
- func Size[V any](seq iter.Seq[V]) int
- func Sorted[K comparable, V cmp.Ordered](m map[K]V) []K
- func SortedDifference[K cmp.Ordered](keys, seq iter.Seq[K]) iter.Seq[K]
- func SortedIntersect[K cmp.Ordered](keys, seq iter.Seq[K]) iter.Seq[K]
- func SortedUnion[K cmp.Ordered](keys, seq iter.Seq[K]) iter.Seq[K]
- func Unique[K comparable](keys iter.Seq[K]) iter.Seq[K]
- func UniqueBy[K comparable, V any](values iter.Seq[V], key func(V) K) iter.Seq2[K, V]
- type MapSet
- func Cast[K comparable, V any](m map[K]V) MapSet[K, V]
- func Collect[K comparable, V any](keys iter.Seq[K], value V) MapSet[K, V]
- func Count[K comparable](keys iter.Seq[K]) MapSet[K, int]
- func Group[K comparable, V any](seq iter.Seq2[K, V]) MapSet[K, []V]
- func GroupBy[K comparable, V any](values iter.Seq[V], key func(V) K) MapSet[K, []V]
- func Index[K comparable](keys iter.Seq[K]) MapSet[K, int]
- func IndexBy[K comparable, V any](values iter.Seq[V], key func(V) K) MapSet[K, V]
- func Memoize[K comparable, V any](keys iter.Seq[K], f func(K) V) MapSet[K, V]
- func Reduce[K comparable, V any](seq iter.Seq2[K, V], f func(V, V) V) MapSet[K, V]
- func Set[K comparable](keys ...K) MapSet[K, struct{}]
- func (m MapSet[K, V]) Add(keys ...K)
- func (m MapSet[K, V]) Contains(key K) bool
- func (m MapSet[K, V]) Delete(keys ...K)
- func (m MapSet[K, V]) Difference(keys iter.Seq[K]) iter.Seq2[K, V]
- func (m MapSet[K, V]) Equal(keys iter.Seq[K]) bool
- func (m MapSet[K, V]) Insert(keys iter.Seq[K], value V)
- func (m MapSet[K, V]) Intersect(keys iter.Seq[K]) iter.Seq2[K, V]
- func (m MapSet[K, V]) IsDisjoint(keys iter.Seq[K]) bool
- func (m MapSet[K, V]) IsSubset(keys iter.Seq[K]) bool
- func (m MapSet[K, V]) IsSuperset(keys iter.Seq[K]) bool
- func (m MapSet[K, V]) Missing(key K) bool
- func (m MapSet[K, V]) Overlap(keys iter.Seq[K]) (int, int, int)
- func (m MapSet[K, V]) Remove(keys iter.Seq[K])
- func (m MapSet[K, V]) ReverseDifference(keys iter.Seq[K]) iter.Seq[K]
- func (m MapSet[K, V]) SymmetricDifference(keys iter.Seq[K]) iter.Seq[K]
- func (m MapSet[K, V]) Toggle(keys iter.Seq[K], value V)
- func (m MapSet[K, V]) Union(seqs ...iter.Seq2[K, V]) MapSet[K, V]
Examples ¶
- Package (Difference)
- Package (Intersect)
- Package (Superset)
- Package (Unique)
- Cast
- Collect
- Compact
- CompactBy
- CompareValues
- Count
- Difference
- Equal
- EqualCounts
- GoIter
- Group
- GroupBy
- Index
- IndexBy
- Intersect
- IsDisjoint
- IsEmpty
- IsSubset
- Keys
- MapSet.Add
- MapSet.Contains
- MapSet.Delete
- MapSet.Difference
- MapSet.Equal
- MapSet.Insert
- MapSet.Intersect
- MapSet.IsDisjoint
- MapSet.IsSubset
- MapSet.IsSuperset
- MapSet.Missing
- MapSet.Overlap
- MapSet.Remove
- MapSet.ReverseDifference
- MapSet.SymmetricDifference
- MapSet.Toggle
- MapSet.Union
- Max
- Memoize
- Min
- Reduce
- Set
- Size
- Sorted
- SortedDifference
- SortedIntersect
- SortedUnion
- Unique
- UniqueBy
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compact ¶ added in v0.3.0
Compact returns consecutive runs of deduplicated keys, with counts.
Related:
Example ¶
k := slices.Values([]string{"b", "b", "a", "a", "b"})
for key, count := range Compact(k) {
fmt.Println(key, count)
}
Output: b 2 a 2 b 1
func CompactBy ¶ added in v0.3.0
CompactBy is like Compact but uses a key function and collects all values.
Related:
Example ¶
v := slices.Values([]string{"B", "b", "A", "a", "b"})
for key, values := range CompactBy(v, strings.ToLower) {
fmt.Println(key, values)
}
Output: b [B b] a [A a] b [b]
func CompareValues ¶ added in v0.5.0
func CompareValues[K comparable, V cmp.Ordered](m map[K]V) func(K, K) int
CompareValues returns a function which compares by value.
Related:
- Sorted to sort by value
- slices functions with a custom cmp.Compare
Example ¶
c := CompareValues(Index(slices.Values([]string{"c", "b", "a"})))
fmt.Println(slices.SortedFunc(slices.Values([]string{"a", "b"}), c))
Output: [b a]
func Difference ¶ added in v0.2.0
Difference returns the ordered keys which are not present in the sequence(s).
Related:
- MapSet.ReverseDifference if the sequence was a map
- SortedDifference if sequences are sorted
Performance:
- time: O(k)
- space: O(k)
Example ¶
s1 := slices.Values([]string{"a", "b"})
s2 := slices.Values([]string{"b", "c"})
fmt.Println(slices.Collect(Difference(s1, s2)))
Output: [a]
func Equal ¶ added in v0.4.0
func Equal[K comparable](keys, seq iter.Seq[K]) bool
Equal returns whether the sets of keys are equal.
Related:
- MapSet.Equal if either sequence was a map
Performance:
- time: O(k)
- space: O(k)
Example ¶
k := slices.Values([]string{"b", "a", "b"})
s := slices.Values([]string{"a"})
fmt.Println(Equal(k, slices.Values([]string{"a", "b"})), Equal(k, s), Equal(k, s))
Output: true false false
func EqualCounts ¶ added in v0.4.0
func EqualCounts[K comparable](keys, seq iter.Seq[K]) bool
EqualCounts returns whether the multisets of keys are equal.
Related:
- Equal to ignore counts
- Count and maps.Equal if either sequence were counts
Performance:
- time: O(k)
- space: O(k)
Example ¶
k := slices.Values([]string{"b", "a", "b"})
s := slices.Values([]string{"a", "b"})
fmt.Println(EqualCounts(k, k), EqualCounts(k, s), EqualCounts(s, k))
Output: true false false
func GoIter ¶ added in v0.4.0
GoIter iterates the sequence in a background goroutine and channel. An unbuffered channel (size 0) is sufficient for parallelism, but channels introduce overhead. As always, benchmark first.
Example ¶
s := slices.Values([]string{"a", "b", "c"})
fmt.Println(slices.Collect(GoIter(s, 0)))
Output: [a b c]
func Intersect ¶ added in v0.3.0
Intersect returns the ordered keys which are present in the sequence(s).
Related:
- MapSet.Intersect if the sequence was a map
- SortedIntersect if sequences are sorted
Performance:
- time: O(k)
- space: O(k)
Example ¶
s1 := slices.Values([]string{"a", "b"})
s2 := slices.Values([]string{"d", "c", "b"})
fmt.Println(slices.Collect(Intersect(s1, s2)))
Output: [b]
func IsDisjoint ¶ added in v0.4.0
func IsDisjoint[K comparable](keys, seq iter.Seq[K]) bool
IsDisjoint returns whether no keys are present in the sequence.
Related:
- MapSet.IsDisjoint if the sequence was a map
Performance:
- time: O(k)
- space: O(k)
Example ¶
k := slices.Values([]string{"a"})
s1 := slices.Values([]string{"b"})
s2 := slices.Values([]string{"b", "a"})
fmt.Println(IsDisjoint(k, s1), IsDisjoint(k, s2), IsDisjoint(s2, k))
Output: true false false
func IsEmpty ¶ added in v0.4.0
IsEmpty returns where there are no values in a sequence.
Example ¶
s := slices.Values([]int{})
fmt.Println(IsEmpty(s))
Output: true
func IsSubset ¶ added in v0.2.0
func IsSubset[K comparable](keys, seq iter.Seq[K]) bool
IsSubset returns whether all keys are present in the sequence.
Related:
- MapSet.IsSuperset if the sequence was a map
Performance:
- time: O(k)
- space: O(k)
Example ¶
s1 := slices.Values([]string{"a"})
s2 := slices.Values([]string{"a", "b"})
fmt.Println(IsSubset(s1, s2), IsSubset(s2, s1))
Output: true false
func Keys ¶ added in v0.4.0
Keys returns the keys from a sequence of pairs.
Related:
- maps.Keys for a map
Example ¶
s := slices.All([]string{"a", "b", "c"})
fmt.Println(slices.Collect(Keys(s)))
Output: [0 1 2]
func Max ¶ added in v0.3.0
Max returns the key(s) with the maximum corresponding value. Will be empty only if the sequence is empty.
Related:
- Count to rank by frequency
- slices.MaxFunc with CompareValues
Example ¶
s := Max(maps.All(map[string]int{"a": 2, "b": 2, "c": 1}))
slices.Sort(s)
fmt.Println(s)
Output: [a b]
func Min ¶ added in v0.3.0
Min returns the key(s) with the minimum corresponding value. Will be empty only if the sequence is empty.
Related:
- Count to rank by frequency
- slices.MinFunc with CompareValues
Example ¶
s := Min(maps.All(map[string]int{"a": 2, "b": 1, "c": 1}))
slices.Sort(s)
fmt.Println(s)
Output: [b c]
func Size ¶ added in v0.4.0
Size returns the number of values in a sequence.
Example ¶
s := slices.Values([]int{0, 0, 0})
fmt.Println(Size(s))
Output: 3
func Sorted ¶
func Sorted[K comparable, V cmp.Ordered](m map[K]V) []K
Sorted returns keys ordered by corresponding value.
Related:
- Index to retain original order
- Count to rank by frequency
- slices.SortedFunc with CompareValues
Example ¶
fmt.Println(Sorted(Index(slices.Values([]string{"b", "a", "b"}))))
Output: [b a]
func SortedDifference ¶ added in v0.4.0
SortedDifference returns the difference of sorted keys.
Performance:
- time: O(k)
Example ¶
s1, s2 := slices.Values([]string{"b", "c"}), slices.Values([]string{"a", "b", "d"})
fmt.Println(slices.Collect(SortedDifference(s1, s2)))
Output: [c]
func SortedIntersect ¶ added in v0.4.0
SortedIntersect returns the intersection of sorted keys.
Performance:
- time: O(k)
Example ¶
s1, s2 := slices.Values([]string{"b", "c"}), slices.Values([]string{"a", "b", "d"})
fmt.Println(slices.Collect(SortedIntersect(s1, s2)))
Output: [b]
func SortedUnion ¶ added in v0.4.0
SortedUnion returns the union of sorted keys.
Related:
- Compact to deduplicate
Performance:
- time: O(k)
Example ¶
s1, s2 := slices.Values([]string{"b", "c"}), slices.Values([]string{"a", "b", "d"})
fmt.Println(slices.Collect(SortedUnion(s1, s2)))
Output: [a b b c d]
func Unique ¶
func Unique[K comparable](keys iter.Seq[K]) iter.Seq[K]
Unique returns keys in order without duplicates.
Related:
Performance:
- time: O(k)
- space: O(k)
Example ¶
k := slices.Values([]string{"b", "a", "b"})
fmt.Println(slices.Collect(Unique(k)))
Output: [b a]
func UniqueBy ¶ added in v0.3.0
UniqueBy is like Unique but uses a key function to compare values. For values that compare equal, the first key-value pair is returned.
Related:
Performance:
- time: O(k)
- space: O(k)
Example ¶
v := slices.Values([]string{"B", "a", "b"})
for key, value := range UniqueBy(v, strings.ToLower) {
fmt.Println(key, value)
}
Output: b B a a
Types ¶
type MapSet ¶
type MapSet[K comparable, V any] map[K]V
MapSet is a `map` extended with set methods.
func Cast ¶
func Cast[K comparable, V any](m map[K]V) MapSet[K, V]
Cast returns a zero-copy MapSet. Equivalent to `MapSet[K, V](m)` without having to specify concrete types.
An instantiated type alias would have the same functionality. Methods can also be called as unbound functions: `MapSet[K, V].Method(m, ...)`.
Example ¶
m := map[string]bool{}
Cast(m).Add("a")
fmt.Println(m)
// equivalent to
type aSet = MapSet[string, bool]
aSet(m).Add("a")
aSet.Add(m, "a'")
Output: map[a:false]
func Collect ¶ added in v0.2.0
func Collect[K comparable, V any](keys iter.Seq[K], value V) MapSet[K, V]
Collect returns unique keys with a default value. Equivalent to Set when value is `struct{}{}`.
Related:
- maps.Collect for an iter.Seq2
Example ¶
k := slices.Values([]string{"b", "a", "b"})
fmt.Println(Collect(k, true))
Output: map[a:true b:true]
func Count ¶
func Count[K comparable](keys iter.Seq[K]) MapSet[K, int]
Count returns unique keys with their counts.
Related:
- Compact if the keys are already grouped
Example ¶
fmt.Println(Count(slices.Values([]string{"b", "a", "b"})))
Output: map[a:1 b:2]
func Group ¶ added in v0.5.0
func Group[K comparable, V any](seq iter.Seq2[K, V]) MapSet[K, []V]
Group returns values grouped by keys.
Related:
- GroupBy for key function
Example ¶
seq := func(yield func(string, int) bool) {
_ = yield("a", 1) && yield("b", 2) && yield("a", 3)
}
fmt.Println(Group(seq))
Output: map[a:[1 3] b:[2]]
func GroupBy ¶
func GroupBy[K comparable, V any](values iter.Seq[V], key func(V) K) MapSet[K, []V]
GroupBy returns values grouped by key function.
Related:
Example ¶
v := slices.Values([]string{"B", "a", "b"})
fmt.Println(GroupBy(v, strings.ToLower))
Output: map[a:[a] b:[B b]]
func Index ¶
func Index[K comparable](keys iter.Seq[K]) MapSet[K, int]
Index returns unique keys with their first index position.
Related:
Example ¶
fmt.Println(Index(slices.Values([]string{"b", "a", "b"})))
Output: map[a:1 b:0]
func IndexBy ¶
func IndexBy[K comparable, V any](values iter.Seq[V], key func(V) K) MapSet[K, V]
IndexBy returns values indexed by key function. If there are collisions, the last value remains.
Related:
Example ¶
v := slices.Values([]string{"B", "a", "b"})
fmt.Println(IndexBy(v, strings.ToLower))
Output: map[a:a b:b]
func Memoize ¶ added in v0.2.0
func Memoize[K comparable, V any](keys iter.Seq[K], f func(K) V) MapSet[K, V]
Memoize caches function call.
Example ¶
fmt.Println(Memoize(slices.Values([]string{"b", "a", "b"}), strings.ToUpper))
Output: map[a:A b:B]
func Reduce ¶ added in v0.5.0
func Reduce[K comparable, V any](seq iter.Seq2[K, V], f func(V, V) V) MapSet[K, V]
Reduce combines values grouped by keys with binary function.
Related:
- Group to collect into a slice
Example ¶
seq := func(yield func(string, int) bool) {
_ = yield("a", 1) && yield("b", 2) && yield("a", 3)
}
fmt.Println(Reduce(seq, func(i, j int) int { return i + j }))
Output: map[a:4 b:2]
func Set ¶
func Set[K comparable](keys ...K) MapSet[K, struct{}]
Set returns unique keys with an empty struct value.
Related:
- Collect for an iter.Seq
Example ¶
fmt.Println(Set("b", "a", "b"))
Output: map[a:{} b:{}]
func (MapSet[K, V]) Add ¶
func (m MapSet[K, V]) Add(keys ...K)
Add key(s) with zero value.
Related:
- MapSet.Insert for many keys
Example ¶
s := Set("a", "b")
s.Add("b", "c")
fmt.Println(len(s))
Output: 3
func (MapSet[K, V]) Contains ¶
Contains returns whether the key is present.
Related:
- MapSet.IsSuperset for multiple keys
Example ¶
s := Set("b", "a", "b")
fmt.Println(s.Contains("a"), s.Contains("c"))
Output: true false
func (MapSet[K, V]) Delete ¶
func (m MapSet[K, V]) Delete(keys ...K)
Delete key(s).
Related:
- MapSet.Remove for many keys
Example ¶
s := Set("a", "b")
s.Delete("b", "c")
fmt.Println(len(s))
Output: 1
func (MapSet[K, V]) Difference ¶
Difference returns the key-value pairs which are not present in the keys.
Related:
- MapSet.Remove to modify in-place
- MapSet.ReverseDifference if the keys were a map
- Difference if the receiver was not a map
Performance:
- time: O(m+k)
- space: O(min(m,k))
Example ¶
k := slices.Values([]string{"b", "c"})
fmt.Println(maps.Collect(Set("a", "b").Difference(k)))
Output: map[a:{}]
func (MapSet[K, V]) Equal ¶ added in v0.2.0
Equal returns whether the key sets are equivalent.
Related:
- maps.Equal to compare values
Performance:
- time: O(k)
- space: O(min(m, k))
Example ¶
k := slices.Values([]string{"b", "a", "b"})
fmt.Println(Set("a", "b").Equal(k), Set("a").Equal(k))
Output: true false
func (MapSet[K, V]) Insert ¶ added in v0.2.0
Insert keys with default value.
Related:
- maps.Insert for an iter.Seq2
- maps.Copy for a map
Example ¶
m := MapSet[string, bool]{}
m.Insert(slices.Values([]string{"b", "a", "b"}), true)
fmt.Println(m)
Output: map[a:true b:true]
func (MapSet[K, V]) Intersect ¶
Intersect returns the ordered key-value pairs which are present in both.
Performance:
- time: O(k)
Example ¶
m := MapSet[string, int]{"a": 0, "b": 1}
s := slices.Values([]string{"b", "c"})
for key, value := range m.Intersect(s) {
fmt.Println(key, value)
}
Output: b 1
func (MapSet[K, V]) IsDisjoint ¶
IsDisjoint returns whether no keys are present.
Performance:
- time: O(k)
Example ¶
k := slices.Values([]string{"b", "a", "b"})
fmt.Println(Set("c").IsDisjoint(k), Set("a").IsDisjoint(k))
Output: true false
func (MapSet[K, V]) IsSubset ¶ added in v0.2.0
IsSubset returns whether every map key is present in keys.
Related:
- MapSet.IsSuperset if the keys were a map
- IsSubset if the receiver was not a map
Performance:
- time: O(k)
- space: O(min(m, k))
Example ¶
k := slices.Values([]string{"b", "a", "b"})
fmt.Println(Set("a").IsSubset(k), Set("a", "c").IsSubset(k))
Output: true false
func (MapSet[K, V]) IsSuperset ¶
IsSuperset returns whether all keys are present.
Performance:
- time: O(k)
Example ¶
k := slices.Values([]string{"b", "a", "b"})
fmt.Println(Set("a", "b").IsSuperset(k), Set("a").IsSuperset(k))
Output: true false
func (MapSet[K, V]) Missing ¶
Missing returns whether the key is not present. Negation of MapSet.Contains; useful to pass as a bound method.
Related:
- MapSet.IsDisjoint for multiple keys
Example ¶
s := Set("b", "a", "b")
fmt.Println(s.Missing("a"), s.Missing("c"))
Output: false true
func (MapSet[K, V]) Overlap ¶ added in v0.5.0
Overlap returns the sizes of the intersection and differences: left only, both, right only.
Similarity measures:
- overlap coefficient: both / (min(left, right) + both)
- Jaccard index: both / (left + both + right)
Performance:
- time: Θ(k)
- space: Θ(k)
Example ¶
s, k := Set("a", "b", "c"), []string{"b", "c", "d"}
fmt.Println(s.Overlap(slices.Values(k)))
Output: 1 2 1
func (MapSet[K, V]) Remove ¶ added in v0.3.0
Remove keys.
Related:
- MapSet.Difference to not modify in-place
Example ¶
s := Set("a", "b")
s.Remove(slices.Values([]string{"b", "c"}))
fmt.Println(s)
Output: map[a:{}]
func (MapSet[K, V]) ReverseDifference ¶ added in v0.2.0
ReverseDifference returns the ordered keys which are not present in the map. Also known as the relative complement.
Performance:
- time: O(k)
Example ¶
k := slices.Values([]string{"b", "c"})
fmt.Println(slices.Collect(Set("a", "b").ReverseDifference(k)))
Output: [c]
func (MapSet[K, V]) SymmetricDifference ¶ added in v0.2.0
SymmetricDifference returns keys which are not in both.
Related:
- MapSet.Toggle to modify in-place
Performance:
- time: O(m+k)
- space: O(min(m, k))
Example ¶
k := slices.Values([]string{"b", "c"})
fmt.Println(slices.Collect(Set("a", "b").SymmetricDifference(k)))
Output: [c a]
func (MapSet[K, V]) Toggle ¶ added in v0.3.0
Toggle removes present keys, and inserts missing keys.
Related:
- MapSet.SymmetricDifference to not modify in-place
Example ¶
s := Set("a", "b")
s.Toggle(maps.Keys(Set("b", "c")), struct{}{})
fmt.Println(s)
Output: map[a:{} c:{}]
func (MapSet[K, V]) Union ¶ added in v0.2.0
Union merges all keys with successive inserts.
Related:
- maps.Insert to modify in-place
- SortedUnion for sorted sequences
Performance:
- time: Θ(m+k)
- space: Ω(max(m, k))..O(m+k)
Example ¶
m := map[string]int{"a": 0, "b": 1}
n := map[string]int{"b": 2, "c": 3}
fmt.Println(Cast(m).Union(maps.All(n)))
Output: map[a:0 b:2 c:3]