A functional programming library for Python mimicking Java Streams and JS Arrays.
Project description
fpstreams
A robust, type-safe functional programming library for Python.
fpstreams brings the power of Java Streams, Rust Results, and JavaScript Array methods to Python. It provides a fluent interface for data processing, null safety, and error handling without the boilerplate, all while remaining fully typed for IDE autocompletion.
Features
- Fluent Streams: Lazy evaluation chains (
map,filter,reduce,zip) - Null Safety:
Optionmonad to eliminateNonechecks. - Error Handling:
Resultmonad (Success/Failure) to replace uglytry/exceptblocks. - Powerful Collectors: Grouping, partitioning, and joining made simple.
- Functional Tools: Utilities like
pipeandcurryfor cleaner composition.
Installation
pip install fpstreams
Comparison between standard streams and fpstreams.parallel() on a 4-core machine:
| Task | Sequential | Parallel | Speedup |
|---|---|---|---|
| Heavy Calculation (Factorials) | 21.10s | 8.25s | 2.56x |
| I/O Simulation (Requests) | 2.08s | 0.73s | 2.87x |
Note: Parallelism has overhead. Use .parallel() only for computationally intensive tasks or large datasets.
Usage Example
Streams (using name from Java)
from fpstreams import Stream
data = [
{"name": "Alice", "role": "admin", "age": 30},
{"name": "Bob", "role": "dev", "age": 25},
{"name": "Charlie", "role": "admin", "age": 45}
]
# Get names of admins whose age is over 25, sorted alphabetically
names = (
Stream(data)
.filter(lambda u: u["role"] == "admin")
.filter(lambda u: u["age"] > 25)
.map(lambda u: u["name"].upper())
.sorted()
.to_list()
)
# Output: ['ALICE', 'CHARLIE']
Null Safety with Option
from fpstreams import Stream
# Find the first user named "Steven" (who doesn't exist)
email = (
Stream(data)
.filter(lambda u: u["name"] == "Steven")
.find_first() # Returns Option[User]
.map(lambda u: u["email"]) # Skipped because Option is empty
.or_else("default@example.com")
)
Error handling with Result
from fpstreams import Result
def risky_parsing(value):
return int(value) # Might crash if value is not a number
# Safe execution
result = (
Result.of(lambda: risky_parsing("invalid"))
.map(lambda x: x * 2)
.on_failure(lambda e: print(f"Parsing failed: {e}")) # Logs error
.get_or_else(0) # Returns 0 instead of crashing
)
Collectors
grouping data using collectors
from fpstreams import Stream, Collectors
fruits = ["apple", "avocado", "banana", "blueberry", "cherry"]
# Group fruits by their first letter
grouped = (
Stream(fruits)
.collect(Collectors.grouping_by(lambda s: s[0]))
)
# Output: {'a': ['apple', 'avocado'], 'b': ['banana', 'blueberry'], 'c': ['cherry']}
Infinite Streams & Lazy Evaluation
Process massive datasets efficiently. Operations are only executed when needed.
def infinite_counter():
n = 0
while True:
yield n
n += 1
# Take only the first 10 even numbers
evens = (
Stream(infinite_counter())
.filter(lambda x: x % 2 == 0)
.limit(10)
.to_list()
)
Parallel Processing
fpstreams can automatically distribute heavy workloads across all CPU cores using the .parallel() method. It uses an optimized Map-Reduce architecture to minimize memory usage.
from fpstreams import Stream
def heavy_task(x):
return x ** 5000
# Automatically uses all available CPU cores
results = (
Stream(range(10000))
.parallel()
.map(heavy_task)
.to_list()
)
Project Structure
Stream: The core wrapper for sequential data processing.ParallelStream: A multi-core wrapper for heavy parallel processing.Option: A monad container for optional values (Null Safety).Result: A monad container for operations that may fail (Error Handling).Collectors: Helper functions for aggregation (e.g.,grouping_by,partitioning_by).functional: Utilities likepipeandcurryfor functional composition.
Project details
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 fpstreams-0.2.0.tar.gz.
File metadata
- Download URL: fpstreams-0.2.0.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f65faf1c1cc8406427df04a7c77b469bdb3a7c14ee52eb8b41046d903fb3fb81
|
|
| MD5 |
39d48e26e98744508885eaf4b9915d7e
|
|
| BLAKE2b-256 |
64f913203ed61cc732a9e20aa94c24207853110210769e1dff0567ee733d72e8
|
Provenance
The following attestation bundles were made for fpstreams-0.2.0.tar.gz:
Publisher:
publish.yml on steventimes/fpstreams
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fpstreams-0.2.0.tar.gz -
Subject digest:
f65faf1c1cc8406427df04a7c77b469bdb3a7c14ee52eb8b41046d903fb3fb81 - Sigstore transparency entry: 763673613
- Sigstore integration time:
-
Permalink:
steventimes/fpstreams@bdb275afbee2e876195d19af01c01a6e1cd1db49 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/steventimes
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@bdb275afbee2e876195d19af01c01a6e1cd1db49 -
Trigger Event:
release
-
Statement type:
File details
Details for the file fpstreams-0.2.0-py3-none-any.whl.
File metadata
- Download URL: fpstreams-0.2.0-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9bf97dfbe419c1696bd477bcf2f3c50472d853369bc2cc0049455a1dc181dc78
|
|
| MD5 |
29a5eadf247a4d616da6bcc1637e7289
|
|
| BLAKE2b-256 |
94d08c6c9c0974a1b49c81fec5d49e63818b11d0f7710a88f6554f0079e2efe6
|
Provenance
The following attestation bundles were made for fpstreams-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on steventimes/fpstreams
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fpstreams-0.2.0-py3-none-any.whl -
Subject digest:
9bf97dfbe419c1696bd477bcf2f3c50472d853369bc2cc0049455a1dc181dc78 - Sigstore transparency entry: 763673616
- Sigstore integration time:
-
Permalink:
steventimes/fpstreams@bdb275afbee2e876195d19af01c01a6e1cd1db49 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/steventimes
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@bdb275afbee2e876195d19af01c01a6e1cd1db49 -
Trigger Event:
release
-
Statement type: