mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-27 11:17:29 +02:00
restructured the project to comply community guideline, for others check release note
This commit is contained in:
71
internal/utils/functional/slice.go
Normal file
71
internal/utils/functional/slice.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package functional
|
||||
|
||||
type Slice[T any] struct {
|
||||
s []T
|
||||
}
|
||||
|
||||
func NewSlice[T any]() *Slice[T] {
|
||||
return &Slice[T]{make([]T, 0)}
|
||||
}
|
||||
|
||||
func NewSliceN[T any](n int) *Slice[T] {
|
||||
return &Slice[T]{make([]T, n)}
|
||||
}
|
||||
|
||||
func NewSliceFrom[T any](s []T) *Slice[T] {
|
||||
return &Slice[T]{s}
|
||||
}
|
||||
|
||||
func (s *Slice[T]) Size() int {
|
||||
return len(s.s)
|
||||
}
|
||||
|
||||
func (s *Slice[T]) Empty() bool {
|
||||
return len(s.s) == 0
|
||||
}
|
||||
|
||||
func (s *Slice[T]) NotEmpty() bool {
|
||||
return len(s.s) > 0
|
||||
}
|
||||
|
||||
func (s *Slice[T]) Iterator() []T {
|
||||
return s.s
|
||||
}
|
||||
|
||||
func (s *Slice[T]) Set(i int, v T) {
|
||||
s.s[i] = v
|
||||
}
|
||||
|
||||
func (s *Slice[T]) Add(e T) *Slice[T] {
|
||||
s.s = append(s.s, e)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Slice[T]) AddRange(other *Slice[T]) *Slice[T] {
|
||||
s.s = append(s.s, other.s...)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Slice[T]) ForEach(do func(T)) {
|
||||
for _, v := range s.s {
|
||||
do(v)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Slice[T]) Map(m func(T) T) *Slice[T] {
|
||||
n := make([]T, len(s.s))
|
||||
for i, v := range s.s {
|
||||
n[i] = m(v)
|
||||
}
|
||||
return &Slice[T]{n}
|
||||
}
|
||||
|
||||
func (s *Slice[T]) Filter(f func(T) bool) *Slice[T] {
|
||||
n := make([]T, 0)
|
||||
for _, v := range s.s {
|
||||
if f(v) {
|
||||
n = append(n, v)
|
||||
}
|
||||
}
|
||||
return &Slice[T]{n}
|
||||
}
|
||||
Reference in New Issue
Block a user