Friday, September 27, 2013

From imperative to functional in Java 8: paper review

I have been writing functional programs for thirty years, first in KRC, then in Miranda, and more recently in Haskell and Erlang. In the past few years, though, something rather remarkable has been happening: the essentials of functional programming have been popping up in more “serious” mainstream languages: JavaScript, C#, C++ and now, with the advent of the 8th version of the standard, Java. What is this functional core that’s coming into these languages: the ability to compute functions as a part of the work of a program, and to pass these around as parameters or return values of other functions: closures as “first-class citizens” in Strachey’s words.

But why is this happening? As the authors of this fine paper explain, it is to exploit the parallelism that is now a feature of some many computing platforms, from smartphones and tablets, through laptops and desktops to servers and supercomputers. To give one example: mapping a function across a collection data structure is one of the key parallel patterns, providing the data analogue of a linear (control) pipeline.

Given the opportunity, how can we program using these new features? One possibility is to start from scratch with these new constructs, but a more attractive option is to refactor existing code to exploit these features: that is precisely the topic of this paper. The authors show how refactoring opportunities are identified, and what transformations need to be performed. Moreover, they anatomise some of the attendant difficulties: how are side-effects to be handled? how to deal with different scoping regimes between anonymous inner classes and closures? Their work is validated through a set of substantial case studies, and made available through the standard Net Beans distribution.

I would recommend this paper to anyone interested in exploiting the parallelism inherent in many standard programs, as well as anyone who would like to see a justification for machine-supported refactoring. Finally it is a remarkable vindication of getting students involved in research: two of the four authors took part as undergraduate summer interns.

This is a draft review for Computing Reviews of Crossing the Gap from Imperative to Functional Programming through Refactoring.

No comments:

Post a Comment