Skip to main content

A functional programming library for Python mimicking Java Streams and JS Arrays.

Project description

fpstreams

Build Status License: MIT PyPI version

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).
  • Parallel Processing: Automatic multi-core distribution with .parallel().
  • Clean Code Syntax: Syntactic sugar like .pick() and .filter_none() to replace lambdas.
  • Data Science Ready: Convert streams directly to Pandas DataFrames, NumPy arrays, or CSV/JSON files.
  • Null Safety: Option to eliminate None checks.
  • Error Handling: Result (Success/Failure) to replace ugly try/except blocks.

Installation

pip install fpstreams

Quick Start

1. Basic

Replace messy loops with clean, readable pipelines.

from fpstreams import Stream, Collectors

data = ["apple", "banana", "cherry", "apricot", "blueberry"]

# Filter, transform, and group in one
result = (
    Stream(data)
    .filter(lambda s: s.startswith("a") or s.startswith("b"))
    .map(str.upper)
    .collect(Collectors.grouping_by(lambda s: s[0]))
)
# Output: {'A': ['APPLE', 'APRICOT'], 'B': ['BANANA', 'BLUEBERRY']}

2. Clean Code Shortcuts

Stop writing repetitive lambdas for dictionaries.

users = [
    {"id": 1, "name": "Alice", "role": "admin"},
    {"id": 2, "name": "Bob", "role": None},
    {"id": 3, "name": None, "role": "user"},
]

names = (
    Stream(users)
    .pick("name")      # Extract "name" key
    .filter_none()     # Remove None values
    .to_list()
)
# Output: ["Alice", "Bob"]

3. 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.

import math
from fpstreams import Stream

def heavy_task(x):
    return math.factorial(5000)

# Automatically uses all available CPU cores
results = (
    Stream(range(1000))
    .parallel()
    .map(heavy_task)
    .to_list()
)

4. Data Science & I/O

Seamlessly integrate with the scientific stack.

# Quick statistics
stats = Stream([1, 2, 3, 4, 5, 100]).describe()

# Output: {'count': 6, 'sum': 115, 'mean': 19.16, 'min': 1, 'max': 100, ...}

# Convert to Pandas
df = Stream(users).to_df()

# Stream directly to file
Stream(users).to_csv("output.csv")
Stream(users).to_json("output.json")

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()
)

Benchmark

Comparison between standard streams and fpstreams.parallel() on a 4-core machine:

Task Sequential(s) Parallel(s) Speedup
Heavy Calculation (Factorials) 24.7603 10.8182 2.29x
I/O Simulation (Sleep) 2.0986 0.8405 2.50x
Light Calculation (Multiplication) 0.0151 0.3796 0.04x

Note: Parallel streams have overhead. Use them for CPU-intensive tasks or slow I/O, not simple arithmetic.

Project Structure

  • Stream: The core wrapper for sequential data processing.
  • ParallelStream: A multi-core wrapper for heavy parallel processing.
  • Option: Null-safe container.
  • Result: Error-handling container.
  • Collectors: Accumulation utilities (grouping, joining, summary stats).

Licence

This project is licensed under the MIT License - see the LICENSE file for details.

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

fpstreams-0.3.0.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fpstreams-0.3.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file fpstreams-0.3.0.tar.gz.

File metadata

  • Download URL: fpstreams-0.3.0.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fpstreams-0.3.0.tar.gz
Algorithm Hash digest
SHA256 04cc6a91d4bd237341fa891bc1d5b35d90fdbf7d81133cc42fce1da3cdccbe92
MD5 c2f5e2e61b0e2ebb28d175c391e738f9
BLAKE2b-256 26a06f668a40c3bb556f7fe8558767d9b1793608789342668a82e4a8abb1aea6

See more details on using hashes here.

Provenance

The following attestation bundles were made for fpstreams-0.3.0.tar.gz:

Publisher: publish.yml on steventimes/fpstreams

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fpstreams-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: fpstreams-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fpstreams-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a79519da9bb38f1dcb38ae33cd7eb395b16f3d2c7ce79bc093397bc16bc5ff9
MD5 23606565a46e1a3dc20dc2a81709acc5
BLAKE2b-256 9fdc01fe15237c7e22fcb4bc3a665d5a6573f5cabc1298741d67d063344705cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for fpstreams-0.3.0-py3-none-any.whl:

Publisher: publish.yml on steventimes/fpstreams

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page