Functional alternatives of your everyday tools

I am programming on regular basis more than 4 years now. For almost 2 years i am committed to functional programming, Scala being my language of choice. At my first workplace i experienced conflict between functional and objective oriented program design, and tools, which is the reason for writing this blogpost. Some basic knowledge of scala language is recommended to fully understand this post.



Before going any further let’s consider meaning of this in context of programming language and software design. Frequently the following can be observed: Simple languages require complex solutions to solve problems, which are simple to solve in (some)complex languages. I want to show this on the several commonly used functionalities, how Scala which is arguably very complex language can solve problems in a very simple way.

Resource management

There is one feature of Java language i discovered not so long a go. That try block can automatically manage a resource:

It turns out that Scala is missing this functionality, but one can easily implement it: 


Server-side authorization

Let’s consider the following code, it’s extract from one of my projects implementing authorization of a user in PlayFramework application. It does not require any support from the framework itself, it is just pure Scala code, taking advantage of some of its features:


Disclaimer: all plumbing regarding database access, data extraction from headers and all other framework and domain related things is omitted. We would still need to implement in any framework and language, so let’s just assume it is just somehow done.
So what is happening in the following snippet of code? In play framework request handling is done via functions: Request => Future[Response]. Scala comes with plenty of ways to manipulate functions, one can easily pass them around so the following function should also be possible: (Request => Future[Response]) => (Request => Future[Response])
That is exactly the shape of userLoggedIn.async which modifies passed function to accept only authorized users.
Second approach is also shown in this snippet, function modification can be performed with withPermissions method, simply by wrapping its body.

Client side authentication

Recently i needed to integrate with some external service, it uses OATH authentication, which means that tokens have to be refreshed once in a while and all that good stuff. It is also required to pass specific parameter when authorizing. There is also the following demand: http client i could use exposed the following method:

Being used to solving problems in functional manner, i was faced with the following difficulty:
How to solve the issue of expiring tokens. Functional features of scala language came to the rescue. I decided to store keys in some secure storage. This way my service should work without unnecessary token refreshing even in case of failure. Implementation:

Method refreshToken is the one communicating with OAUTH endpoint, upon completion it returns fresh access token. Call site of the following is as simple as that:

I decided to do it this way, so authorized looks like a special keyword (thus i think it improves readability), but it is also be possible to make it look like the following:


Asynchronous code

In the previous examples i demonstrated simple, (almost) framework agnostic implementation of commonly used functionalities. Both of them had the following in common: It was an example of asynchronous code, utilizing futures to some extend. Now i will finally present some alternatives from not-functional world and show that good abstractions matter.
You are probably familiar with the term “Callback Hell”, describing problems which emerged from using callbacks as a way of dealing with asynchronous computation. There are several solutions, one of the most popular is async-await syntax. Let’s consider the following example (taken from here):

Now it is time to show functional Scala alternative, unfortunately scala standard library does not provide any way to schedule asynchronous computation (Futures used previously are eager, executing as soon as they are created). This is why i am going to use Monix library and its Task. The code look like this:

Scala code has one important difference from the javascript one, Task is lazy, so nothing actually happens, until runOnComplete method is invoked. Other than that it should be functionally equivalent. But aside from being typesafe (and shorter!) main difference is the way of composing asynchronous computation. In reality Scala code is actually using a very simple mechanism, compared to async-await from javascript.
The so-called “for comprehension” is just a syntactic sugar for chaining flatMap method invocations, so function add1 is equivalent to the following:

On the other hand async-await has to use some internal state machine, and generate quite a lot of code which ends up doing nothing more then chaining asynchronous computation.


The main point of this post is to show that given well thought syntax of a language implementation of tools can be concise and simple. It happens that functional languages are the ones offering this kind of flexibility. “For comprehension” in Scala is in many ways similar to “do” keyword in Haskell. The same goes with multiple lists of parameters (in reality it is something else in Haskel, but this topic is beyond this blog post). This feature in combination with functions allows a Programmer to write ones own control structures and extend language with ease.


by Łukasz Lampart, Scala Developer at Reality Games.