mal-go/vector/vector.go
ajet 5ac5acd6d1
All checks were successful
Go / test (push) Successful in 3s
get rid of extra module level funcs
2025-11-04 15:08:32 -10:00

61 lines
1.0 KiB
Go

package vector
import (
"mal-go/list"
"mal-go/utils"
"strings"
)
// TODO: replace COW with proper HashArrayMappedTrie impelmentation of PersistentVector
type Vector struct {
_slice []any
}
func New() *Vector {
return new(Vector).Init()
}
func (this *Vector) Init() *Vector {
this._slice = make([]any, 0)
return this
}
func (this *Vector) Conj(data any) *Vector {
newVec := New()
for _, el := range this._slice {
newVec._slice = append(newVec._slice, el)
}
newVec._slice = append(newVec._slice, data)
return newVec
}
func String(this *Vector) string {
if this == nil {
return "[]"
}
var sb strings.Builder
sb.WriteRune('[')
for _, el := range this._slice {
sb.WriteString(utils.Stringify(el))
sb.WriteRune(' ')
}
return sb.String()[:sb.Len()-1] + "]"
}
func (this *Vector) String() string {
return String(this)
}
func ToList(this *Vector) list.IList {
l := list.Empty()
for i := len(this._slice) - 1; i >= 0; i-- {
l = l.Conj(this._slice[i])
}
return l
}
func (this *Vector) ToList() list.IList {
return ToList(this)
}