Trevor Oakes

First Class and Higher Order Functions

In order to expand my mind, I’ve been exploring different languages, but sometimes I’ve had to come back to the first language I learned in depth: Java. And when I do, the thing I miss most is first class, higher order functions. That and pattern matching.

Let me show you what I mean:

The cool thing about a language with first class functions, like Racket, is that if the language did not provide us with map, we can write zipWith.

But besides that, allowing functions as arguments to and return values from functions allows you to abstract more things than you can without that ability. Many design patterns exist because the language does not have first class functions, but because that idea is very useful, people emulate first class functions poorly using object orientation. The visitor pattern, for example, is essentially a fold.

A programmer can emulate first class functions by hand using object orientation, but take a look at an attempt to do what took 7 short lines above in Racket in Java and ask yourself, why not use a language with them in the first place?

Comments