In this second article, we will continue to explore some concepts and examples on functional programming. Explanations will be kept brief to keep you from getting confused or bored. However, at the end of the article, there will be a small question for those that favor challenges.

Similar to the first article, the programming language used will be Scale – with rather similar syntax to Java or Python, Scale is easier for programmers to approach, and understandable for readers with basic programming knowledge, even without any experience in the language.

1. First-class function

In Scala as well as in other functional programming languages exists the concept of first-class function, which, aside from the ability to declare functions like in imperative programming languages, can be treated as data types in Scala and used in other functions.

For example, if we want to add 1 to a natural variable number in Scala, we can write:

Per the previous article, the arrow symbolizes function transformation from $$x$$ to $$x + 1$$. This particular function today remains unnamed, and is a one-variable function. Of course, nothing is stopping you should you find naming this function necessary.

Now, we shall declare a functional variable the same way we assign a $$T$$ -type variable in other familiar programming languages, as follows:

then, we name the $$f$$ function with one particular parameter $$x$$, for example $$f(10)$$, and receive 11 as a result. Here, the function will add 1 to the parameter, so for better visualization in a long program or in programming of real products, we should name the function “increase”, instead of $$f$$.

However, calculation functions in reality are often much more complex compared to the aforementioned. There, we can write the function’ logic as we want, similar to the body of normal programming, for example:

With $$f(3)$$, we will get the result of 35. Please notice that in Scala, getting results does not require the use of keyword “return”. The final command in the function body will be calculated, and the result shall be returned nevertheless.

2. Utilizing functions

Assume that we have a list of data of natural numbers as follows:

and we want to calculate on these data with the function $$f$$, so that the result satisfy some particular condition, for example, lower than 500.

Solving this with a similar mindset to the previous article will require 2 steps:

• First, apply function $$f$$ to individual component of list $$xs$$, we will get a new list;
• Then, filter the new list and keep numbers that satisfy the filter condition (in this case a function of (x: Int) => Boolean.)  This function can be the logarithm to the filter function.

This can be done by a chain of corresponding map and filter commands:

We can simplify with the “_” symbol, as introduced in the first article:

The result will be $$List(7, 35, 87, 163, 263, 387)$$

In the aforementioned examples, for simplifications, we can use first-class functions, with resulting functions being just as simple. At the same time, functions can also include more than one variables, work with 2 and above variables, and return results of some particular types. For example, this is the function with 2 variables, with the end result being a sequence string and a list of natural numbers.

3. Quite-the-confusing example

Now, if you had managed to understand the above basic problems, you can search up popular functions on popular data types in Scala, like List, Map, (also called Python dict)…

Here’s a small challenge. What can the below Scala code do, and what will be the result?

Of course, getting the result is simple: you just need to copy and paste the code in to a Scala translator. However, it is more important to know how each function works.

And finally, after understanding the purpose of the above Scala and functional programming style, let’s try doing something similar with an imperative programming language like Java. Then, you will see why many Scala users hesitate to go back to Java, unless obliged to do so.

Le Hong Phuong
Source: ereka

Related posts: