Skip to content

Commit 261242c

Browse files
DoDaekcorona10
authored andcommitted
set: Implement __or__ of set (#84)
* set: Implement __or__ of set * set: create a new instance for the value returned
1 parent 4f7bb19 commit 261242c

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

py/set.go

+17
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,23 @@ func (s *Set) M__and__(other Object) (Object, error) {
125125
return ret, nil
126126
}
127127

128+
func (s *Set) M__or__(other Object) (Object, error) {
129+
ret := NewSet()
130+
b, ok := other.(*Set)
131+
if !ok {
132+
return nil, ExceptionNewf(TypeError, "unsupported operand type(s) for &: '%s' and '%s'", s.Type().Name, other.Type().Name)
133+
}
134+
for j := range s.items {
135+
ret.items[j] = SetValue{}
136+
}
137+
for i := range b.items {
138+
if _, ok := s.items[i]; !ok {
139+
ret.items[i] = SetValue{}
140+
}
141+
}
142+
return ret, nil
143+
}
144+
128145
// Check interface is satisfied
129146
var _ I__len__ = (*Set)(nil)
130147
var _ I__bool__ = (*Set)(nil)

py/tests/set.py

+17
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,21 @@
1313
assert 2 in d
1414
assert 3 in d
1515

16+
doc="__or__"
17+
a = {1, 2, 3}
18+
b = {2, 3, 4, 5}
19+
c = a.__or__(b)
20+
assert 1 in c
21+
assert 2 in c
22+
assert 3 in c
23+
assert 4 in c
24+
assert 5 in c
25+
26+
d = a | b
27+
assert 1 in c
28+
assert 2 in c
29+
assert 3 in c
30+
assert 4 in c
31+
assert 5 in c
32+
1633
doc="finished"

0 commit comments

Comments
 (0)