Skip to main content

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

Project description

fpstreams

License: MIT

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: Option monad to eliminate None checks.
  • Error Handling: Result monad (Success/Failure) to replace ugly try/except blocks.
  • Powerful Collectors: Grouping, partitioning, and joining made simple.
  • Functional Tools: Utilities like pipe and curry for cleaner composition.

Installation

pip install fpstreams

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

Project Structure

1.Stream: The core wrapper for iterables.
2.Option: A container for optional values.
3.Result: A container for operations that may fail.
4.Collectors: Helper functions for aggregation.
5.functional: Utilities like pipe and curry

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.1.1.tar.gz (8.6 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.1.1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for fpstreams-0.1.1.tar.gz
Algorithm Hash digest
SHA256 60468dfaa950041b15fb173e4895254df21b79d1945bacbc59ca59d19532aa1e
MD5 5f2ab1bc9b28747210f0f8b4f1be7f5a
BLAKE2b-256 967a439a9728855b23f62fc1ed759f3778dc4fee68a89a92cdcd3c998b1b9d02

See more details on using hashes here.

Provenance

The following attestation bundles were made for fpstreams-0.1.1.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.1.1-py3-none-any.whl.

File metadata

  • Download URL: fpstreams-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.7 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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 17b881d8305105fc537932887e081afea449b95f69066122aff9d3dd8b25edd1
MD5 594f74cafad0f3acab23be5c18dde830
BLAKE2b-256 eee81e4f912acf18d9e868559c91fbe3f7615f6e673097ac658f462cb3403b65

See more details on using hashes here.

Provenance

The following attestation bundles were made for fpstreams-0.1.1-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