-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathiterator.go
More file actions
62 lines (53 loc) · 1.28 KB
/
iterator.go
File metadata and controls
62 lines (53 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2018 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Iterator objects
package py
// A python Iterator object
type Iterator struct {
Pos int
Seq Object
}
var IteratorType = NewType("iterator", "iterator type")
// Type of this object
func (o *Iterator) Type() *Type {
return IteratorType
}
// Define a new iterator
func NewIterator(Seq Object) *Iterator {
m := &Iterator{
Pos: 0,
Seq: Seq,
}
return m
}
func (it *Iterator) M__iter__() (Object, error) {
return it, nil
}
// Get next one from the iteration
func (it *Iterator) M__next__() (res Object, err error) {
if tuple, ok := it.Seq.(Tuple); ok {
if it.Pos >= len(tuple) {
return nil, StopIteration
}
res = tuple[it.Pos]
it.Pos++
return res, nil
}
index := Int(it.Pos)
if I, ok := it.Seq.(I__getitem__); ok {
res, err = I.M__getitem__(index)
} else if res, ok, err = TypeCall1(it.Seq, "__getitem__", index); !ok {
return nil, ExceptionNewf(TypeError, "'%s' object is not iterable", it.Type().Name)
}
if err != nil {
if IsException(IndexError, err) {
return nil, StopIteration
}
return nil, err
}
it.Pos++
return res, nil
}
// Check interface is satisfied
var _ I_iterator = (*Iterator)(nil)