r/functionalprogramming • u/mhashim6 • Mar 18 '23
OO and FP FP and OOP are close siblings (using OOP to teach Currying) | The Upside-Down Trees
https://blog.mhashim6.me/fp-and-oop-are-close-siblings/
26
Upvotes
7
u/luther9 Mar 18 '23
There is one big difference between the OOP and FP solutions. To use the example near the end of the article, the
doublerobject could potentially have many methods, but thedoublerfunction is just a single curried function.I like to think of classes as libraries of functions for a given data type.
6
u/antonivs Mar 18 '23
I think a deeper relationship that you're touching on here is the relationship, if not equivalence, between objects and closures.
When you partially apply a function, the result is a closure with some state, i.e. the arguments that were applied. You can then apply that closure to some other value, which then has access to the original state.
When you instantiate an object with arguments that you store in the object, you're similarly creating a package of state, which can be used in the same way.
If you look at the traditional design of an eval/apply interpreter, you may notice that a closure can be treated as an object with a single method, "apply" - not that you would necessarily want to implement it that way, but it's a valid equivalence. The partially applied arguments would be kept in instance variables.
Similarly, chapter 3 of the same book, SICP, Modularity, Objects and State shows how to implement an object system using closures, where an "object" such as a bank account is implemented as a closure that returns a dispatch function which can invoke other functions local to the closure - much like a standard dynamic OO object.
If you do a search for something like closure vs object you'll find a lot of discussion of this equivalence.
2
u/habbalah_babbalah Mar 19 '23
FP and OOP are close siblings in a similar way to "The enemy of my enemy is my friend." If your intent is to lure OOP developers to FP... good luck with that. 😀
Where there is an intersection of concepts and behaviors, you can say that they are similar. They sometimes use similar techniques or data structures to achieve similar goals, e.g. recursion and recursive abstractions like Whole-Part (aggregation, container components).
I knew a functional programmer at my local hackerspace. She had the habit of meeting new people, figuring out whether they were OOP or procedural programmers, and then "educating" (her word not mine) them with, "Your code and systems will always be wrong, because it doesn't resolve to an equation." When I asked how she dealt with needs like type abstraction, composition, collaboration, she started explaining category theory to me, lol.
Ultimately this led to inviting her mentor, Philip Wadler, round to the space, for a lecture on Category Theory. At the end, Wadler kind of shrugs and sort of admits he doesn't fully understand it either.
https://youtu.be/9mZuyUzyN4Q
2
u/[deleted] Mar 18 '23
[removed] — view removed comment