This commit is contained in:
parent
413ab23904
commit
5ac5acd6d1
43
list/list.go
43
list/list.go
@ -5,12 +5,20 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type IList interface {
|
||||
type ICollection interface {
|
||||
Conj(data any) IList
|
||||
IsEmpty() bool
|
||||
}
|
||||
|
||||
type ISeq interface {
|
||||
First() any
|
||||
Rest() IList
|
||||
}
|
||||
|
||||
type IList interface {
|
||||
ISeq
|
||||
ICollection
|
||||
String() string
|
||||
IsEmpty() bool
|
||||
Len() int
|
||||
}
|
||||
|
||||
@ -42,22 +50,7 @@ func (this *List) Conj(val any) IList {
|
||||
}
|
||||
|
||||
func (this *EmptyList) Conj(val any) IList {
|
||||
return Conj(nil, val)
|
||||
}
|
||||
|
||||
func Conj(this IList, val any) IList {
|
||||
if this == nil {
|
||||
return New(val)
|
||||
} else {
|
||||
return this.Conj(val)
|
||||
}
|
||||
}
|
||||
|
||||
func Rest(this IList) IList {
|
||||
if this == nil {
|
||||
return emptyList
|
||||
}
|
||||
return this.Rest()
|
||||
return New(val)
|
||||
}
|
||||
|
||||
func (this *List) Rest() IList {
|
||||
@ -71,17 +64,13 @@ func (this *EmptyList) Rest() IList {
|
||||
return emptyList
|
||||
}
|
||||
|
||||
func First(this *List) any {
|
||||
func (this *List) First() any {
|
||||
if this == nil {
|
||||
return nil
|
||||
}
|
||||
return this.Value
|
||||
}
|
||||
|
||||
func (this *List) First() any {
|
||||
return First(this)
|
||||
}
|
||||
|
||||
func (this *EmptyList) First() any {
|
||||
return nil
|
||||
}
|
||||
@ -98,7 +87,7 @@ func (this *EmptyList) IsEmpty() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func String(this *List) string {
|
||||
func (this *List) String() string {
|
||||
if IsEmpty(this) {
|
||||
return "()"
|
||||
}
|
||||
@ -107,17 +96,13 @@ func String(this *List) string {
|
||||
sb.WriteRune('(')
|
||||
// Iterate and print elements
|
||||
var e IList
|
||||
for e = this; !IsEmpty(e); e = Rest(e) {
|
||||
for e = this; !IsEmpty(e); e = e.Rest() {
|
||||
sb.WriteString(fmt.Sprint(e.First()))
|
||||
sb.WriteRune(' ')
|
||||
}
|
||||
return sb.String()[:sb.Len()-1] + ")"
|
||||
}
|
||||
|
||||
func (this *List) String() string {
|
||||
return String(this)
|
||||
}
|
||||
|
||||
func (this *EmptyList) String() string {
|
||||
return "()"
|
||||
}
|
||||
|
||||
@ -37,7 +37,7 @@ func TestListRest(t *testing.T) {
|
||||
assert.Equal(t, "(5)", l.Rest().Rest().String(), "should return rest sublist")
|
||||
assert.Equal(t, "()", l.Rest().Rest().Rest().String(), "should return rest sublist")
|
||||
assert.Equal(t, Empty(), Empty().Rest(), "should return rest sublist")
|
||||
assert.Equal(t, Empty(), Rest(Empty().Rest()), "should return rest sublist")
|
||||
assert.Equal(t, Empty(), Empty().Rest().Rest(), "should return rest sublist")
|
||||
}
|
||||
|
||||
// generative tests
|
||||
|
||||
@ -21,7 +21,7 @@ func (this *Vector) Init() *Vector {
|
||||
return this
|
||||
}
|
||||
|
||||
func Conj(this *Vector, data any) *Vector {
|
||||
func (this *Vector) Conj(data any) *Vector {
|
||||
newVec := New()
|
||||
for _, el := range this._slice {
|
||||
newVec._slice = append(newVec._slice, el)
|
||||
@ -30,10 +30,6 @@ func Conj(this *Vector, data any) *Vector {
|
||||
return newVec
|
||||
}
|
||||
|
||||
func (this *Vector) Conj(data any) *Vector {
|
||||
return Conj(this, data)
|
||||
}
|
||||
|
||||
func String(this *Vector) string {
|
||||
if this == nil {
|
||||
return "[]"
|
||||
@ -54,7 +50,7 @@ func (this *Vector) String() string {
|
||||
func ToList(this *Vector) list.IList {
|
||||
l := list.Empty()
|
||||
for i := len(this._slice) - 1; i >= 0; i-- {
|
||||
l = list.Conj(l, this._slice[i])
|
||||
l = l.Conj(this._slice[i])
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user