REPOSITORY / ScuroNeko

extypes

Project code, history and documentation.
ACTIVE PUBLIC SOURCE
2026-02-04 12:43:08 +03:00
2026-09-14 13:52:43 +03:00
2026-09-14 13:52:43 +03:00
2026-09-14 13:52:43 +03:00
2026-09-14 13:52:43 +03:00
2026-09-14 13:52:43 +03:00
2026-09-14 13:52:43 +03:00
2026-09-14 13:52:43 +03:00
2026-09-14 13:52:43 +03:00
2026-09-14 13:52:43 +03:00
2026-09-14 13:52:43 +03:00
2026-09-14 13:52:43 +03:00
2026-09-14 13:52:43 +03:00
2026-09-14 13:52:43 +03:00
2026-09-14 13:52:43 +03:00

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 as Push, Pop, Remove, Filter, Map
  • Tuple[T]: a read-oriented ordered collection with JSON support
  • Set[T]: a uniqueness-enforcing collection backed by a slice
  • HashMap[K, V]: a generic map wrapper with helper methods
  • Queue[T]: a fixed-size, mutex-protected FIFO queue

Requirements

  • Go 1.27+

Installation

go get git.scuroneko.dev/scuroneko/extypes/v2

Import

import "git.scuroneko.dev/scuroneko/extypes/v2"

Design Notes

  • The package stays close to Go built-ins instead of introducing a large abstraction layer.
  • Slice, Set, and HashMap methods often return a collection for chaining.
  • Slice.Map, Set.Map, and HashMap.Map return new collections and may change the value type. They do not modify their receivers.
  • Some methods mutate the underlying storage in place: Slice.Swap, Set.Swap, HashMap.Add, and HashMap.Remove.
  • Tuple is read-oriented, but NewTupleFrom wraps the provided slice without copying it.
  • Set preserves first-seen order. Its Equal method is order-sensitive.
  • Set.Map removes duplicate mapping results while preserving their first-seen order.
  • HashMap iteration order is unspecified, so Keys, Values, Items, IndexKey, and IndexValue should not be used where stable ordering is required.
  • Equality and search for Slice, Tuple, Set, and HashMap.IndexValue use reflect.DeepEqual, so element types do not need to be comparable.

Quick Examples

Slice

nums := extypes.NewSliceFrom([]int{1, 2, 3})

nums = nums.Push(4)
nums = nums.Remove(2)
labels := nums.Map(func(v int) string {
	return fmt.Sprintf("item-%d", v*10)
})

fmt.Println(labels.ToArray()) // [item-10 item-30 item-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.NewQueue[string](2)

_ = q.Enqueue("first")
_ = q.Enqueue("second")

head, _ := q.Peek()
fmt.Println(head) // first

item, _ := q.Dequeue()
fmt.Println(item) // first

API Overview

Slice[T]

Useful methods:

  • Len, Cap, Get, At, First, Last, FirstOK, LastOK
  • Index, Equal
  • Push, Pop, Remove, Swap
  • Filter, ForEach, Map, Join
  • ToArray, ToAnyArray, ToSet, ToTuple
  • All

Tuple[T]

Useful methods:

  • Len, Cap, Get, At, FirstOK, LastOK
  • Equal
  • Filter, ForEach, Join
  • ToArray, ToAnyArray, ToSet, ToSlice
  • MarshalJSON, UnmarshalJSON
  • All

Set[T]

Useful methods:

  • Len, Cap, Get, At, First, Last, FirstOK, LastOK
  • Index, Equal
  • Add, Pop, Remove, Swap
  • Filter, ForEach, Map, Join
  • ToArray, ToAnyArray, ToSlice, ToTuple
  • All

HashMap[K, V]

Useful methods:

  • Len, Get, GetOrDefault, Contains
  • Add, Remove
  • Keys, Values, Items
  • IndexKey, IndexValue
  • Filter, ForEach, Map
  • All

Queue[T]

Useful methods:

  • NewQueue
  • Len, Size, IsEmpty, IsFull
  • Enqueue, Dequeue, Peek
  • Raw

Returned errors:

  • QueueFullErr
  • QueueEmptyErr

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.scuroneko.dev/scuroneko/extypes/v2

See CHANGELOG.md for v2 migration notes.

S
Description
No description provided
Readme
86 KiB
0 Stars 1 Watchers 0 Forks
1.2.0
Latest
2026-03-17 14:37:35 +03:00
Languages
Go 100%