-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
teivah
committed
Dec 5, 2018
1 parent
f5d04df
commit c8dc5d8
Showing
11 changed files
with
419 additions
and
690 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package rxgo | ||
|
||
type Iterable interface { | ||
Iterator() Iterator | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,30 @@ | ||
package rxgo | ||
|
||
// Iterator type is implemented by Iterable. | ||
type Iterator interface { | ||
Next() (interface{}, error) | ||
Next() bool | ||
Value() interface{} | ||
} | ||
|
||
type iteratorFromChannel struct { | ||
item interface{} | ||
ch chan interface{} | ||
} | ||
|
||
func (s *iteratorFromChannel) Next() bool { | ||
if v, ok := <-s.ch; ok { | ||
s.item = v | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (s *iteratorFromChannel) Value() interface{} { | ||
return s.item | ||
} | ||
|
||
func NewIteratorFromChannel(ch chan interface{}) Iterator { | ||
return &iteratorFromChannel{ | ||
ch: ch, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package rxgo | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIteratorFromChannel(t *testing.T) { | ||
ch := make(chan interface{}, 1) | ||
it := NewIteratorFromChannel(ch) | ||
|
||
ch <- 1 | ||
assert.True(t, it.Next()) | ||
assert.Equal(t, 1, it.Value()) | ||
|
||
ch <- 2 | ||
assert.True(t, it.Next()) | ||
assert.Equal(t, 2, it.Value()) | ||
|
||
close(ch) | ||
assert.False(t, it.Next()) | ||
} |
Oops, something went wrong.