0
# Lodash flip Function
1
2
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.
3
4
**Note**: This tile documents only the `flip` function from the lodash library. For complete lodash documentation, refer to the [official lodash documentation](https://lodash.com/docs).
5
6
## Package Information
7
8
- **Package Name**: lodash
9
- **Package Type**: npm
10
- **Language**: JavaScript
11
- **Installation**: `npm install lodash`
12
13
## Core Imports
14
15
```javascript
16
import _ from "lodash";
17
```
18
19
For CommonJS:
20
21
```javascript
22
const _ = require("lodash");
23
```
24
25
For specific function import (recommended for modern bundlers):
26
27
```javascript
28
import flip from "lodash/flip";
29
```
30
31
For ES6 modules with default import:
32
33
```javascript
34
import lodash from "lodash";
35
const flip = lodash.flip;
36
```
37
38
## Basic Usage
39
40
```javascript
41
import _ from "lodash";
42
// Alternative: import flip from "lodash/flip";
43
44
// Create a function that expects arguments in a specific order
45
function greet(greeting, name) {
46
return `${greeting}, ${name}!`;
47
}
48
49
// Create a flipped version that reverses argument order
50
const flippedGreet = _.flip(greet);
51
52
// Now we can call with name first, greeting second
53
flippedGreet("World", "Hello");
54
// => "Hello, World!"
55
56
// More practical example with array operations
57
function divide(a, b) {
58
return a / b;
59
}
60
61
const flippedDivide = _.flip(divide);
62
divide(10, 2); // => 5 (10 divided by 2)
63
flippedDivide(2, 10); // => 5 (10 divided by 2, arguments flipped)
64
```
65
66
## Capabilities
67
68
### Function Argument Reversal
69
70
Creates a function that invokes the provided function with arguments in reverse order.
71
72
```javascript { .api }
73
/**
74
* Creates a function that invokes `func` with arguments reversed.
75
*
76
* @param {Function} func - The function to flip arguments for.
77
* @returns {Function} Returns the new function.
78
*/
79
function flip(func);
80
```
81
82
**Parameters:**
83
- `func` *(Function)*: The function to flip arguments for
84
85
**Returns:**
86
- *(Function)*: Returns the new function that invokes the original function with arguments in reverse order
87
88
**Usage Examples:**
89
90
```javascript
91
// Basic argument flipping
92
const flipped = _.flip(function() {
93
return _.toArray(arguments);
94
});
95
96
flipped('a', 'b', 'c', 'd');
97
// => ['d', 'c', 'b', 'a']
98
99
// Practical use with callbacks
100
function processData(data, callback) {
101
// Process data and call callback with result
102
const result = data.map(x => x * 2);
103
callback(null, result);
104
}
105
106
// Some APIs expect callback-first patterns
107
const callbackFirst = _.flip(processData);
108
callbackFirst(myCallback, [1, 2, 3]);
109
// Equivalent to: processData([1, 2, 3], myCallback)
110
111
// Useful for partial application
112
const subtract = (a, b) => a - b;
113
const flippedSubtract = _.flip(subtract);
114
115
const subtractFrom10 = _.partial(flippedSubtract, 10);
116
subtractFrom10(3); // => 7 (equivalent to 10 - 3)
117
```
118
119
**Common Use Cases:**
120
121
1. **API Compatibility**: Converting functions to match different calling conventions
122
2. **Functional Programming**: Creating point-free style functions with different argument orders
123
3. **Partial Application**: Enabling partial application where the "primary" argument comes later
124
4. **Callback Pattern Conversion**: Converting between different callback patterns (error-first vs success-first)
125
126
## Architecture
127
128
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.
129
130
```javascript
131
// Internal implementation concept (simplified)
132
function flip(func) {
133
return createWrapper(func, FLIP_FLAG); // FLIP_FLAG = 512
134
}
135
136
// When wrapper is called:
137
if (isFlip && args.length > 1) {
138
args.reverse();
139
}
140
```
141
142
**Notes:**
143
144
- The flip function creates a wrapper that reverses all arguments passed to it
145
- The original function's `this` context is preserved in the flipped version
146
- Works with functions that accept any number of arguments
147
- Returns a new function; does not modify the original function
148
- Part of lodash's broader function manipulation utilities alongside `curry`, `partial`, `rearg`, etc.