-
Notifications
You must be signed in to change notification settings - Fork 336
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
Showing
13 changed files
with
1,279 additions
and
1,211 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,65 @@ | ||
package rxgo | ||
|
||
type Iterable interface { | ||
Iterator() Iterator | ||
} | ||
|
||
type iterableFromChannel struct { | ||
ch chan interface{} | ||
} | ||
|
||
type iterableFromSlice struct { | ||
s []interface{} | ||
} | ||
|
||
type iterableFromRange struct { | ||
start int | ||
count int | ||
} | ||
|
||
type iterableFromFunc struct { | ||
f func(chan interface{}) | ||
} | ||
|
||
func (it *iterableFromFunc) Iterator() Iterator { | ||
out := make(chan interface{}) | ||
go it.f(out) | ||
return newIteratorFromChannel(out) | ||
} | ||
|
||
func (it *iterableFromChannel) Iterator() Iterator { | ||
return newIteratorFromChannel(it.ch) | ||
} | ||
|
||
func (it *iterableFromSlice) Iterator() Iterator { | ||
return newIteratorFromSlice(it.s) | ||
} | ||
|
||
func (it *iterableFromRange) Iterator() Iterator { | ||
return newIteratorFromRange(it.start-1, it.start+it.count) | ||
} | ||
|
||
func newIterableFromChannel(ch chan interface{}) Iterable { | ||
return &iterableFromChannel{ | ||
ch: ch, | ||
} | ||
} | ||
|
||
func newIterableFromSlice(s []interface{}) Iterable { | ||
return &iterableFromSlice{ | ||
s: s, | ||
} | ||
} | ||
|
||
func newIterableFromRange(start, count int) Iterable { | ||
return &iterableFromRange{ | ||
start: start, | ||
count: count, | ||
} | ||
} | ||
|
||
func newIterableFromFunc(f func(chan interface{})) Iterable { | ||
return &iterableFromFunc{ | ||
f: f, | ||
} | ||
} |
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,67 @@ | ||
package rxgo | ||
|
||
// Iterator type is implemented by Iterable. | ||
import "github.com/reactivex/rxgo/errors" | ||
|
||
type Iterator interface { | ||
Next() (interface{}, error) | ||
} | ||
|
||
type iteratorFromChannel struct { | ||
ch chan interface{} | ||
} | ||
|
||
type iteratorFromSlice struct { | ||
index int | ||
s []interface{} | ||
} | ||
|
||
type iteratorFromRange struct { | ||
current int | ||
end int // Included | ||
} | ||
|
||
func (it *iteratorFromChannel) Next() (interface{}, error) { | ||
if next, ok := <-it.ch; ok { | ||
return next, nil | ||
} | ||
|
||
return nil, errors.New(errors.EndOfIteratorError) | ||
} | ||
|
||
func (it *iteratorFromSlice) Next() (interface{}, error) { | ||
it.index = it.index + 1 | ||
if it.index < len(it.s) { | ||
return it.s[it.index], nil | ||
} else { | ||
return nil, errors.New(errors.EndOfIteratorError) | ||
} | ||
} | ||
|
||
func (it *iteratorFromRange) Next() (interface{}, error) { | ||
it.current = it.current + 1 | ||
if it.current <= it.end { | ||
return it.current, nil | ||
} else { | ||
return nil, errors.New(errors.EndOfIteratorError) | ||
} | ||
} | ||
|
||
func newIteratorFromChannel(ch chan interface{}) Iterator { | ||
return &iteratorFromChannel{ | ||
ch: ch, | ||
} | ||
} | ||
|
||
func newIteratorFromSlice(s []interface{}) Iterator { | ||
return &iteratorFromSlice{ | ||
index: -1, | ||
s: s, | ||
} | ||
} | ||
|
||
func newIteratorFromRange(start, end int) Iterator { | ||
return &iteratorFromRange{ | ||
current: start, | ||
end: end, | ||
} | ||
} |
Oops, something went wrong.