UniCoreFW

over_args()

Create a function that applies transformations to arguments before calling func.

Implementation

Args: func: The function to invoke *transforms: Functions to transform corresponding arguments Returns: A function that transforms arguments before calling func

Example

def add(a, b): return a + b square = lambda x: x * x double = lambda x: x * 2 transform_add = over_args(add, square, double) transform_add(3, 4) # (3*3) + (4*2) = 9 + 8 = 17

Expected output: 17

Source Code

def over_args(func: Callable, *transforms: Callable) -> Callable: _validate_callable(func) # Handle case where transforms are passed as a single tuple/list if len(transforms) == 1 and isinstance(transforms[0], (list, tuple)): transforms = transforms[0] # type: ignore for transform in transforms: _validate_callable(transform) def wrapper(*args, **kwargs): transformed_args = [] for i, arg in enumerate(args): if i < len(transforms): transformed_args.append(transforms[i](arg)) else: transformed_args.append(arg) return func(*transformed_args, **kwargs) return _copy_function_metadata(wrapper, func)