juxtapose()
Create a function that applies multiple functions to the same arguments.
Implementation
Args: *functions: Functions to apply in parallel Returns: A function that returns a list of results from all functions
Example
first_char = lambda s: s[0]
last_char = lambda s: s[-1]
both_ends = juxtapose(first_char, last_char)
both_ends("hello")
Expected output: ['h', 'o']
Source Code
def juxtapose(*functions: Callable) -> Callable:
for func in functions:
_validate_callable(func)
def parallel(*args, **kwargs):
return [func(*args, **kwargs) for func in functions]
# Set _argcount for compatibility with tests
parallel._argcount = max((f.__code__.co_argcount for f in functions), default=0) # type: ignore
parallel.__name__ = f"juxtapose({', '.join(f.__name__ for f in functions)})"
return parallel