the blog for developers

Is this functional programming?

Recently I’ve read a blog post by Keith Dahlby. It was titled “Is Functional Abstraction Too Clever?”. The title sounded more rhetoric because clearly Keith was very satisfied with the code he presented.

But it got me thinking again. After some time with Lisp at university, some Haskell and some functional thinking in Ruby, I’m deep into Scala for some months. I’ve written some functional code before in Java, but I’m using much more with Scala now. Many examples are mostly thinking and syntactic sugar to me, not something I can put a finger on: this is functional.

Haskell examples clearly are more functional – how so? – and some blog authors give very nice functional solutions. Some blog posts by Raganwald and Debasish gave me great insights into functional composability. But many examples of people new to functional programming do not look functional to me.

Keith says about functional programming:

To me, this is the value proposition of functional programming. By expressing the algorithm in terms of common operations, we’re able to spend more time thinking about the problem than the details of the solution.

Let’s take a look at his code:

static int[] GetSlots(int slots, int max)
{
    return new Random().Values(1, max)
                       .Take(slots - 1)
                       .Append(0, max)
                       .OrderBy(i => i)
                       .Pairwise((x, y) => y - x)
                       .ToArray();
}

Looks more like a filter pipeline than functional programming. Especially compared for example to the things Apocalisp does with functional programming. Years ago I wrote a rendering pipeline, before the functional hype, and it was just a collection of Filter objects which filtered and transformed input into output. There were many users, and no-one thought of it as “functional”. Keith goes on:

A similar change in an imperative implementation would almost certainly have been more involved and prone to error.

The example is written in C# so it supports closures neatly, transformed to Java it might look like this (the generics are not totally correct, I’m spoiled by Scala):

static int[] getSlots(int slots, int max) {
  return new Random()
      .values(1,max)
      .take(slots - 1)
      .append(0, max)
      .orderBy(new Order[int]() { public int by(int i) { return i })
      .pairwise(new Folder[int]() { public int fold(int x, int y) { return y - x })
      .toArray();
}

More involved? More prone to error? Not so sure. Someone with an OO background would righteously say: Sure this is OO. Method chaining and objects.

As we all know, Java is not a functional programming language, but an object oriented one. There isn’t much difference here, some shorter code, some syntactic sugar for creating functions. The only difference between object oriented and functional being that OO is more noisy? Is OO as superset of FP? The line blurs even more with Functional Java. Is functional not only a way of thinking? Immutable, side effect free, composable code blocks?

Is this example functional? What is functional? Your view would be appreciated, especially if you’re one of the usual suspects.

About the author

stephan Stephan Schmidt has been working with internet technologies for the last 20 years. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own. You can find him on Google +

Discuss on Hacker News Vote on HN