Knowledge tile focusing on lodash's flip function which creates a function that invokes the provided function with arguments reversed
npx @tessl/cli install tessl/npm-lodash--flip@4.2.0This knowledge tile focuses on lodash's flip function, which creates a function that invokes the provided function with arguments in reverse order. This utility is particularly useful for converting functions that expect arguments in one order to work with APIs that provide them in a different order.
Note: This tile documents only the flip function from the lodash library. For complete lodash documentation, refer to the official lodash documentation.
npm install lodashimport _ from "lodash";For CommonJS:
const _ = require("lodash");For specific function import (recommended for modern bundlers):
import flip from "lodash/flip";For ES6 modules with default import:
import lodash from "lodash";
const flip = lodash.flip;import _ from "lodash";
// Alternative: import flip from "lodash/flip";
// Create a function that expects arguments in a specific order
function greet(greeting, name) {
return `${greeting}, ${name}!`;
}
// Create a flipped version that reverses argument order
const flippedGreet = _.flip(greet);
// Now we can call with name first, greeting second
flippedGreet("World", "Hello");
// => "Hello, World!"
// More practical example with array operations
function divide(a, b) {
return a / b;
}
const flippedDivide = _.flip(divide);
divide(10, 2); // => 5 (10 divided by 2)
flippedDivide(2, 10); // => 5 (10 divided by 2, arguments flipped)Creates a function that invokes the provided function with arguments in reverse order.
/**
* Creates a function that invokes `func` with arguments reversed.
*
* @param {Function} func - The function to flip arguments for.
* @returns {Function} Returns the new function.
*/
function flip(func);Parameters:
func (Function): The function to flip arguments forReturns:
Usage Examples:
// Basic argument flipping
const flipped = _.flip(function() {
return _.toArray(arguments);
});
flipped('a', 'b', 'c', 'd');
// => ['d', 'c', 'b', 'a']
// Practical use with callbacks
function processData(data, callback) {
// Process data and call callback with result
const result = data.map(x => x * 2);
callback(null, result);
}
// Some APIs expect callback-first patterns
const callbackFirst = _.flip(processData);
callbackFirst(myCallback, [1, 2, 3]);
// Equivalent to: processData([1, 2, 3], myCallback)
// Useful for partial application
const subtract = (a, b) => a - b;
const flippedSubtract = _.flip(subtract);
const subtractFrom10 = _.partial(flippedSubtract, 10);
subtractFrom10(3); // => 7 (equivalent to 10 - 3)Common Use Cases:
The flip function is implemented using lodash's internal createWrapper function with a FLIP_FLAG bitmask. When the resulting function is called, lodash checks for the flip flag and reverses the arguments array before passing them to the original function.
// Internal implementation concept (simplified)
function flip(func) {
return createWrapper(func, FLIP_FLAG); // FLIP_FLAG = 512
}
// When wrapper is called:
if (isFlip && args.length > 1) {
args.reverse();
}Notes:
this context is preserved in the flipped versioncurry, partial, rearg, etc.