The flip
function from Lodash creates a new function that invokes the original function with its arguments reversed.
Here's an example of how to use it in JavaScript:
212 chars10 lines
In this example, we define a function called subtract
that subtracts b
from a
. We then create a new function called flippedSubtract
using lodash.flip
, which takes our subtract
function as an argument. When we call flippedSubtract
with arguments 5
and 3
, it calls subtract
with arguments 3
and 5
, reversing the order of the arguments. This results in the value -2
being returned. Similarly, when we call flippedSubtract
with arguments 3
and 5
, it calls subtract
with arguments 5
and 3
, also reversing the order of the arguments. This results in the value 2
being returned.
gistlibby LogSnag