65 lines
1.1 KiB
Go
65 lines
1.1 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 Conj(this *Vector, 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 (this *Vector) Conj(data any) *Vector {
|
|
return Conj(this, data)
|
|
}
|
|
|
|
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.New()
|
|
for i := len(this._slice) - 1; i >= 0; i-- {
|
|
l = list.Conj(l, this._slice[i])
|
|
}
|
|
return l
|
|
}
|
|
|
|
func (this *Vector) ToList() list.IList {
|
|
return ToList(this)
|
|
}
|