Pysistence v0.4.0 documentation

PLists - immutable lists

Introduction

A PList is a list that never changes, even when you add to it. To instantiate it, you should use the make_list convenience function:

>>> from pysistence import make_list
>>> make_list(1,2)
PList([1, 2])

As with any other list, items may be added to it:

>>> from pysistence import make_list
>>> l = make_list(1,2)
>>> l.cons(0)
PList([0, 1, 2])

The same is true of removing items:

>>> from pysistence import make_list
>>> l = make_list(1,2,3)
>>> l.rest
PList([2, 3])

You can remove any number of arbitrary arguments:

>>> from pysistence import make_list
>>> ls = make_list(1, 2, 3)
>>> ls.without(2)
PList([1, 3])

You can even chain these things together:

>>> from pysistence import make_list
>>> ls = make_list(1, 2, 3)
>>> ls.rest.without(2).cons(4)
PList([4, 3])

Reference

pysistence.persistent_list.make_list(items)

Make a new persistent list from items.

class pysistence.persistent_list.PList(first, rest=None)

A PList is a list that is mutated by copying. This makes them effectively immutable like tuples. The difference is that tuples require you to copy the entire structure. PLists will reuse as much of your existing list as possible.

concat(next_list)

Concatenate this list with another list.

first

Get the first item in this list.

frest

The first item of the rest. Equivalent to some_list.rest.first

replace(old, new)

Return a new list that has new instead of old. Will raise an exception if old is not found.

rest

Get all items except the first in this list.

reverse()

Make a new PList that is the reverse of this one.

without(*items)

Return a new PList with items removed