Skip to content

Commit

Permalink
Merge pull request #9 from go-andiamo/as_slice
Browse files Browse the repository at this point in the history
Add `AsSlice`
  • Loading branch information
marrow16 authored Apr 29, 2023
2 parents 3522b47 + 88954c2 commit 679a141
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ To update Streams to the latest version, run:
</td>
</tr>
<tr></tr>
<tr>
<td>
<code>AsSlice()</code><br>
<ul>
returns the underlying slice
</ul>
</td>
<td>
<code>[]T</code>
</td>
</tr>
<tr></tr>
<tr>
<td>
<code>Concat(add Stream[T])</code><br>
Expand Down
7 changes: 7 additions & 0 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ type Stream[T any] interface {
// if provided comparator is nil but the value type of elements in this stream are directly mappable (i.e. primitive or non-pointer types) then
// Distinct is used as the result, otherwise returns an empty stream
Unique(c Comparator[T]) Stream[T]
// AsSlice returns the underlying slice
AsSlice() []T
}

// Of creates a new stream of the values provided
Expand Down Expand Up @@ -194,6 +196,11 @@ func (s *stream[T]) Append(items ...T) Stream[T] {
}
}

// AsSlice returns the underlying slice
func (s *stream[T]) AsSlice() []T {
return s.elements
}

// Concat creates a new stream with all the elements of this stream followed by all the elements of the added stream
func (s *stream[T]) Concat(add Stream[T]) Stream[T] {
r := &stream[T]{
Expand Down
6 changes: 6 additions & 0 deletions stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func TestStream_Append(t *testing.T) {
require.Equal(t, 6, s2.Len())
}

func TestStream_AsSlice(t *testing.T) {
s := Of("a", "b", "c")
sl := s.AsSlice()
require.Equal(t, 3, len(sl))
}

func TestStream_Concat(t *testing.T) {
s := Of("a", "b", "c")
add := Of("d", "e", "f")
Expand Down
5 changes: 5 additions & 0 deletions streamable.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func (s Streamable[T]) Append(items ...T) Stream[T] {
}
}

// AsSlice returns the underlying slice
func (s Streamable[T]) AsSlice() []T {
return s
}

// Concat creates a new stream with all the elements of this stream followed by all the elements of the added stream
func (s Streamable[T]) Concat(add Stream[T]) Stream[T] {
r := &stream[T]{
Expand Down
5 changes: 5 additions & 0 deletions streamable_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func (s *streamableSlice[T]) Append(items ...T) Stream[T] {
}
}

// AsSlice returns the underlying slice
func (s *streamableSlice[T]) AsSlice() []T {
return *s.elements
}

// Concat creates a new stream with all the elements of this stream followed by all the elements of the added stream
func (s *streamableSlice[T]) Concat(add Stream[T]) Stream[T] {
r := &stream[T]{
Expand Down
6 changes: 6 additions & 0 deletions streamable_slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func TestStreamableSlice_Append(t *testing.T) {
require.Equal(t, 6, s2.Len())
}

func TestStreamableSlice_AsSlice(t *testing.T) {
s := NewStreamableSlice(&[]string{"a", "b", "c"})
sl := s.AsSlice()
require.Equal(t, 3, len(sl))
}

func TestStreamableSlice_Concat(t *testing.T) {
s := NewStreamableSlice(&[]string{"a", "b", "c"})

Expand Down
7 changes: 7 additions & 0 deletions streamable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ func TestStreamable_Append(t *testing.T) {
require.Equal(t, 6, s2.Len())
}

func TestStreamable_AsSlice(t *testing.T) {
sl := []string{"a", "b", "c"}
s := Streamable[string](sl)
sl2 := s.AsSlice()
require.Equal(t, 3, len(sl2))
}

func TestStreamable_Concat(t *testing.T) {
sl := []string{"a", "b", "c"}
s := Streamable[string](sl)
Expand Down
4 changes: 4 additions & 0 deletions test_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,7 @@ func (s *testStream[T]) Unique(c Comparator[T]) Stream[T] {
}
return r
}

func (s *testStream[T]) AsSlice() []T {
return s.elements
}

0 comments on commit 679a141

Please sign in to comment.