fnc - Functional Programming Utilities
fnc is a Python library that provides functional programming utilities, primarily focusing on working with generators and iterable data. It offers curried versions of common functions like `map`, `filter`, and `pipe` for a more declarative style. The current version is 0.5.3, released in October 2021, and the project is in a maintenance mode with infrequent updates.
Common errors
-
TypeError: 'map' object is not iterable
cause This often occurs when you forget to materialize the result of an `fnc` operation, which returns a generator. For example, trying to iterate over `map(...)` directly without `list()` or `tuple()`.fixEnsure the final step in your functional chain or direct `fnc` call includes a materialization step, such as `list()`, `tuple()`, or `set()`. Example: `result = list(pipe(data, ...))`. -
TypeError: 'builtin_function_or_method' object is not callable
cause This can happen if you import `fnc.map` or `fnc.filter` but then attempt to use them with the syntax for Python's built-in `map` or `filter` (i.e., `map(func, iterable)` instead of `map(func)(iterable)` or `pipe(iterable, map(func))`). Python's built-in `map` function gets shadowed, and `fnc.map` is a curried function requiring a different call pattern.fixUnderstand that `fnc.map` and `fnc.filter` are curried. Use them in a `pipe` operation or by first providing the function argument, then the iterable: `fnc_map_double = map(lambda x: x * 2); result = list(fnc_map_double(data))`.
Warnings
- gotcha fnc's `map` and `filter` conflict with Python's built-in `map` and `filter`. The `fnc` versions are curried, meaning they expect to be called with arguments in stages (e.g., `map(func)(iterable)` or used within `pipe`), unlike the built-in versions which expect `map(func, iterable)`.
- gotcha Most `fnc` utilities return generators or iterators. For immediate evaluation and to see concrete results, you must explicitly convert them to a list, tuple, or other materializable collection (e.g., `list(result)`).
- gotcha The `fnc` library is in maintenance mode; the last release (0.5.3) was in 2021. While stable, active development and new features are not frequent. Consider this for long-term project planning or if cutting-edge functional features are required.
Install
-
pip install fnc
Imports
- pipe
from fnc import pipe
- map
map(lambda x: x*2, [1,2,3]) # using fnc.map without currying or pipe
from fnc import map
- filter
filter(lambda x: x>0, [1,0,-1]) # using fnc.filter without currying or pipe
from fnc import filter
- curry
from fnc import curry
Quickstart
from fnc import pipe, map, filter, take
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Example 1: Use pipe for a functional chain
result_pipe = pipe(
data,
map(lambda x: x * 2), # Double each number
filter(lambda x: x > 10), # Keep only numbers greater than 10
take(3), # Take the first 3 results
list # Materialize the generator into a list
)
print(f"Result with pipe: {result_pipe}") # Expected: [12, 14, 16]
# Example 2: Curried map and filter manually
double = map(lambda x: x * 2)
odd = filter(lambda x: x % 2 != 0)
processed_data = double(odd(data)) # Apply odd filter, then double
print(f"Manual curried chain: {list(processed_data)}") # Expected: [2, 6, 10, 14, 18]