How to use partially applied functions in Scala alvin March 7, 2018 - 8:16pm

Problem: You want to eliminate repetitively passing variables into a Scala function by (a) passing common variables into the function to (b) create a new function that’s pre-loaded with those values, and then (c) use the new function, passing it only the unique variables it needs.

Solution: The classic example of a partially applied function begins with a simple sum function:

This is a page from my book, Functional Programming, Simplified

Partially-Applied Functions (and Currying)


My motivations for writing this lesson are a little different than usual. Typically I think, “You’ll want to know this feature so you can use it like ___,” but the first motivation for this lesson goes like this: You’ll want to know about the concept of “currying” because experienced FP developers talk about it a lot, especially if they have Haskell programming experience. (I did mention that Haskell was named after Haskell Curry, didn’t I?)

Applying arguments to functions (currying)

I wish I had known more about Haskell and its influence on Scala before I wrote the Scala Cookbook. I don’t think that knowledge would have helped immensely, but it may have made the Cookbook a little better, maybe two to three percent better, something like that.

One area where more knowledge of Haskell would have helped is in regards to applying arguments to functions, i.e, currying. This page from the book Real World Haskell shows a nice example of how applying arguments to functions in Haskell works.