How Lodash partialRight Appends Arguments

The _.partialRight method in the Lodash JavaScript library creates a partially applied function where pre-specified arguments are appended to the end of the argument list instead of the beginning. This article explains how _.partialRight functions, how it structures argument order during execution, and how to effectively use it with code examples and placeholder mechanics.

Understanding _.partialRight

In functional programming, partial application is the process of fixing a number of arguments to a function, producing another function of smaller arity. While standard partial application (like Lodash's _.partial) binds arguments from the left, _.partialRight binds arguments from the right-hand side.

When you create a function using _.partialRight(func, [partials]), Lodash stores the provided partial arguments. When the resulting wrapper function is later invoked with runtime arguments, Lodash places the runtime arguments first, followed immediately by the partially applied arguments.

Basic Syntax and Execution

The syntax for _.partialRight is:

_.partialRight(func, [partials])

Consider a simple greeting function that expects two parameters:

const _ = require('lodash');

function greet(greeting, name) {
  return `${greeting}, ${name}!`;
}

// Partially apply the 'name' argument from the right
const greetFred = _.partialRight(greet, 'Fred');

// Provide the 'greeting' argument at call time
console.log(greetFred('Hello')); 
// Output: "Hello, Fred!"

In this example, 'Fred' is anchored to the second parameter (name). When greetFred('Hello') is called, 'Hello' fills the first open parameter (greeting), resulting in greet('Hello', 'Fred').

Handling Multiple Arguments

When multiple arguments are supplied at both creation time and invocation time, _.partialRight maintains their relative order while placing the invocation arguments before the preset arguments.

function formatPath(domain, apiVersion, endpoint) {
  return `https://${domain}/${apiVersion}/${endpoint}`;
}

// Preset apiVersion and endpoint
const getUsersEndpoint = _.partialRight(formatPath, 'v1', 'users');

// Provide domain at invocation
console.log(getUsersEndpoint('example.com'));
// Output: "https://example.com/v1/users"

The call sequence evaluates as follows:

  1. Runtime arguments are collected: ['example.com']
  2. Preset arguments are collected: ['v1', 'users']
  3. Lodash merges them in order: ['example.com', 'v1', 'users']
  4. The target function executes with the combined array.

Using Placeholders

Lodash allows the use of its identity object (_) as a placeholder to reserve specific positions within the appended arguments.

function sendEmail(to, subject, body) {
  return `Sending "${subject}" to ${to}: ${body}`;
}

// Fix 'body', leave 'subject' dynamic via placeholder
const notifyAdmin = _.partialRight(sendEmail, _, 'System reboot scheduled.');

console.log(notifyAdmin('admin@example.com', 'Alert'));
// Output: "Sending "Alert" to admin@example.com: System reboot scheduled."

When a placeholder is encountered, Lodash maps the runtime arguments to fill open slots from left to right, filling placeholders first before appending remaining arguments.

Key Differences Between _.partial and _.partialRight

This distinction makes _.partialRight ideal for adapting functions designed with callback-last interfaces or functions where configuration parameters naturally belong at the end of the argument signature.