-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_set.py
More file actions
50 lines (37 loc) · 1.11 KB
/
Copy pathexample_set.py
File metadata and controls
50 lines (37 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Example: PersistentSet
Demonstrates immutable set operations with set algebra.
"""
from mnemosyne.set import PersistentSet
# Create an empty set
s = PersistentSet()
# Add elements
s1 = s.add("apple")
s2 = s1.add("banana")
s3 = s2.add("cherry")
print("Set after adding [apple, banana, cherry]:")
print("v3:", s3.to_set())
# Try adding duplicate (ignored)
s4 = s3.add("apple")
print("\nAfter adding 'apple' again:")
print("Same set:", s4 == s3)
# Remove an element
s5 = s3.remove("banana")
print("\nAfter removing 'banana':")
print("v5:", s5.to_set())
# Set operations: Union
s_a = PersistentSet().add(1).add(2).add(3)
s_b = PersistentSet().add(2).add(3).add(4)
print("\nSet A:", s_a.to_set())
print("Set B:", s_b.to_set())
union = s_a.union(s_b)
print("A U B (union):", union.to_set())
intersection = s_a.intersection(s_b)
print("A ∩ B (intersection):", intersection.to_set())
difference = s_a.difference(s_b)
print("A - B (difference):", difference.to_set())
# All versions remain accessible (immutability)
print("\nAll versions:")
print(f"s1: {s1.to_set()}")
print(f"s3: {s3.to_set()}")
print(f"s5: {s5.to_set()}")