diff --git a/map.go b/map.go new file mode 100644 index 0000000..0509887 --- /dev/null +++ b/map.go @@ -0,0 +1,46 @@ +package laniakea + +type HashMap[K comparable, V any] map[K]V + +func NewMapFrom[K comparable, V any](m map[K]V) HashMap[K, V] { + out := make(HashMap[K, V]) + for k, v := range m { + out[k] = v + } + return out +} +func (m HashMap[K, V]) Len() int { + return len(m) +} +func (m HashMap[K, V]) Add(key K, val V) HashMap[K, V] { + m[key] = val + return m +} +func (m HashMap[K, V]) Remove(key K) HashMap[K, V] { + delete(m, key) + return m +} +func (m HashMap[K, V]) Get(key K) V { + return m[key] +} +func (m HashMap[K, V]) GetOrDefault(key K, def V) V { + v, ok := m[key] + if !ok { + return def + } + return v +} +func (m HashMap[K, V]) Contains(key K) bool { + _, ok := m[key] + return ok +} +func (m HashMap[K, V]) Filter(f func(K, V) bool) HashMap[K, V] { + out := make(HashMap[K, V]) + for k, v := range m { + if !f(k, v) { + continue + } + out[k] = v + } + return out +} diff --git a/slice.go b/slice.go index 219b0a2..2d62079 100644 --- a/slice.go +++ b/slice.go @@ -6,7 +6,7 @@ type Slice[T comparable] []T func NewSliceFrom[T comparable](slice []T) Slice[T] { s := make(Slice[T], len(slice)) - copy(s[:], slice) + copy(s, slice) return s } func (s Slice[T]) Len() int { @@ -21,6 +21,11 @@ func (s Slice[T]) Get(index int) T { func (s Slice[T]) Last() T { return s.Get(s.Len() - 1) } +func (s Slice[T]) Index(el T) int { + return slices.Index(s, el) +} + +// Swap is mutable func func (s Slice[T]) Swap(i, j int) Slice[T] { s[i], s[j] = s[j], s[i] return s @@ -55,7 +60,7 @@ func (s Slice[T]) Pop(index int) Slice[T] { return out } func (s Slice[T]) Remove(el T) Slice[T] { - index := slices.Index(s, el) + index := s.Index(el) if index == -1 { return s } @@ -78,12 +83,20 @@ func (s Slice[T]) ToAnyArray() []any { return out } +// ForEach is mutable func +func (s Slice[T]) ForEach(f func(e T) T) Slice[T] { + for i, v := range s { + s[i] = f(v) + } + return s +} + type Set[T comparable] []T func NewSetFrom[T comparable](slice []T) Set[T] { s := make(Set[T], 0) for _, v := range slice { - if slices.Index(slice, v) >= 0 { + if s.Index(v) >= 0 { continue } s = append(s, v) @@ -102,11 +115,14 @@ func (s Set[T]) Get(index int) T { func (s Set[T]) Last() T { return s[s.Len()-1] } +func (s Set[T]) Index(el T) int { + return slices.Index(s, el) +} func (s Set[T]) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s Set[T]) Add(v T) Set[T] { - index := slices.Index(s, v) + index := s.Index(v) if index >= 0 { return s } @@ -126,7 +142,7 @@ func (s Set[T]) Pop(index int) Set[T] { return out } func (s Set[T]) Remove(el T) Set[T] { - index := slices.Index(s, el) + index := s.Index(el) if index == -1 { return s } diff --git a/version.go b/version.go index a98aaa7..2661f98 100644 --- a/version.go +++ b/version.go @@ -1,8 +1,8 @@ package laniakea const ( - VersionString = "0.3.8" + VersionString = "0.3.9" VersionMajor = 0 VersionMinor = 3 - VersionPatch = 8 + VersionPatch = 9 )