tuple and code cleanup
This commit is contained in:
83
set.go
83
set.go
@@ -16,18 +16,12 @@ func NewSetFrom[T any](slice []T) Set[T] {
|
||||
}
|
||||
return s
|
||||
}
|
||||
func (s Set[T]) Len() int {
|
||||
return len(s)
|
||||
}
|
||||
func (s Set[T]) Cap() int {
|
||||
return cap(s)
|
||||
}
|
||||
func (s Set[T]) Get(index int) T {
|
||||
return s[index]
|
||||
}
|
||||
func (s Set[T]) Last() T {
|
||||
return s[s.Len()-1]
|
||||
}
|
||||
func (s Set[T]) Len() int { return len(s) }
|
||||
func (s Set[T]) Cap() int { return cap(s) }
|
||||
func (s Set[T]) Get(index int) T { return s[index] }
|
||||
func (s Set[T]) Last() T { return s.Get(s.Len() - 1) }
|
||||
func (s Set[T]) First() T { return s.Get(0) }
|
||||
|
||||
func (s Set[T]) Index(el T) int {
|
||||
for i := range s {
|
||||
if reflect.DeepEqual(s[i], el) {
|
||||
@@ -62,33 +56,6 @@ func (s Set[T]) Remove(el T) Set[T] {
|
||||
}
|
||||
return s.Pop(index)
|
||||
}
|
||||
func (s Set[T]) Filter(f func(e T) bool) Set[T] {
|
||||
out := make(Set[T], 0)
|
||||
for _, v := range s {
|
||||
if f(v) {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
func (s Set[T]) ToSlice() Slice[T] {
|
||||
out := make(Slice[T], s.Len())
|
||||
copy(out, s)
|
||||
return out
|
||||
}
|
||||
func (s Set[T]) ToArray() []T {
|
||||
out := make([]T, len(s))
|
||||
copy(out, s)
|
||||
return out
|
||||
}
|
||||
func (s Set[T]) ToAnyArray() []any {
|
||||
out := make([]any, len(s))
|
||||
for i, v := range s {
|
||||
out[i] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s Set[T]) Equal(s2 Set[T]) bool {
|
||||
if s.Len() != s2.Len() {
|
||||
return false
|
||||
@@ -103,3 +70,41 @@ func (s Set[T]) Equal(s2 Set[T]) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s Set[T]) ToSlice() Slice[T] { return NewSliceFrom(s) }
|
||||
func (s Set[T]) ToTuple() Tuple[T] { return NewTupleFrom(s) }
|
||||
func (s Set[T]) ToArray() []T {
|
||||
out := make([]T, len(s))
|
||||
copy(out, s)
|
||||
return out
|
||||
}
|
||||
func (s Set[T]) ToAnyArray() []any {
|
||||
out := make([]any, len(s))
|
||||
for i, v := range s {
|
||||
out[i] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
func (s Set[T]) Filter(f func(e T) bool) Set[T] {
|
||||
out := make(Set[T], 0)
|
||||
for _, v := range s {
|
||||
if f(v) {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
func (s Set[T]) ForEach(f func(int, T)) Set[T] {
|
||||
for i, v := range s {
|
||||
f(i, v)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Map is mutable func
|
||||
func (s Set[T]) Map(f func(e T) T) Set[T] {
|
||||
for i, v := range s {
|
||||
s[i] = f(v)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user