Categories
Lodash

Learning JavaScript by Implementing Lodash Methods — Playing With Function Arguments

Spread the love

Lodash is a very useful utility library that lets us work with objects and arrays easily.

However, now that the JavaScript standard library is catching up to libraries such as Lodash, we can implement many of the functions in simple ways.

In this article, we’ll look at how to implement more Lodash methods that are used to manipulate function arguments.

flip

The Lodashy flip method applies the arguments to a function in reverse order.

We can easily implement this ourselves with the spread operator. For instance, we can create our own flip method as follows:

const flip = (fn) => (...args) => fn(...args.reverse())

In the code above, we created the flip function by getting the fn parameter, which is a function.

Then we return a function with the args array and then return fn with the args applied in reverse by calling args.reverse() and then use the spread operator to apply the arguments.

We can then call it as follows:

const flipped = flip((...args) => args);

And then when we call the flipped function as follows:

flipped('a', 'b', 'c', 'd')

We then see that the returned value is [“d”, “c”, “b”, “a”] .

negate

The Lodash negate method that returns a function that negates the predicate that’s passed into the negate method.

For instance, we can write the following code:

const negate = (predicate) => (...args) => !predicate(...args)

In the code above, we created the negate function by making a function that takes in a predicate and then return a function that takes in the args and then return the predicate called with args spread by the spread operator negated.

Then we can call it as follows:

const isEven = (n) => n % 2 == 0;
[1, 2, 3, 4, 5, 6].filter(negate(isEven))

We called by first defining an isEven function that has the condition that the n % 2 == 0 , which returns true if the number is even.

Then we can used it with the filter method as follows:

[1, 2, 3, 4, 5, 6].filter(negate(isEven))

We negated the isEven method’s returned value with negate , so the returned numbers are odd numbers.

partial

The Lodash partial method returns a function that takes a function with the arguments partially applied to it. Then we can call that returned function with more arguments.

For instance, we can create our own partial method with the following code:

const partial = (fn, ...partialArgs) => (...args) => fn(...partialArgs, ...args)

In the code above, we created a function that takes fn , which is a function, and partialArgs , which are some of the arguments that are called. Then we return a function that takes more arguments and calls fn with the partialArgs and args spread within the fn .

Both partialArgs and args are applied to fn with the spread operator.

Then we can call it as follows:

const greet = (greeting, name) => {
  return `${greeting} ${name}`;
}

const sayHelloTo = partial(greet, 'hello');
console.log(sayHelloTo('joe'));

We defined the greet function, which takes 2 arguments. With our partial function, we returned a function with the first greeting argument and then returns the function with the given argument applied and set it to sayHelloTo

Then we call sayHelloTo and with the 2nd argument and then we can see from the console log that it’s hello joe as we expected.

partialRight

The partialRight method is like partial , except that we apply the arguments from right to left instead.

We can just change partial slightly to create the partialRight function as follows:

const partialRight = (fn, ...partialArgs) => (...args) => fn(...args, ...partialArgs)

In the code above, we just flipped the partialArgs and args order.

Then we can call it as follows:

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

const greetFred = partialRight(greet, 'joe');
console.log(greetFred('hi'));

We defined the greet function with the greeting and name arguments and then we applied the arguments in reverse order.

Then we get that console log outputs 'hi joe' .

Conclusion

The flip method returns a function that has the arguments applied in reverse. We can implement that easily with the spread and rest operators.

Lodash’s negate method returns a function that negates the result of the given predicate function. We can just return a function that negates the returned result of the predicate function.

The partial and partialRight methods return a function that’s partially applied partially and takes more arguments that’s applied in addition to the partially applied arguments.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *