Using the lazy List() functor

The List() functor can be confusing at first. It's extremely lazy, unlike Python's built-in list type. When we evaluate the built-in list(range(10)) method, the list() function will evaluate the range() object to create a list with 10 items. The PyMonad List() functor, however, is too lazy to even do this evaluation.

Here's the comparison:

>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> List(range(10))
[range(0, 10)]  

The List() functor did not evaluate the range() object, it just preserved it without being evaluated. The pymonad.List() function is useful to collect functions without evaluating them.

The use of range() here can be a little confusing. The Python 3 range() object is also lazy. In this case, ...

Get Functional Python Programming - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.