Skip to content

Commit

Permalink
Added conversion to slice
Browse files Browse the repository at this point in the history
  • Loading branch information
lrleon committed Mar 21, 2021
1 parent f121d7e commit 932db96
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 932db96

Please sign in to comment.