Skip to content

Commit

Permalink
Merge pull request #1511 from actonlang/abe-sets-tuples
Browse files Browse the repository at this point in the history
AbE: improve sets & tuples docs
  • Loading branch information
plajjan authored Sep 23, 2023
2 parents b68ac53 + 21f62cc commit cdbba2b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
28 changes: 20 additions & 8 deletions docs/acton-by-example/src/primitives/sets.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
Source:
```python
actor main(env):
# Duplicate entries
s = {"foo", "foo"}
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("bar")
print("Set after adding 'bar':", 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)
Expand All @@ -23,8 +31,12 @@ actonc sets.act

Output:
```sh
Set without duplicate 'foo': {"foo"}
Set after adding 'bar': {"bar", "foo"}
Entries in set: 2
Set after discarding 'foo': {"bar"}
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'}
```
26 changes: 18 additions & 8 deletions docs/acton-by-example/src/primitives/tuples.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,28 @@ Source:
actor main(env):
# Items in a tuple can be of different types
t = ("foo", 42)
# Items are accessed liked attributes on a object (this is so we can find
# out type of items at compile time), not like the index in a list.
# Fields are accessed by their index and using field / attribute selection style:
print(t)
print(t.0)
print(t.1)

# Named tuples
# Tuples can use named fields
nt = (a="bar", b=1337)
print(nt)
print(nt.a)
print(nt.b)

r = foo()
# TODO: bool() should not be necessary
if bool(r.b):
print(r.c)

await async env.exit(0)

def foo() -> (a: str, b: bool, c: int):
"""A function that returns a tuple with fields name a and b
"""
return (a = "hello", b=True, c=123)
```

Compile and run:
Expand All @@ -28,15 +37,16 @@ actonc sets.act

Output:
```sh
("foo", 42)
('foo', 42)
foo
42
("bar", 1337)
('bar', 1337)
bar
1337
123
```

- tuples allow storing multiple items in one variable
- tuples are fixed length, unlike lists which can be appended and shortened
- fields in a tuple can be of different types
- named tuples are similar to records in other languages
- tuples have a fixed fields
- tuples with named fields is like an anonymous data class, i.e. the data type itself has no name
- tuples with named fields can be used like a simple record type

0 comments on commit cdbba2b

Please sign in to comment.