SHARE
TWEET

Untitled

a guest Oct 10th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package design
  2.  
  3. import "errors"
  4.  
  5. type iterator interface {
  6.     hasNext() bool
  7.     next() int
  8. }
  9.  
  10. type bowl struct {
  11.     cur   int
  12.     array []int
  13. }
  14.  
  15. var bow bowl
  16.  
  17. func init() {
  18.     bow.cur = 0
  19.     bow.array = []int{1, 2, 3}
  20. }
  21.  
  22. var errEnd = errors.New("the iterator has reached the end")
  23.  
  24. func (b *bowl) NewIterator() {
  25.     b.cur = 0
  26. }
  27.  
  28. func (b *bowl) hasNext() bool {
  29.     return b.cur < len(b.array)
  30. }
  31.  
  32. func (b *bowl) next() (int, error) {
  33.     if b.hasNext() {
  34.         b.cur++
  35.         return b.array[b.cur-1], nil
  36.     }
  37.     return 0, errEnd
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top