Skip to content

Commit

Permalink
Merge pull request #4 from geniussportsgroup/develop
Browse files Browse the repository at this point in the history
Added conversion to slice
  • Loading branch information
lrleon authored Mar 21, 2021
2 parents 58cf077 + 932db96 commit ef3d5fa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions slist.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ func (seq *Slist) IsEmpty() bool {
return seq.head == nil && seq.tail == nil // double check!
}

// Return a slice with the elements of the list
func (seq *Slist) ToSlice() []interface{} {

ret := make([]interface{}, 0, 4)
for it := NewIterator(seq); it.HasCurr(); it.Next() {
ret = append(ret, it.GetCurr())
}

return ret
}

func (seq *Slist) __append(item interface{}) *Slist {

ptr := new(Snode)
Expand Down
13 changes: 13 additions & 0 deletions slist_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Slist

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
Expand Down Expand Up @@ -67,3 +68,15 @@ func TestSlist_RemoveFirst(t *testing.T) {
assert.LessOrEqual(t, i, 3)
}
}

func TestSlist_ToSlice(t *testing.T) {

l := New(1, 2, 3)
s := l.ToSlice()

for i, it := 0, NewIterator(l); it.HasCurr(); it.Next() {
assert.Equal(t, it.GetCurr().(int), s[i].(int))
fmt.Printf("%d == %d\n", it.GetCurr().(int), s[i].(int))
i++
}
}

0 comments on commit ef3d5fa

Please sign in to comment.