functional-streams

raw JSON →
1.6.4 verified Mon Apr 27 auth: no python

A Python library that provides functional programming stream operations similar to Java streams, enabling declarative, concise data transformations via lazy evaluation, fluent chaining, and common operations like map, filter, reduce, and more. Current version 1.6.4, supports Python >=3.6, with moderate release cadence (minor updates every few months).

pip install functional-streams
error AttributeError: module 'functional' has no attribute 'streams'
cause Incorrect import: trying to import 'functional.streams' directly via 'import functional.streams' and then using functional.streams.Stream.
fix
Use 'from functional.streams import Stream'.
error Stream(...).filter(...) returns <functional.streams.Stream object at 0x...> instead of the expected result
cause Forgetting to call a terminal operation. The stream is lazy and only returns the stream object until a terminal operation is invoked.
fix
Add a terminal operation like .to_list() or .reduce() at the end of the chain.
gotcha Stream operations are lazy; a 'terminal' operation like to_list() or sum() is required to trigger evaluation. Without a terminal operation, nothing will execute.
fix Always call a terminal operation (to_list(), first(), reduce(), etc.) at the end of your stream pipeline.
gotcha Streams are single-use: once a terminal operation is called, the stream cannot be reused. Attempting to do so will raise a RuntimeError.
fix Create a new stream for each pipeline. If you need to reuse data, store intermediate results in a list.
deprecated The 'streams' module (lower-level) is considered deprecated in favor of 'functional.streams' (higher-level). Using old imports may lead to compatibility issues in future versions.
fix Use 'from functional.streams import Stream' instead of 'from streams import Stream'.

Create a stream from a list, filter even numbers, double them, and collect to list.

from functional.streams import Stream

result = (Stream([1, 2, 3, 4])
          .filter(lambda x: x % 2 == 0)
          .map(lambda x: x * 2)
          .to_list())
print(result)  # [4, 8]