Skip to content

Implement set repr #117

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 6 commits into from
Oct 14, 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
Next Next commit
Implemented __repr__ of set
  • Loading branch information
xarus01 committed Oct 8, 2019
commit a4d8dc4a9693d23faf83612f6bcd0db61553896f
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ require (
github.com/gopherjs/gopherwasm v1.0.0 // indirect
github.com/peterh/liner v1.1.0
)

go 1.13
21 changes: 21 additions & 0 deletions py/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package py

import "bytes"

var SetType = NewTypeX("set", "set() -> new empty set object\nset(iterable) -> new set object\n\nBuild an unordered collection of unique elements.", SetNew, nil)

type SetValue struct{}
Expand Down Expand Up @@ -103,6 +105,25 @@ func (s *Set) M__bool__() (Object, error) {
return NewBool(len(s.items) > 0), nil
}

func (s * Set) M__repr__() (Object, error) {
var out bytes.Buffer
out.WriteRune('{')
spacer := false
for item := range s.items {
if spacer {
out.WriteString(", ")
}
str, err := ReprAsString(item)
if err != nil {
return nil, err
}
out.WriteString(str)
spacer = true
}
out.WriteRune('}')
return String(out.String()), nil
}

func (s *Set) M__iter__() (Object, error) {
items := make(Tuple, 0, len(s.items))
for item := range s.items {
Expand Down
4 changes: 4 additions & 0 deletions py/tests/set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
doc="repr"
assert repr({1,2,3}) == "{1, 2, 3}"

doc="finished"