or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--flip

Knowledge tile focusing on lodash's flip function which creates a function that invokes the provided function with arguments reversed

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash@4.2.x#flip

To install, run

npx @tessl/cli install tessl/npm-lodash--flip@4.2.0

index.mddocs/

Lodash flip Function

This 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.

Package Information

  • Package Name: lodash
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash

Core Imports

import _ 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;

Basic Usage

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)

Capabilities

Function Argument Reversal

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 for

Returns:

  • (Function): Returns the new function that invokes the original function with arguments in reverse order

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:

  1. API Compatibility: Converting functions to match different calling conventions
  2. Functional Programming: Creating point-free style functions with different argument orders
  3. Partial Application: Enabling partial application where the "primary" argument comes later
  4. Callback Pattern Conversion: Converting between different callback patterns (error-first vs success-first)

Architecture

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:

  • The flip function creates a wrapper that reverses all arguments passed to it
  • The original function's this context is preserved in the flipped version
  • Works with functions that accept any number of arguments
  • Returns a new function; does not modify the original function
  • Part of lodash's broader function manipulation utilities alongside curry, partial, rearg, etc.