Higher-order functions are quite like objects

April 26, 2008 at 11:10 pm Leave a comment

I occasionally read things that cite higher-order functions as one of the main advantages of dynamic or functional programming languages… this is good since I quite like higher-order functions and dynamic programming languages, the only issue is that creating higher order functions is almost exactly the same as object instantiation.

To make this slightly clearer, consider this example written in python:

def makeAdder(x):
    def f(y):
         return x + y
    return f
g = makeAdder(5)

and compare this to

class Adder:
    def __init__(self, x):
        self.x = x
    def execute(self, y):
        return x + y

    def execute_something_else(self, y):

g = Adder(5)

This code is more or less identical, both examples are just storing some state and then executing a function at a later time based on this state, the only difference is that the higher-order function uses a more specific mechanism – there is only one function that we can call on the state we set – whereas with a class we can call as many functions as we want.

Does this mean that higher-order functions are meaningless and we should just go back to doing object oriented programming, which is more general? Not exactly.

Higher order functions still have some advantages over class instantiation, it’s probably just that they aren’t the ones that people normally list.

Some advantages include:

  1. Function calls have a fixed interface. By limiting yourself to using this fixed interface all code that uses this interface can be composed easily. People might be more likely to limit themselves to using language features than they are to using some arbitrarily decided upon interface.
  2. The syntax for defining and calling functions has slightly less noise than that for defining and instantiating classes in most languages – (thought not a hideous amount less). Removing noise has a habit of making things easier to read (assuming this noise isn’t providing people with context)
  3. Using higher-order functions tend to produce code that is slightly more declarative in nature (i.e things ‘are something’ rather than ‘doing something’), this can, depending on mood, make code easier to read as one doesn’t need to follow the flow of the program so much. (To make this declarative versus imperative argument clearer consider the task of parsing an SQL statement compared to a function that does the corresponding task).
  4. You don’t need to make a conscious decision about what you are doing when you use higher-order functions, whereas if you use the corresponding “class and function” code, you will have probably have had to decide upon this idiom

So using higher-order function can help you when programming by being less powerful, less general and more restrictive… or something like that.


Entry filed under: Uncategorized.

Backing up one’s home directory with tar Sending mail through gmail Using Emacs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed

April 2008
« Mar   May »

%d bloggers like this: