Get the latest tech news

Dualities in functional programming


Duality is technical jargon for "complementary". Programmers often overlook the role dualities in programming and hence fail to recognize them, even if they are hiding in plain sight. In this article we focus on two important pairs of dual concepts: producers and consumers, sum types and product types. I also spend some time ranting about default values being as bad as null values.

In this viewpoint, the program and the executing environment are the two partners performing a tango dance (or the two opponents in a tennis-game if you prefer sport over artistic metaphors). When the library function returns a sum-type (for instance, either an error or a successful result) you should forcefully verify that you have a piece of code to handle either branches. This series of equation means that the HTTP-response handler in our example actually is made of two possibly-independent error-handler pieces of code(a * c) and a success-handler taking a decoded JSON value(a * d).

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Dualities

Dualities