09. Sep 2012 12:09
No comments

The Joys of functional programming in Scala

a box showing x as input resulting in f(x)
a box showing x as input resulting in f(x)

In this article we will go through some of the basic syntax of functions in Scala and functions as first class citizens in Scala and Clojure. We will compare the syntactic concepts with their decompiled Java and comparable Java concepts. It concludes in a comparison of decompiled closures in Scala and Clojure.

Going with the trends in programming, as a friend of VM based languages, will mean that there is no going around the hype that is Scala. One can harshly summarize it into an interesting mixture of paradigmes with an at times amusingly huge choice of syntax. All prejeduce aside it is in fact great joy to wildly mix common methodologies of both paradigmes, whilst fully ignorant towards the extremists believes of either side.

Perhaps it is nothing new to the python programmer or others spending their daily programming in an interpreted language both functional and object oriented, but for those coming from a Java background it can be great to finally have an excuse to make use of those features otherwise considered unmaintainable and possibly undesirable

Function syntax in Scala

On first sight a vast amount of its functional world appears a mangeling and mixing of endlessly nested inner classes, strangely repetitive API's defining only slightly different function types, and endless different amounts of syntax to make the exact same statement. However its concepts of functions as objects and thus first class members, with their underlying implementation being an annonumous inner class overriding an apply method of their implemented interface, strangely interesting possibilities open up. Enough for words, lets see some of that syntax, whilst slowly fading away the magic...

...all three of the above decompile into the exact same java code...

So the whole thing isn't too far away from...

Great! now with that thinking we should be able get through some of that syntactic beauty. Thinking back to some of the things mumbled about earlier the very next thing to try is first class functions, which in lesser fancy words simply means to assign and pass around functions in variables

Functions as first class citizens in Scala

To declare a function that takes a function we can use the same notation as seen above to define a function type so (Input) => Return however nesting all those lovely equal arrows quite quickly becomes a little messy...

as we've come this far, we might as well imagine we want a function that returns a function rather than calling it and returning its return...

and for the sake of looking at some more of those pretty brackets and arrows, a function that gets a function which got a function and the whole thing returning a function...

Joking aside, it may seem over the top at first but you may get to a point where arranging your functions becomes so repetitive that you want to create a function doing so instead. 

Just so that we can say this article has actually got some useful background, here a little example of what can commonly be done with functions as the above (rather the first one than the overly brackety ones). Below we take two intiger and a function as input, the function we expect has to take two integer and return an integer.

Closures in Scala and Clojure decompiled into Java 

We'll keep it simple to make their equivalents more readable and actually be able to read the decompilations. First of all a simple closure in Scala...

We'll leave out the type declarations, so we'll need a bit of imagination to fill the gaps (this is simply to avoid cluttering this article with even more uninteresting code snippets). Below the above decompiled into Java...

As already hinted at earlier, most of the magic here is as expected done via nested annonumous inner classes. The complication lies in preventing the nested class from altering the state of the outer class. To do so one requires a little trickery keeping the state in question immutable as mostly seen above. Now lets see the same as the above, i.e. a simple closure in Clojure...

If we now compile the above ahead of time using (compile 'simple) and decompile the results into java, we'll see...

We see again that functions are boiled down to nested classes but their calling and the general seperation of states is a little more convoluted than the previously seen bytecode decompiled from the scala closure. If you want to delve in deeper into some of the snippets seen above all the tools we used are free. To decompile the different bytecode classes we used the simple tools provided by http://java.decompiler.free.fr/ and for the coding itself we used the Eclipse plugin for Scala and Counterclockwise for Clojure.

Books about the topic „Scala“

The following books are all about the topic "Scala" and are highly recommended. I have not read all of these books, but a good number of them and some of them I used for my research as well.
£15.46
Order now »
Functional Programming in Scala
Paul Chiusano, Manning Publications
£23.94
Order now »
Programming in Scala
Martin Odersky, Artima Inc
£15.46
Order now »
Scala for the Impatient
Cay S. Horstmann, Addison Wesley
£18.66
Order now »
Programming Scala: Scalability = Functional Programming + Objects
Dean Wampler, O'Reilly Media

This article might also interest you.

Visitors who read this article, also read the articles below. Just take a look at them and maybe you find them interesting.
15 Visitors also read this
9 Visitors also read this
8 Visitors also read this
5 Visitors also read this
4 Visitors also read this

Comments about the topic „The Joys of functional programming in Scala“

If you like you can leave a comment about the topic and exchange with other reads. In order to comment you need to login and then you can start immediately.
Login now to comment