Skip to content

set: Implement __sub__ and __xor__ of set #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
set: Implement __xor__ of set
  • Loading branch information
DoDaek committed Sep 22, 2019
commit 3b404c485c16f3a5e92beceefc7d4cd10676ac90
20 changes: 20 additions & 0 deletions py/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,26 @@ func (s *Set) M__sub__(other Object) (Object, error) {
return ret, nil
}

func (s *Set) M__xor__(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 {
_, ok := s.items[i]
if ok {
delete(ret.items, i)
} else {
ret.items[i] = SetValue{}
}
}
return ret, nil
}

// Check interface is satisfied
var _ I__len__ = (*Set)(nil)
var _ I__bool__ = (*Set)(nil)
Expand Down
13 changes: 13 additions & 0 deletions py/tests/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,17 @@
assert 4 in d
assert 5 in d

doc="__xor__"
a = {1, 2, 3}
b = {2, 3, 4, 5}
c = a.__xor__(b)
assert 1 in c
assert 4 in c
assert 5 in c

d = a ^ b
assert 1 in c
assert 4 in c
assert 5 in c

doc="finished"