Golang set operations using maps and iterators.
There are many mapset
implementations available, but they restrict the values to struct{}
or bool
. This means slices, maps, and iterators have to be converted to sets. Which besides being inefficient, loses slice ordering and map values. Additionally since sets are not built-in, they realistically will always be secondary types. Even in languages with built-in sets, it is common to call set operations on keys while still retaining data in a map.
So iterset
is built around generic maps with any
value type. Maps can be casted without copying, and the methods support set operations which integrate with functions in maps and slices. Additionally the methods support iterators for further efficiency. Inspired by Python sets, which allow iterables in methods.
There are constructors for all common use cases.
Cast
a mapUnique
iterates keys in orderCollect
with default valueSet
from variadic argsIndex
retains original positionCount
stores key countsIndexBy
stores values by key functionGroupBy
stores slices grouped by key functionMemoize
caches function call
Methods support iterators, compatible with slices.Values
and maps.Keys
.
Equal
IsSubset
IsSuperset
IsDisjoint
Union
Intersect
Difference
ReverseDifference
SymmetricDifference
Scalar operations can be passed as bound methods for functional programming.
Get
Contains
Missing
Add
Delete
No dependencies. Go 1.23 required.
go get github.com/coady/iterset
go test .