b1dd8d55fc962f64c311d462b2081473fd292a48
extypes
extypes is a small Go package with generic collection helpers built on top of native slices and maps.
It provides:
Slice[T]: slice helpers such asPush,Pop,Remove,Filter,MapTuple[T]: a read-oriented ordered collection with JSON supportSet[T]: a uniqueness-enforcing collection backed by a sliceHashMap[K, V]: a generic map wrapper with helper methodsQueue[T]: a fixed-size, mutex-protected FIFO queue
Requirements
- Go
1.25+
Installation
go get git.nix13.pw/scuroneko/extypes
Import
import "git.nix13.pw/scuroneko/extypes"
Design Notes
- The package stays close to Go built-ins instead of introducing a large abstraction layer.
Slice,Set, andHashMapmethods often return the receiver for chaining.- Some methods mutate the underlying storage in place:
Slice.Map,Slice.Swap,Set.Map,Set.Swap,HashMap.Add,HashMap.Remove,HashMap.Map. Tupleis read-oriented, butNewTupleFromwraps the provided slice without copying it.Setpreserves first-seen order. ItsEqualmethod is order-sensitive.Set.Mapdoes not restore uniqueness if the mapping function produces duplicates.HashMapiteration order is unspecified, soKeys,Values,Items,IndexKey, andIndexValueshould not be used where stable ordering is required.- Equality and search for
Slice,Tuple,Set, andHashMap.IndexValueusereflect.DeepEqual, so element types do not need to becomparable.
Quick Examples
Slice
nums := extypes.NewSliceFrom([]int{1, 2, 3})
nums = nums.Push(4)
nums = nums.Remove(2)
nums = nums.Map(func(v int) int { return v * 10 })
fmt.Println(nums.ToArray()) // [10 30 40]
Tuple
tuple := extypes.NewTupleFrom([]string{"api", "worker", "cron"})
filtered := tuple.Filter(func(v string) bool {
return v != "worker"
})
fmt.Println(filtered.Join(", ")) // api, cron
Set
set := extypes.NewSetFrom([]string{"go", "go", "docs", "tests"})
set = set.Add("bench")
set = set.Remove("docs")
fmt.Println(set.ToArray()) // [go tests bench]
HashMap
ports := extypes.NewMapFrom(map[string]int{
"http": 80,
"https": 443,
})
ports.Add("ssh", 22)
secure := ports.Filter(func(name string, port int) bool {
return port >= 100 || name == "https"
})
fmt.Println(secure.Contains("https")) // true
Queue
q := extypes.CreateQueue[string](2)
_ = q.Enqueue("first")
_ = q.Enqueue("second")
head, _ := q.Peak()
fmt.Println(head) // first
item, _ := q.Dequeue()
fmt.Println(item) // first
API Overview
Slice[T]
Useful methods:
Len,Cap,Get,First,LastIndex,EqualPush,Pop,Remove,SwapFilter,ForEach,Map,JoinToArray,ToAnyArray,ToSet,ToTuple
Tuple[T]
Useful methods:
Len,Cap,GetEqualFilter,ForEach,JoinToArray,ToAnyArray,ToSet,ToSliceMarshalJSON,UnmarshalJSON
Set[T]
Useful methods:
Len,Cap,Get,First,LastIndex,EqualAdd,Pop,Remove,SwapFilter,ForEach,Map,JoinToArray,ToAnyArray,ToSlice,ToTuple
HashMap[K, V]
Useful methods:
Len,Get,GetOrDefault,ContainsAdd,RemoveKeys,Values,ItemsIndexKey,IndexValueFilter,ForEach,Map
Queue[T]
Useful methods:
CreateQueueLen,Size,IsEmpty,IsFullEnqueue,Dequeue,PeakRaw
Returned errors:
QueueFullErrQueueEmptyErr
Running Tests
go test ./...
Godoc
The package includes doc comments for the public API, so local documentation can be viewed with standard Go tooling:
go doc
go doc git.nix13.pw/scuroneko/extypes
Description