a lazily evaluated sequence type for Python
Project description
lazyseq is a simple library implementing a single class, LazySeq, which provides a lazily evaluated sequence that can be used like an immutable list. You can think of it as a Pythonic version of Clojure’s Seq. The main use of LazySeq is for wrapping generators or generator expressions to make them persistent, but still lazy.
LazySeq implements Python’s sequence interface, and thus has the methods __getitem__, __len__, __contains__, __iter__, __reversed__, index, and count.
Getting an item from a LazySeq is equivalent to getting an items from the provided iterable as a list. However, only the necessary elements of the iterable are evaluated (all those up to the maximum requested element), and all evaluated elements are cached on the LazySeq so it can be iterated over again. Note that some operations like len(seq) will by necessity iterate over (and thus cache) the entire iterable.
To use LazySeq, just call LazySeq on any Python iterable:
>>> from lazyseq import LazySeq
>>> seq = LazySeq(x ** 2 for x in range(5))
>>> seq
LazySeq([...])
>>> seq[:3]
[0, 1, 4]
>>> seq
LazySeq([0, 1, 4, ...])
>>> list(seq)
[0, 1, 4, 9, 16]
>>> seq
LazySeq([0, 1, 4, 9, 16])
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file lazyseq-0.1.1.tar.gz
.
File metadata
- Download URL: lazyseq-0.1.1.tar.gz
- Upload date:
- Size: 2.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b97c3ba0409bce295963aceedb75b4f98021d65227c37f181b841e405b6903f |
|
MD5 | be39fad9f64110869cc7e071ee484d6a |
|
BLAKE2b-256 | 286806f8d1dfb10c8ebde82cda785b337efe4b3b103d4e1855de98a1a55a2d92 |