get rid of extra module level funcs
All checks were successful
Go / test (push) Successful in 3s

This commit is contained in:
Adam Jeniski 2025-11-04 15:08:32 -10:00
parent 413ab23904
commit 5ac5acd6d1
3 changed files with 17 additions and 36 deletions

View File

@ -5,12 +5,20 @@ import (
"strings" "strings"
) )
type IList interface { type ICollection interface {
Conj(data any) IList Conj(data any) IList
IsEmpty() bool
}
type ISeq interface {
First() any First() any
Rest() IList Rest() IList
}
type IList interface {
ISeq
ICollection
String() string String() string
IsEmpty() bool
Len() int Len() int
} }
@ -42,22 +50,7 @@ func (this *List) Conj(val any) IList {
} }
func (this *EmptyList) Conj(val any) IList { func (this *EmptyList) Conj(val any) IList {
return Conj(nil, val) return New(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()
} }
func (this *List) Rest() IList { func (this *List) Rest() IList {
@ -71,17 +64,13 @@ func (this *EmptyList) Rest() IList {
return emptyList return emptyList
} }
func First(this *List) any { func (this *List) First() any {
if this == nil { if this == nil {
return nil return nil
} }
return this.Value return this.Value
} }
func (this *List) First() any {
return First(this)
}
func (this *EmptyList) First() any { func (this *EmptyList) First() any {
return nil return nil
} }
@ -98,7 +87,7 @@ func (this *EmptyList) IsEmpty() bool {
return true return true
} }
func String(this *List) string { func (this *List) String() string {
if IsEmpty(this) { if IsEmpty(this) {
return "()" return "()"
} }
@ -107,17 +96,13 @@ func String(this *List) string {
sb.WriteRune('(') sb.WriteRune('(')
// Iterate and print elements // Iterate and print elements
var e IList 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.WriteString(fmt.Sprint(e.First()))
sb.WriteRune(' ') sb.WriteRune(' ')
} }
return sb.String()[:sb.Len()-1] + ")" return sb.String()[:sb.Len()-1] + ")"
} }
func (this *List) String() string {
return String(this)
}
func (this *EmptyList) String() string { func (this *EmptyList) String() string {
return "()" return "()"
} }

View File

@ -37,7 +37,7 @@ func TestListRest(t *testing.T) {
assert.Equal(t, "(5)", l.Rest().Rest().String(), "should return rest sublist") 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, "()", l.Rest().Rest().Rest().String(), "should return rest sublist")
assert.Equal(t, Empty(), Empty().Rest(), "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 // generative tests

View File

@ -21,7 +21,7 @@ func (this *Vector) Init() *Vector {
return this return this
} }
func Conj(this *Vector, data any) *Vector { func (this *Vector) Conj(data any) *Vector {
newVec := New() newVec := New()
for _, el := range this._slice { for _, el := range this._slice {
newVec._slice = append(newVec._slice, el) newVec._slice = append(newVec._slice, el)
@ -30,10 +30,6 @@ func Conj(this *Vector, data any) *Vector {
return newVec return newVec
} }
func (this *Vector) Conj(data any) *Vector {
return Conj(this, data)
}
func String(this *Vector) string { func String(this *Vector) string {
if this == nil { if this == nil {
return "[]" return "[]"
@ -54,7 +50,7 @@ func (this *Vector) String() string {
func ToList(this *Vector) list.IList { func ToList(this *Vector) list.IList {
l := list.Empty() l := list.Empty()
for i := len(this._slice) - 1; i >= 0; i-- { for i := len(this._slice) - 1; i >= 0; i-- {
l = list.Conj(l, this._slice[i]) l = l.Conj(this._slice[i])
} }
return l return l
} }