1. Functional programming

In computer science, functional programming is a popular and important programming method. However, in Vietnam and many other countries, students that major in computer science and IT are rarely taught this from the beginning. Normally, they will be taught imperative programming – with two characteristic styles called procedural programming and object-oriented programming. In fact, most familiar programming languages like C, C++, Java, Python… are first designed in accordance with imperative programming, before being integrated with functional programming features in later updates.

This article will provide a brief and simple introduction for complete beginners without any knowledge in functional programming. The only requirement is that you will need basic programming skills.

Now, the core problem of functional programming is to regard the many structures and calculations of programs as mathematical functions, as well as avoid altering data status.

In Math, we often write $$y = f(x)$$, which implies $$y$$ is a function of $$x$$: should $$x$$ be the data, $$f$$ be the calculation, then $$y$$ is the result of that calculation. Calculating $$f(x)$$ thus does not change $$x$$, and there shall be only one value of $$y$$ for every value of $$x$$. Similar results will occur if we apply $$f$$ function on the same $$x$$ data. This will prevent side effects, which are often seen in non-functional programming languages.

The recent years have seen the rise of usage of more and more functional programming languages for both academic and industrial purposes. Some languages support multiple programming styles simultaneously, for example, full support of imperative programming, object-oriented programming, and functional programming. Notable languages with this ability include Scala, Python, and JavaScript.

2. Scala Programming Language

Scala is a programming language that runs on a Java basis, which supports both object-oriented and functional programming, with much shorter semantics than Java (comparable with Python). There are many benefits to using Scala, with many popular distributed platforms and big data processors written using this language like Apache Spark.

3. Simple examples

Now, let us go into some small examples of using Scala in functional programming.

3.1. Finding the sum to a set of natural numbers

Scala’s popular data structures are preset with common functions, or in this case, is to find the sum. For example, the below code declares a set of natural number, then add them up using the sum function, and print the results to the screen:

It can be seen that in this code, $$x$$ is a set, $$f$$ is the sum function, and $$y$$ is the result, or $$y = f(x)$$. To get the same result in Java, we will need to write a loop, initiate variable s, and add individual numbers to the variable.

3.2. Finding the sum to a set of squares of natural numbers

Using the same set of numbers as the above example, we now will want to calculate:

$$s = 1^2 + 3^2 + 5^2 + 7^2 + 9^2$$

Mathematically, here we will need two functions, one to calculate the square of each number, and one to add the squares up. First, we will need a map function to turn numbers of the set into squares, before adding them using the sum function:

In Mathematics, this is called a combination of functions, or a functional composition of $$y = g (f(.))$$, where $$f$$ and $$g$$ are functions, and the $$=>$$ is the transformation of functions, where each $$x$$ value is transformed to a respective $$x^2$$ value.

3.3. Filter elements that meet specific requirements

Continuing form example 1, if we want to filter elements equal to or more than 5 from xs, then we only need to use a filter function as following:

The first command will create ys from xs, with only numbers equal to or more than 5. The second command on the other hand, will print these numbers on the screen. We can also make the first command more brief with the use of an underscore as followed:

Here, the underscore represents every number in xs. Notice that the parameters of a filter function is basically a logical function with true or false values representing the filter conditions, where only satisfying numbers in xs remains. All used functions since the beginning (sum, map, filter) do not change any part of the original xs data.

4. Conclusion

This short article briefly introduces functional programming and Scala programming languages. There are many more important and sophisticated concepts to functional programming like high order functions, recursion, pure functions… all listed in the image at the beginning of the text. The following articles (if any) will explore more and layout examples regarding these concepts.

Le Hong Phuong
Source: Ereka

Related posts: