2 Commits

Author SHA1 Message Date
948ac0a1b0 non comparable slices 2026-02-06 13:18:11 +03:00
7d930b6e97 fix Pop method 2026-02-04 12:56:22 +03:00

View File

@@ -1,10 +1,13 @@
package extypes
import "slices"
import (
"reflect"
"slices"
)
type Slice[T comparable] []T
type Slice[T any] []T
func NewSliceFrom[T comparable](slice []T) Slice[T] {
func NewSliceFrom[T any](slice []T) Slice[T] {
s := make(Slice[T], len(slice))
copy(s, slice)
return s
@@ -22,7 +25,18 @@ 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)
for i := range s {
//t1 := reflect.TypeOf(el)
//t2 := reflect.TypeOf(s[i])
//if t1 != t2 {
// continue
//}
if reflect.DeepEqual(s[i], el) {
return i
}
}
return -1
//return slices.Index(s, el)
}
// Swap is mutable func
@@ -50,12 +64,12 @@ func (s Slice[T]) Pop(index int) Slice[T] {
if index == 0 {
return s[1:]
}
out := make(Slice[T], s.Len()-index)
out := make(Slice[T], 0, s.Len()-1)
for i, e := range s {
if i == index {
continue
}
out[i] = e
out = append(out, e)
}
return out
}
@@ -132,12 +146,12 @@ func (s Set[T]) Pop(index int) Set[T] {
if index == 0 {
return s[1:]
}
out := make(Set[T], s.Len()-index)
out := make(Set[T], 0, s.Len()-1)
for i, e := range s {
if i == index {
continue
}
out[i] = e
out = append(out, e)
}
return out
}