tuple immutable
This commit is contained in:
24
tuple.go
24
tuple.go
@@ -1,19 +1,23 @@
|
||||
package extypes
|
||||
|
||||
import "reflect"
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type Tuple[T any] struct {
|
||||
s Slice[T]
|
||||
}
|
||||
|
||||
func NewTupleFrom[T any](src []T) Tuple[T] {
|
||||
return Tuple[T]{src}
|
||||
}
|
||||
func (t Tuple[T]) Len() int { return t.s.Len() }
|
||||
func (t Tuple[T]) Cap() int { return t.s.Cap() }
|
||||
func (t Tuple[T]) Get(index int) T { return t.s.Get(index) }
|
||||
func NewTupleFrom[T any](src []T) Tuple[T] { return Tuple[T]{src} }
|
||||
func NewTuple[T any]() Tuple[T] { return Tuple[T]{make(Slice[T], 0)} }
|
||||
func (t Tuple[T]) Len() int { return t.s.Len() }
|
||||
func (t Tuple[T]) Cap() int { return t.s.Cap() }
|
||||
func (t Tuple[T]) Get(index int) T { return t.s.Get(index) }
|
||||
|
||||
func (t Tuple[T]) Equal(t2 Tuple[T]) bool {
|
||||
if t.Len() != t2.Len() || t.Cap() != t2.Cap() {
|
||||
if t.Len() != t2.Len() {
|
||||
return false
|
||||
}
|
||||
for i := range t.s {
|
||||
@@ -55,3 +59,7 @@ func (t Tuple[T]) ForEach(f func(int, T)) Tuple[T] {
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func (t Tuple[T]) String() string { return fmt.Sprint(t.s) }
|
||||
func (t *Tuple[T]) UnmarshalJSON(data []byte) error { return json.Unmarshal(data, &t.s) }
|
||||
func (t Tuple[T]) MarshalJSON() ([]byte, error) { return json.Marshal(t.s) }
|
||||
|
||||
Reference in New Issue
Block a user