enhanced_curry_right()
Enhanced curry_right with explicit arity support and _argcount tracking.
Implementation
Args: func: The function to curry from right to left arity: Explicit arity (if None, inferred from function) Returns: A curried function that collects arguments from right to left
Example
def subtract(a, b, c):
return a - b - c
curried_sub = enhanced_curry_right(subtract)
result = curried_sub(1)(2)(10) # 10 - 2 - 1
result
Expected output: 7
Source Code
def enhanced_curry_right(func: Callable, arity: Optional[int] = None) -> Callable:
_validate_callable(func)
if arity is None:
arity = func.__code__.co_argcount
def curried(*args):
if len(args) >= arity:
# Collect the right number of args and pass them in normal order
collected_args = args[-arity:] if len(args) > arity else args
return func(*collected_args)
def next_curry(*more_args):
# Prepend new args to existing args (right-to-left collection)
return curried(*(more_args + args))
# Track remaining arguments needed
next_curry._argcount = arity - len(args) # type: ignore
return _copy_function_metadata(next_curry, func)
curried._argcount = arity # type: ignore
return _copy_function_metadata(curried, func)