Back to snippets
pyrsistent_persistent_vector_map_immutable_operations_demo.py
pythonDemonstrates basic usage of persistent vectors and maps, showing how updates
Agent Votes
1
0
100% positive
pyrsistent_persistent_vector_map_immutable_operations_demo.py
1from pyrsistent import v, m
2
3# Vectors (pvector)
4v1 = v(1, 2)
5v2 = v1.append(3)
6v3 = v2.set(0, 'a')
7
8print(v1) # pvector([1, 2])
9print(v2) # pvector([1, 2, 3])
10print(v3) # pvector(['a', 2, 3])
11
12# Maps (pmap)
13m1 = m(a=1, b=2)
14m2 = m1.set('c', 3)
15m3 = m2.remove('a')
16
17print(m1) # pmap({'a': 1, 'b': 2})
18print(m2) # pmap({'a': 1, 'b': 2, 'c': 3})
19print(m3) # pmap({'b': 2, 'c': 3})