Replace iterator with stream and enjoy immutable.
Project description
Streams
Honor SICP 3.5 Streams. It shows a way to see "changing" data differently.
Introduction
Iterators actually irrecoverably consumes the data in it. This is not acceptable for (not pure but Lisp-like) functional programming. This package is built for solve this.
Stream is designed to be immutable, but not enforced by codes. Prevent doing anything with s._head or s._tail.
Usage (all codes are complete)
## Importing and Constructing a Stream
from sicp_streams import Stream
s = Stream("axolotl", "barnacle", "coral")
# Equivalently, use another stream as last element means "rest"
# s = Stream("axolotl", Stream("barnacle", Stream("coral", None)))
# Equivalently, use a callable as last element also means "rest", and will not be called until it is necessary
# s = Stream("axolotl", lambda: Stream("barnacle", lambda: Stream("coral", lambda: None)))
# Therefore, it is possible to construct a stream that never ends
ones = Stream(1, lambda: ones)
## Get Data from It
assert s.head == "axolotl"
assert s.tail.head == "barnacle"
assert s.tail.tail.head == "coral"
assert s.tail.tail.tail is None # end of stream is None
## Get Data by subscripting
assert (s[0], s[1], s[2]) == ("axolotl", "barnacle", "coral")
## Turn into an Iterator
it = iter(s)
assert next(it) == "axolotl"
assert next(it) == "barnacle"
assert next(it) == "coral"
## Construct from an iterable
Stream.from_iterable([1, 2, 3])
## Construct from generator function
@Stream.from_generator_function
def counts(n):
while True:
yield n
n += 1
integers = counts(1)
Toolbox Analog to itertools and Iterator-related Built-in Functions
from sicp_streams import Stream
import streamtools
# some of them have different name (for obvious but unnecessary reason)
# map -> smap
streamtools.smap(lambda x: x + 1, Stream(1, 2, 3))
# filter -> sfilter
streamtools.sfilter(lambda x: x % 2 == 0, Stream(1, 2, 3))
# zip -> szip
streamtools.szip(Stream(1, 2, 3), Stream(4, 5, 6))
# islice -> sslice
streamtools.sslice(Stream(1, 2, 3, 4, 5), 2, 4)
Demo, reimplementing SICP 3.5 (Not all of it)
import streamdemo
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sicp-streams-0.2.0.tar.gz.
File metadata
- Download URL: sicp-streams-0.2.0.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/3.7.0 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
984b5184454494298fd6fe876ffd95fe9492e3b3e74cadf92ea2eaa3e0f477af
|
|
| MD5 |
e23b9afe25a930403582c5d52a7e7bfb
|
|
| BLAKE2b-256 |
feccabc9cb37c357aca8cbfc7dffbf5976148fc29fdc289ab1d73bf4860b2b08
|
File details
Details for the file sicp_streams-0.2.0-py3-none-any.whl.
File metadata
- Download URL: sicp_streams-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/3.7.0 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ad2d2917e43f3b24324ee17c8b0ab82df3df187af06457a1028ece448786b399
|
|
| MD5 |
b0c5411a34c58749d568e274afe620e9
|
|
| BLAKE2b-256 |
52a1ee94723d111e9a6627e90962fd1af5e988812e8f6fddf4a9a209a22aad55
|