Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Improve JavaScript Function Usability with Proper Argument Order in Functional Programming

    Kyle ShevlinKyle Shevlin
    javascriptJavaScript

    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.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: 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.

    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.

    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.

    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.

    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.

    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.

    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.

    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.

    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.

    Discuss

    Discuss