flow_right()
Create a function that is the composition of functions, executed right to left.
Implementation
Args: *functions: Functions to compose Returns: A function that applies all functions in reverse sequence
Example
double = lambda x: x * 2
add_one = lambda x: x + 1
transform = flow_right(double, add_one)
transform(5) # add_one(5) then double(6) = 12
Expected output: 12
Source Code
def flow_right(*functions: Callable) -> Callable:
for func in functions:
_validate_callable(func)
if not functions:
return lambda x: x
def composed(*args, **kwargs):
result = functions[-1](*args, **kwargs)
for func in reversed(functions[:-1]):
result = func(result)
return result
# Set _argcount for compatibility with tests
composed._argcount = functions[-1].__code__.co_argcount if functions else 0 # type: ignore
composed.__name__ = f"flow_right({', '.join(f.__name__ for f in functions)})"
return composed