Thanks to visit codestin.com
Credit goes to github.com

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 all commits
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
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: 3 additions & 4 deletions py/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ 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)
}
// FIXME should be able to initialise from an iterable!
return NewSetFromItems(iterable.(Tuple)), nil
return NewSet(), nil
}

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

d = a ^ b
assert 1 in c

doc="set"
a = set([1,2,3])
b = set("set")
c = set((4,5))
assert len(a) == 3
assert len(b) == 3
assert len(c) == 2
assert 1 in a
assert 2 in a
assert 3 in a
assert "s" in b
assert "e" in b
assert "t" in b
assert 4 in c
assert 5 in c

Expand Down