Improve JavaScript Function Usability with Proper Argument Order in Functional Programming

Kyle Shevlin
InstructorKyle Shevlin
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 years ago

This lesson teaches you to order the arguments of your functions following functional programming principles. By ordering our arguments in a specific way, we allow our functions to benefit from partial application, improve reusability, and enable composition of functions. Specifically, we will focus on always providing the data as the last argument to our functions so that the result of one function can be piped as the argument into another function.

Instructor: [00:00] The order of arguments in non-curried functions is somewhat trivial, and a matter of preference. If I have a map function that receives an array and a callback, and uses it, because I receive all the arguments at the same time, it really doesn't make a difference if I change their order to be callback and array.

[00:18] I need both arguments in order for the function to work, and I can't create partially applied functions. However, if this is a curried function, the order of arguments makes a huge difference.

[00:29] Let's make a curried map function similar to Lodash's map function. We'll first perceive an array, and then a callback, and return array.map passing in the callback. If I now create an array and a function to use in the callback, if I create a partially applied function by supplying the array, we can see that while we can pass different callbacks to it and get the right result, we've locked in the data.

[00:57] The only thing we can change is the callback. This doesn't give us any extra utility than we get from just calling the map method directly on the array. If we change the argument order to receive the callback first and then the array second, we derive much more utility from this curried function.

[01:24] Now we can create a withDouble function that uses the map function and supplies the callback and awaits any array of numbers to double. For example, the array we already have or perhaps an array of even numbers.

[01:42] A useful way to think about curried functions is to order arguments from most specific to least specific argument. The least specific argument in every case is always going to be the data that could be our Boolean, a number, a string, an object, or an array, the primitives of your language.

[02:02] Take, for example, a prop function. Prop will be used to derive a value off an object by receiving a key and then an object, and returning the value stored at that key. I can now create partially applied functions that are designed to retrieve values out of any object.

[02:22] In this case I'll create a propName function with the specific argument of name. Now let's create a list of people, they'll be objects with name properties. I'm going to use the names of some of my friends.

[02:40] Now I can use my propName function which expects its data last in combination with my map function that expects its data last. If we log this out, we'll see an array of names.

egghead
egghead
~ 25 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today