Skip to content

Commit 4f7bb19

Browse files
DoDaekcorona10
authored andcommitted
Implement and of set (#82)
* set: implement __and__ of set * set: add test code of set * set: modify year of the copyright and return value of the function
1 parent eb115a9 commit 4f7bb19

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

py/set.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ func (s *Set) M__iter__() (Object, error) {
111111
return NewIterator(items), nil
112112
}
113113

114+
func (s *Set) M__and__(other Object) (Object, error) {
115+
ret := NewSet()
116+
b, ok := other.(*Set)
117+
if !ok {
118+
return nil, ExceptionNewf(TypeError, "unsupported operand type(s) for &: '%s' and '%s'", s.Type().Name, other.Type().Name)
119+
}
120+
for i := range b.items {
121+
if _, ok := s.items[i]; ok {
122+
ret.items[i] = SetValue{}
123+
}
124+
}
125+
return ret, nil
126+
}
127+
114128
// Check interface is satisfied
115129
var _ I__len__ = (*Set)(nil)
116130
var _ I__bool__ = (*Set)(nil)

py/tests/set.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2019 The go-python Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
doc="__and__"
6+
a = {1, 2, 3}
7+
b = {2, 3, 4, 5}
8+
c = a.__and__(b)
9+
assert 2 in c
10+
assert 3 in c
11+
12+
d = a & b
13+
assert 2 in d
14+
assert 3 in d
15+
16+
doc="finished"

0 commit comments

Comments
 (0)