diff --git a/py/set.go b/py/set.go index f38b32ff..af4eb4e5 100644 --- a/py/set.go +++ b/py/set.go @@ -125,6 +125,23 @@ func (s *Set) M__and__(other Object) (Object, error) { return ret, nil } +func (s *Set) M__or__(other Object) (Object, error) { + ret := NewSet() + b, ok := other.(*Set) + if !ok { + return nil, ExceptionNewf(TypeError, "unsupported operand type(s) for &: '%s' and '%s'", s.Type().Name, other.Type().Name) + } + for j := range s.items { + ret.items[j] = SetValue{} + } + for i := range b.items { + if _, ok := s.items[i]; !ok { + ret.items[i] = SetValue{} + } + } + return ret, nil +} + // Check interface is satisfied var _ I__len__ = (*Set)(nil) var _ I__bool__ = (*Set)(nil) diff --git a/py/tests/set.py b/py/tests/set.py index e1500ed1..e46b412a 100644 --- a/py/tests/set.py +++ b/py/tests/set.py @@ -13,4 +13,21 @@ assert 2 in d assert 3 in d +doc="__or__" +a = {1, 2, 3} +b = {2, 3, 4, 5} +c = a.__or__(b) +assert 1 in c +assert 2 in c +assert 3 in c +assert 4 in c +assert 5 in c + +d = a | b +assert 1 in c +assert 2 in c +assert 3 in c +assert 4 in c +assert 5 in c + doc="finished" \ No newline at end of file