Edit: This post was written tounge-in-cheek to suggest that developers currently excited about the sudden interest in functional programming may find reactive programming interesting also. It was not meant to seriously suggest that F# or functional programming is “useless”, or that the people responsible for F# are in any way wasting their time. I think F# is a brilliant language, and I apologize if anyone was offended by this post.
You can’t walk down the street these days without being told that F# is the next big thing. It’s easy to be taken in by the pomp and pageantry surrounding the functional programming paradigm, and in many ways it’s a bit of an emperor’s new clothes dilemma: most people can see functional programming is, for the most part, useless, but no one wants to admit it
What some people (mostly people on the CLR and C# language team) forget, is that there are alternatives to console applications. Here’s the typical User Experience of an F# program:
There’s only one thing that’s constant in our world: change. Inputs change, assumptions change. So why is everyone so enthralled with a programming paradigm that has such abysmal support for handling change?
Your highly concurrent, lambda powered, currying pattern-matching data analysis algorithm, running over 87 threads and 8 cores, is a pretty good effort. But once it’s analysed the 3,000,000 visits to your web site and told you what products to put on discount, what happens to the 3,000,001′th visitor? Well, once your functional code arrived at a result, it kinda forgot how it got there. So you have to run the whole thing again for a minor change in the inputs.
Wouldn’t it be great if you could write code in a functional programming language, but which automatically detected changes and re-evaluated itself? A kind of event stream processor. As visits come in your algorithm is being constantly evaluated, quickly, and the results updated. It’s the difference between being able to react in real-time, and having to wait for the hourly report to be generated.
As it turns out, there’s a paradigm for that: it’s called functional reactive programming. And unlike normal functional programming, it actually has uses outside of console applications
F# developers go gaga over pattern matching:
let getWarningMessageAboutTooManyToppings toppings =
match toppings with
| 0 -> "You didn't select any toppings"
| 1 -> "You should have more toppings"
| _ when toppings < 5 -> "That's a healthy number of toppings"
| _ -> "Too many toppings on your pizza, fatboy"
Now that’s great, but what happens when I select a new topping? I guess that’s what "Refresh" buttons are for.
Here’s a functional reactive version of pattern matching, supported via Bindable LINQ’s Switch operator (you’ll find this in the PizzaLinq sample application in the Bindable LINQ code).
_healthWarningMessage = _selectedToppings.Count().Switch(
Case.When<int, string>(toppings => toppings < 1,
"Surely you would like more toppings?"
),
Case.When<int, string>(toppings => toppings >= 3,
"You have too many toppings!"
),
Case.Default<int, string>(
"Just right!"
)
);
In Bindable LINQ, when I select a topping on the UI the result of the Count() call will change, which will re-evaluate the Switch operator and return me a different message. There’s no Refresh buttons and no code to wire it up. It’s functional, but it’s also useful.
If you could get enough programmers working on a reactive .NET language, it’s conceivable that you could combine the implicit parallelism of functional languages with reactive languages in order to process changes and have a result that’s continually updated from multiple threads. Unfortunately, all the smart developers are focused right now on a paradigm that’s severely limited to back-end or discreet, point-in-time operations, rather than a more wide-ranging paradigm like that of functional reactive programming.
If you’re a language wonk and you’re considering learning F# and functional programming for fun, consider reactive programming instead.
Filed under: Bindable LINQ | 10 Comments »