many changes

This commit is contained in:
2026-02-04 13:38:36 +03:00
parent 2db7d2a813
commit 62d91dfe07
9 changed files with 43 additions and 94 deletions

View File

@@ -22,69 +22,6 @@ func Max(a, b int) int {
return b
}
type Slice[T any] []T
func NewSliceFrom[T any](slice []T) Slice[T] {
s := make(Slice[T], len(slice))
copy(s[:], slice)
return s
}
func (s Slice[T]) Len() int {
return len(s)
}
func (s Slice[T]) Cap() int {
return cap(s)
}
func (s Slice[T]) Swap(i, j int) Slice[T] {
s[i], s[j] = s[j], s[i]
return s
}
func (s Slice[T]) Filter(f func(e T) bool) Slice[T] {
out := make(Slice[T], 0)
for _, v := range s {
if f(v) {
out = append(out, v)
}
}
return out
}
func (s Slice[T]) Map(f func(e T) T) Slice[T] {
out := make(Slice[T], s.Len())
for i, v := range s {
out[i] = f(v)
}
return out
}
func (s Slice[T]) Pop(index int) Slice[T] {
if index == 0 {
return s[1:]
}
out := make(Slice[T], s.Len()-index)
for i, e := range s {
if i == index {
continue
}
out[i] = e
}
return out
}
func (s Slice[T]) Push(e T) Slice[T] {
return append(s, e)
}
func (s Slice[T]) ToArray() []T {
out := make([]T, len(s))
copy(out, s)
return out
}
func (s Slice[T]) ToAnyArray() []any {
out := make([]any, len(s))
for i, v := range s {
out[i] = v
}
return out
}
func Map[S, R any](f func(s S) R, s []S) []R {
out := make([]R, len(s))
for i := range s {
@@ -93,6 +30,7 @@ func Map[S, R any](f func(s S) R, s []S) []R {
return out
}
// Deprecated
func PopSlice[S any](s []S, index int) []S {
if index == 0 {
return s[1:]
@@ -106,6 +44,8 @@ func PopSlice[S any](s []S, index int) []S {
}
return out
}
// Deprecated
func TypedSliceToAny[S any](s []S) []any {
out := make([]any, len(s))
for i := range s {