The List Monad¶
monad.types.list - The List Monad.
-
class
monad.types.list.
List
(*items)[source]¶ Bases:
monad.types.monadplus.MonadPlus
,monad.mixins.Ord
,_abcoll.Sequence
The List Monad.
Representing nondeterministic computation.
>>> List(42) List(42) >>> List(1, 2, 3) List(1, 2, 3) >>> List([]) List([]) >>> List.from_iterable(range(3)) List(0, 1, 2) >>> List.from_iterable(n for n in (1, 2, 3) if n % 2 == 0) List(2) >>> List(List(2)) List(List(2))
Lists are lazy
>>> from itertools import count >>> m = List.from_iterable(count()) >>> m[:5] List(0, 1, 2, 3, 4) >>> m[520:524] List(520, 521, 522, 523) >>> list(m[1000:1002]) [1000, 1001]
Bind operation with
>>
>>> spawn = lambda cell: List(cell, cell) >>> spawn('c') List('c', 'c') >>> spawn('c') >> spawn List('c', 'c', 'c', 'c') >>> grow = lambda cell: List(cell + '~') >>> grow('o') List('o~') >>> grow('o') >> grow >> grow >> grow List('o~~~~') >>> generation = lambda cell: grow(cell) + spawn(cell) >>> first = List('o') >>> first List('o') >>> first >> generation List('o~', 'o', 'o') >>> first >> generation >> generation List('o~~', 'o~', 'o~', 'o~', 'o', 'o', 'o~', 'o', 'o')