Partial application

In simple terms, a function in Scala can declare several blocks of parameters. Thus, a partial application of a function in Scala either leaves at least one block without values or one parameter without a block. In this section, we'll only talk about the first case.

Actually, filling up the last parameter block will create another function that takes a number of block parameters and decreases them by one. Repeating this until no blocks remain will result in the whole function being applied.

As this Scala feature will extensively be used when creating a layout of our future templates, it's important to grasp this concept. Hopefully, it isn't hard; seriously!

Let's see an example of such a partially applied function:

Partial application

First of all, we defined a map of i18n messages where a map is conceptually the same as java.util.Map, that is, a key-value pair type.

Then we defined a function (showMessage) that is able to retrieve an internationalized message based on its key. We can see that the function name is followed by two blocks of one parameter each.

The next two vals are partially applied versions of this showMessage function, and they are actually vals but their value are functions. These versions are the common functions that are used to show messages either in English or in French.

The last two vals are also functions, but they are fully completed versions of showMessage, that is, with a complete set of parameters. Calling them will simply print the relevant messages without computing them any more.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset