Skip to content

set: Implement initialization set with sequence #100

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 4 commits into from
Sep 29, 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
set: Implement initialization set with sequence
  • Loading branch information
SanggiHong committed Sep 29, 2019
commit 7d566b87d82fd69246a5bc491505b09af318200f
20 changes: 20 additions & 0 deletions py/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ func SequenceList(v Object) (*List, error) {
}
}

// Converts a sequence object v into a Set
func SequenceSet(v Object) (*Set, error) {
switch x := v.(type) {
case Tuple:
return NewSetFromItems(x), nil
case *List:
return NewSetFromItems(x.Items), nil
default:
s := NewSet()
err := Iterate(v, func(item Object) bool {
s.Add(item)
return false
})
if err != nil {
return nil, err
}
return s, nil
}
}

// Call __next__ for the python object
//
// Returns the next object
Expand Down
7 changes: 4 additions & 3 deletions py/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ func SetNew(metatype *Type, args Tuple, kwargs StringDict) (Object, error) {
if err != nil {
return nil, err
}
if iterable == nil {
return NewSet(), nil
if iterable != nil {
return SequenceSet(iterable)
}
return NewSet(), nil
// FIXME should be able to initialise from an iterable!
return NewSetFromItems(iterable.(Tuple)), nil
// return NewSetFromItems(iterable.(Tuple)), nil
}

var FrozenSetType = NewType("frozenset", "frozenset() -> empty frozenset object\nfrozenset(iterable) -> frozenset object\n\nBuild an immutable unordered collection of unique elements.")
Expand Down
12 changes: 12 additions & 0 deletions py/tests/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@

d = a ^ b
assert 1 in c

doc="set"
a = set([1,2,3])
b = set("set")
c = set((4,5))
assert 1 in a
assert 2 in a
assert 3 in a
assert "s" in b
assert "e" in b
assert "t" in b
>>>>>>> set-init-with-list
assert 4 in c
assert 5 in c

Expand Down