-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathzip.go
66 lines (55 loc) · 1.54 KB
/
zip.go
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
63
64
65
66
// 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.
package py
// A python Zip object
type Zip struct {
itTuple Tuple
size int
}
// // A python ZipIterator iterator
// type ZipIterator struct {
// zip Zip
// }
var ZipType = NewTypeX("zip", `zip(iter1 [,iter2 [...]]) --> zip object
Return a zip object whose .__next__() method returns a tuple where
the i-th element comes from the i-th iterable argument. The .__next__()
method continues until the shortest iterable in the argument sequence
is exhausted and then it raises StopIteration.`,
ZipTypeNew, nil)
// Type of this object
func (z *Zip) Type() *Type {
return ZipType
}
// ZipTypeNew
func ZipTypeNew(metatype *Type, args Tuple, kwargs StringDict) (Object, error) {
tupleSize := len(args)
itTuple := make(Tuple, tupleSize)
for i := 0; i < tupleSize; i++ {
item := args[i]
iter, err := Iter(item)
if err != nil {
return nil, ExceptionNewf(TypeError, "zip argument #%d must support iteration", i+1)
}
itTuple[i] = iter
}
return &Zip{itTuple: itTuple, size: tupleSize}, nil
}
// Zip iterator
func (z *Zip) M__iter__() (Object, error) {
return z, nil
}
func (z *Zip) M__next__() (Object, error) {
result := make(Tuple, z.size)
for i := 0; i < z.size; i++ {
value, err := Next(z.itTuple[i])
if err != nil {
return nil, err
}
result[i] = value
}
return result, nil
}
// Check interface is satisfied
var _ I__iter__ = (*Zip)(nil)
var _ I__next__ = (*Zip)(nil)