Skip to content

Commit

Permalink
Return Generator from slice and channel New functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yaa110 committed Dec 2, 2020
1 parent 711c281 commit 21ccac3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
20 changes: 10 additions & 10 deletions generator/channel.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package generator

// Channel implements `Generator` interface for channels.
type Channel struct {
channel <-chan interface{}
value interface{}
// channel implements `Generator` interface for channels.
type channel struct {
chn <-chan interface{}
value interface{}
}

// NewChannel creates a new instance of `Channel` for `channel`.
func NewChannel(channel <-chan interface{}) *Channel {
return &Channel{channel, nil}
// Newchannel creates a new instance of `Generator` for channels.
func NewChannel(chn <-chan interface{}) Generator {
return &channel{chn, nil}
}

// Next prepares a new element, and returns `false` at the end of iteration.
func (g *Channel) Next() bool {
func (g *channel) Next() bool {
var ok bool
g.value, ok = <-g.channel
g.value, ok = <-g.chn
return ok
}

// Value returns the value of prepared element.
func (g *Channel) Value() interface{} {
func (g *channel) Value() interface{} {
return g.value
}
8 changes: 4 additions & 4 deletions generator/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import (
func TestChannelGenerator(t *testing.T) {
assert := assert.New(t)
length := 6
channel := make(chan interface{}, length)
chn := make(chan interface{}, length)

generator := generator.NewChannel(channel)
generator := generator.NewChannel(chn)

for i := 0; i < length; i++ {
channel <- i
chn <- i
ok := generator.Next()
assert.True(ok)
assert.Equal(i, generator.Value())
}

close(channel)
close(chn)

ok := generator.Next()
assert.False(ok)
Expand Down
14 changes: 7 additions & 7 deletions generator/slice.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package generator

// Slice implements `Generator` interface for slices.
type Slice struct {
// slice implements `Generator` interface for slices.
type slice struct {
elements []interface{}
value interface{}
}

// NewSlice creates a new instance of `Slice` for `elements`.
func NewSlice(elements []interface{}) *Slice {
return &Slice{elements, nil}
// Newslice creates a new instance of `Generator` for slices.
func NewSlice(elements []interface{}) Generator {
return &slice{elements, nil}
}

// Next prepares a new element, and returns `false` at the end of iteration.
// This method consumes (shifts) the elements from the slice.
func (g *Slice) Next() bool {
func (g *slice) Next() bool {
if len(g.elements) == 0 {
return false
}
Expand All @@ -22,6 +22,6 @@ func (g *Slice) Next() bool {
}

// Value returns the value of prepared element.
func (g *Slice) Value() interface{} {
func (g *slice) Value() interface{} {
return g.value
}

0 comments on commit 21ccac3

Please sign in to comment.