Calling Lodash Curry With No Arguments
When a function wrapped with Lodash's _.curry is invoked
with no arguments, Lodash evaluates the number of supplied arguments
against the target function's expected arity. If the target function
expects one or more arguments, calling the curried wrapper with no
arguments simply returns another curried function waiting for the
required inputs. This article explains how Lodash evaluates empty
invocations, how arity dictates the outcome, and how this behavior
affects code execution.
How Lodash Determines Execution
The _.curry method wraps a function and checks whether
the total number of supplied arguments is greater than or equal to the
target function's arity (func.length by default, or an
explicitly declared arity).
When the wrapper is called:
- It compares the number of provided valid arguments against the arity threshold.
- If the count meets or exceeds the arity, the original function executes with the accumulated arguments.
- If the count is lower than the arity, Lodash returns a new curried function that retains any previously accumulated arguments.
The Standard Case: Arity Greater Than Zero
For any function that expects at least one parameter, invoking the wrapper with no arguments provides an argument count of zero. Since zero is less than the required arity, the function does not execute. Instead, it returns a curried wrapper.
const _ = require('lodash');
const multiply = (a, b) => a * b;
const curriedMultiply = _.curry(multiply);
// Invocation with zero arguments
const result = curriedMultiply();
console.log(typeof result); // "function"Because returning the wrapper preserves internal state, repeated empty invocations can be chained indefinitely without throwing an error or executing the underlying logic:
const finalResult = curriedMultiply()()()(2)()(3);
console.log(finalResult); // 6The Edge Case: Zero Arity Functions
The only scenario where an empty invocation triggers execution is when the target function has an arity of zero (i.e., it declares no parameters) and no explicit arity was configured.
const getTimestamp = () => Date.now();
const curriedTimestamp = _.curry(getTimestamp);
// Invocation with zero arguments
const result = curriedTimestamp();
console.log(typeof result); // "number"Because getTimestamp.length is 0, the
threshold for execution is zero arguments. When invoked as
curriedTimestamp(), the provided argument count
(0) satisfies the required arity (0), causing
the original function to execute immediately.
Practical Implications
In standard functional programming workflows with Lodash, calling a
curried function with no arguments acts as a no-op that yields the
curried function itself. It does not reset accumulated arguments, nor
does it supply undefined to the underlying parameters. If a
call must explicitly pass undefined, it must be passed
intentionally as curriedFunc(undefined).