| 1 |
This is my take on what functional programming really is, in a way that will make sense to a jobbing programmer just trying to Get Stuff Done. I put it to you that every function you write has two sets of inputs and two sets of outputs. Two? Only one, surely? No, two. Definitely two. Let’s take a look at the first pair with this example: public int square(int x) { return x * x; } // NOTE: The language doesn't matter, but I've picked one with // explicit input & output types, for emphasis. Here, the input you’re used to thinking about is int x, and the output you’re used to is also an int. That’s the first set of inputs & outputs. The traditional set, if you will. Now let’s see an example of the second set of inputs and outputs: public void processNext() { Message message = InboxQueue.popMessage(); if (message != null) { process(message); } } According to the syntax, this function takes no inputs and returns no output, and yet it’s obviously depending on something, and it’s obviously doing something. The fact is, it has a hidden set of inputs and outputs. The hidden input is the state of the InboxQueue before the popMessage() call, and the hidden outputs are whatever process causes, plus the state of InboxQueue after we’re done. |
Комментарии