init commit
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package vector
|
||||
|
||||
import (
|
||||
"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)
|
||||
}
|
||||
Reference in New Issue
Block a user