4 ms·
> > OO languages (i.e. imperative) > > Why can't an OO language be functional? Yes, I think you are totally right. As I see it, OO is about data and behaviour
by epidemian 15y ago
> > OO languages (i.e. imperative)
>
> Why can't an OO language be functional?
Yes, I think you are totally right. As I see it, OO is about data and behaviour encapsulation, and object polymorphism. There is no reason why objects need to carry mutable state. But, they often do, and I think most authors do put OOP in the procedural/imperative side of programming.
Of course come cool hybrid languages do mix concepts from OOP and functional programming in order to have more expressive power. But most of them still map pretty closely to the Von Neumann model: a set of statements executed one after the other that alter the program's state, and hence are imperative too.
A functional program does not map it's computation to this Von Neuman model, which pretty much represents how computers work, but to an abstract model, usually based on mathematical concepts.
Therefore,
even = filter(nats[1:100], i -> i % 2 == 0)
would be functional or not depending on the underlying execution model. A functional programing language would guarantee that the function passed to `filter` is side-effect-free, so it can be free to make the computation of the result in parallel or lazily or whatnot. An imperative language would be required to execute the passed function for each element of the sequence in a defined order, as that function might very well not be side-effect-free.
But, in the end, I think that all that is a very absolutist approach to what is functional and what is not. Trying to draw that line is usually quite futile, as it really depends on very strict definitions, and, well, everybody agreeing on those definitions. I just wanted to clarify my point a little bit (though I probably failed)
> On the other hand, these concepts and expressions are used with so many different meanings and in so many different contexts, that it's no surprise that different people understand them differently. Maybe it's better to just talk in code...
Totaly agreed. Probably a more "fuzzy" definition of "functional", thus allowing one to say that some construct "more functional" than an other one, is more pragmatic. I also would say that
even = filter(nats[1:100], i -> i % 2 == 0)
is much "more functional", or declarative, than
even = []
for i = 1 to 100
if i % 2 == 0
even.append(i)
And I would even say that Python's list comprehensions are even more declarative, even though it is an imperative language too:
even = [i for i in range(0,100) if i % 2 == 0]
Code FTW!