Introduction to Elm, v2

Partial Application

Richard Feldman

Richard Feldman

Vendr, Inc.
Introduction to Elm, v2

Check out a free preview of the full Introduction to Elm, v2 course

The "Partial Application" Lesson is part of the full, Introduction to Elm, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Richard introduces the concept of partial application in Elm.


Transcript from the "Partial Application" Lesson

>> Richard Feldman: We can also use with a different function that's going to call our pluralized function from before. Here's how that might look. Same thing as before, except now we're taking a number. We're calling pluralize leaf, leaves, and num. And that's going to give us back 0 leaves, 1 leaf, and 2 leaves.

Now we've modified pluralize so that it concatenates the actual number on there, using string.fromint. What if I told you, you could also write the exact same logic this way instead, pluralize leaf leaves, and then 0, 1, 2. So this is the same code that we had before except I didn't use an anonymous function, I just left off an argument to pluralize.

And somehow, I'm getting the same result. So the reason this works is because this code actually defines the same structure in Elm. When I say pluralize leaf and leaves, I leave off that last argument. Elm says, I see that you've only partially called this function, you've only partially applied it.

You didn't give it all three arguments that it needs you left one off.
>> Richard Feldman: So in some languages when you do this, you have things like default arguments, you have things like, in JavaScript it will assume that that third argument is undefined and it'll execute the function as normal.

What Elm does is it says, okay, you didn't give me all the arguments I wanted, what I'm gonna do is I'm not gonna run pluralized yet. What we are doing is I'm gonna give you back an anonymous function that takes whatever arguments are remaining, and then sort of finishes the job.

So actually, anytime you see pluralize leaf and leaves, that is a partially applied function. And is going to evaluate not to actually running the function but it is going to evaluate to an anonymous function that takes the remaining arguments and then returns the result of finishing the job.

>> Richard Feldman: So that's why this code does the same thing as the completely fully written out version above. Anytime you call a function without this, that's just what you're going to get, those two are actually completely equivalent in Elm.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now