Functions as interfaces
OO design patterns can be trivial when functions are used
An important aspect of functional programming is that, in a sense, all functions are "interfaces", meaning that many of the roles that interfaces play in object-oriented design are implicit in the way that functions work.
In fact, one of the critical design maxims, "program to an interface, not an implementation", is something you get for free in F#.
To see how this works, let's compare the same design pattern in C# and F#. For example, in C# we might want to use the "decorator pattern" to enhance some core code.
Let's say that we have a calculator interface:
And then a specific implementation:
And then if we want to add logging, we can wrap the core calculator implementation inside a logging wrapper.
So far, so straightforward. But note that, for this to work, we must have defined an interface for the classes. If there had been no ICalculator
interface, it would be necessary to retrofit the existing code.
And here is where F# shines. In F#, you can do the same thing without having to define the interface first. Any function can be transparently swapped for any other function as long as the signatures are the same.
Here is the equivalent F# code.
In other words, the signature of the function is the interface.
Generic wrappers
Even nicer is that by default, the F# logging code can be made completely generic so that it will work for any function at all. Here are some examples:
The new "wrapped" functions can be used anywhere the original functions could be used ? no one can tell the difference!
Exactly the same generic wrapper approach can be used for other things. For example, here is a generic wrapper for timing a function.
The ability to do this kind of generic wrapping is one of the great conveniences of the function-oriented approach. You can take any function and create a similar function based on it. As long as the new function has exactly the same inputs and outputs as the original function, the new can be substituted for the original anywhere. Some more examples:
It is easy to write a generic caching wrapper for a slow function, so that the value is only calculated once.
It is also easy to write a generic "lazy" wrapper for a function, so that the inner function is only called when a result is needed
The strategy pattern
We can apply this same approach to another common design pattern, the "strategy pattern."
Let's use the familiar example of inheritance: an Animal
superclass with Cat
and Dog
subclasses, each of which overrides a MakeNoise()
method to make different noises.
In a true functional design, there are no subclasses, but instead the Animal
class would have a NoiseMaking
function that would be passed in with the constructor. This approach is exactly the same as the "strategy" pattern in OO design.
Note that again, we do not have to define any kind of INoiseMakingStrategy
interface first. Any function with the right signature will work. As a consequence, in the functional model, the standard .NET "strategy" interfaces such as IComparer
, IFormatProvider
, and IServiceProvider
become irrelevant.
Many other design patterns can be simplified in the same way.
Last updated
Was this helpful?