Sets
Source:
actor main(env):
# set syntax is similar to dicts using {} but without keys
s = {"foo", "bar"}
print("Set content:", s)
if "foo" in s:
print("'foo' is in the set")
if "a" not in s:
print("'a' is not in the set")
# Adding an item that is already in the set does nothing
s.add("foo")
print("Set without duplicate 'foo':", s)
s.add("a")
print("Set after adding 'a':", s)
if "a" in s:
print("'a' is in the set now")
print("Entries in set:", len(s))
s.discard("foo")
print("Set after discarding 'foo':", s)
# Use set() to create an empty set. {} means an empty dict!
empty_set = set()
env.exit(0)
Compile and run:
actonc sets.act
./sets
Output:
Set content: {'bar', 'foo'}
'foo' is in the set
'a' is not in the set
Set without duplicate 'foo': {'bar', 'foo'}
Set after adding 'a': {'bar', 'a', 'foo'}
'a' is in the set now
Entries in set: 3
Set after discarding 'foo': {'bar', 'a'}