The Knights of Functional Programming fight the Imperative Dragon.

Photo by Emanuela Meli on Unsplash

Lambda village is the sort of place where nothing happens. In Fact, so little happens there that, Ada, the elder of the village, spends more time managing her pumpkin patch than she does worrying about town affairs.

Of course, why would anything happen in Lambda village, set on the great Alonzo plain, it’s a long way from the rest of the Programming Kingdom.

At the end of the plain, there sits the Imperative Volcano. The Imperative Volcano is the sort of place where the best days are overcast. On the worst days, great thunderstorms strike the volcano. Even the great thunderstorms cannot put out the volcanos burning fire.

The fires they say, are caused by the mighty Imperative Dragon. Although no one has seen the Imperative Dragon for 1000 years! The legends, however, speak of the Imperative Dragon. It is great and terrible those who have met its wrath have faced: verbose code; difficult to follow logic and unmanageable state.

It’s the day that that dragon awoke that this story concerns itself with. On that day Ada was tending to her pumpkin patch. From somewhere in the distance was a loud bang and roar. Terrified Ada looked up to see smoke rising from the Inmeritive Volcano and amongst that smoke: The Imperative Dragon.

It was a horror to behold. It’s Giant wingspan blocked out the sun. Its spiky exoskeleton was a deep metal grey, the flesh behind it glowed red.

Having risen from the smoke the imperative dragon scoured the Alonzo plain for somewhere to wreak its destruction. Finally, its eyes rested on Lambda village.

So the Imperative Dragon began to fly over to the village, As it did thick grey clouds appeared in the sky around it. Ada looked on. And as she did she saw nightmares of what might happen to her village. She saw complex logic destroying her productivity. She saw bugs hiding in functions with difficult to derive purposes and the need for comments everywhere (which suck).

But Just as she feared the worst she heard a sound behind her. At first, she thought it was someone bashing two halves of a coconut together. But no! It was The Knights Of Functional Programming.

There they were. Three of them. Mounted on gorgeous white stallions wearing shining platinum armour. But which would face the Imperative Dragon?

The Knight of declarative programming stepped forward.

When the imperative dragon saw it realised it needed to strengthen its own attacks. As the dragon was imperative it used a for loop like so:

The Declarative Knight saw what the dragon was doing so decided to power up his own attacks.

The people of lambda village we’re in awe. The dragons solution described how he was powering up his attacks. As a result, it wasn’t easy to understand at a glance. but the Knights method described what he was doing, Mapping the attack array to a new one with double the power.

Yes, the knight and dragon were using JavaScript but most languages have similar concepts. in Java, you have the Guava library. C# has the .select(). C++ has std::transformMost functional languages like Clojure and Haskell use map.

Stunned by this, the dragon decided to try a new tactic. What he would do is only use his most powerful attacks. He needed to filter his attack list to select only his strongest attacks.

But unfortunately for the dragon, the Declarative Programming Knight had a solution to do the same thing.

The dragon was so shocked by the declarative knight's response, that he decided to make one more effort to power up his attack, the dragon fused all his attacks into one like so:

But the Kight had a better way:

When all was said and done the dragon was in awe the knight but wasn't ready to give up.

But the dragon realised that there weren’t many points of contention left. For example, the dragon knew that an imperative style dominated the UI space. take HTML as an example:

See how HTML simply describes what you want on the screen. It gives you no intel on how to render a <p> or a <div>.

The dragon considered searching its inner store for more attacks. ut even still it had no options consider SQL and Mongo

See how both describe what data you want: ‘select this from here’ or ‘find what matches this criteria’ rather than the process of opening the file and reading the data.

At that point, the dragon realised the game was up. He had been defeated by the imperative style. He knew of course that the battle would go on. There are more dragons of the object-oriented and procedural styles. There are also more knights of functional programming. So the dragon reserved his strength and flew away.

Ada couldn’t believe her eyes! Lambda village was safe. It was all because of the declarative style used in functional programming. She sat down and made herself a cup of tea. And they all lived happily ever after.