I have heard this time and again, and I am trying to understand and validate the idea that FP and OO are orthogonal.
First of all, what does it mean for 2 concepts t
I’ve just found a wonderful explanation of the orthogonality of OOP and FP.
The basic idea is as follows. Imagine we are working with ASTs of math expressions. So we have different types of nouns (constant, addition, multiplication) and different verbs (eval, toString).
Let’s say, the expression is (1 + 2) * 3. Then the AST would be:
multiplication
/ \
addition 3
/ \
1 2
To implement a verb, we have to provide its implementation for every type of noun. We can represent it as a table:
+---------------------+-------------------------------------+
| eval | toString |
+---------------+---------------------+-------------------------------------+
| constant | value | value.toString |
+---------------+---------------------+-------------------------------------+
| addition | lhs.eval + rhs.eval | lhs.toString + " + " + rhs.toString |
+---------------+---------------------+-------------------------------------+
| mutiplication | lhs.eval * rhs.eval | lhs.toString + " * " + rhs.toString |
+---------------+---------------------+-------------------------------------+
The “orthogonality” comes from the fact, than in OOP we will be implementing this table by rows: we will represent every noun as a class, which will have to implement every method.
In FP, on the other hand, we will implement this table by columns — we will write a function for every verb, and this function will react differently to arguments of different types (using pattern matching probably).