PyFunctional
PyFunctional is a Python package designed for creating data pipelines using a functional programming paradigm, inspired by Scala and Spark. It provides tools for chaining operations on sequences, supporting lazy evaluation, parallel processing, and various data transformations. The current version is 1.5.0, and the project generally focuses on API stability since its 1.0.0 release.
Warnings
- breaking The library was renamed from `ScalaFunctional` to `PyFunctional` in version 0.6.0. Projects using the old package name will need to update their `install_requires` and import paths.
- breaking The behavior of `Sequence.zip_with_index` was modified in version 0.5.0. It no longer behaves as expected in previous versions and should be replaced.
- gotcha Starting from version 0.3.0, all operations in PyFunctional are lazy by default. This means transformations are not executed until an action (like `reduce`, `to_list`, `to_dict`, or iteration) consumes the sequence. This can lead to unexpected behavior if intermediate results are expected to be immediately available or if debugging relies on immediate evaluation.
- gotcha For automatic parallelization of pipeline operations (e.g., `map`, `filter`), you must explicitly import and use `pseq` instead of `seq`. `seq` does not use parallel processing.
Install
-
pip install pyfunctional
Imports
- seq
from functional import seq
- pseq
from functional import pseq
Quickstart
from functional import seq
# Create a sequence from a range of numbers
result = seq(range(10))
.map(lambda x: x * x) # Square each number
.filter(lambda x: x > 10) # Keep numbers greater than 10
.reduce(lambda x, y: x + y, 0) # Sum the remaining numbers, starting with 0
print(result)
# Expected output: 285