Hacker News new | ask | show | jobs
by PheonixPharts 745 days ago
Functional programming is not orthogonal to object oriented programming, they are paradigms that can be used together and the popular object system developed in Java is not close to the only way to do OOP.

R, like Common Lisp, uses an OOP system based on generic functions (well, one of many OOP systems in R, but that's a different topic), where the function handles dispatching to match the object.

Effectively instead of:

    object.method()
you have:

   method(object)
So it works perfectly with the functional paradigm while still being capable of everything an object system is.

The most obvious example of this in R is the `plot` function. You as the programmer don't have to know exactly how to plot an instance of a class, you just pass it into `plot` and it will be handled correctly. If you create a new class you just have to extend the definition of plot in a standard way and it will also be handled for you.

It's a shame that the many flavors of OOP remain relatively unknown to most programmers, and in many of the cases where they're tried (JavaScript's prototype system for example) people have essentially replaced them with more familiar systems.

1 comments

> Effectively instead of object.method() you have method(object)

This reflects a very deep misunderstanding of the distinctive characteristics of each paradigm. It's so far off that it's "not even wrong".

Functional programming is much more about things like purity and referential transparency, about composing functions and/or combinators, about a particular way of managing or modelling effects, about a way of thinking, about using certain kinds of data structures and algorithms.

It's not a syntactical difference.

>> Effectively instead of object.method() you have method(object)

> This reflects a very deep misunderstanding of the distinctive characteristics of each paradigm.

I think the parent made a hasty reading of the GP comment. The GP shows an awareness of multiple OO systems in R.

I believe the GP is attempting to explain to a Java programmer how R could be considered object-oriented even though `plot(item)` does not "look like" what you would see in an object oriented system.

Which is to say: there is an generic function dispatch based on the type of the first argument to the function. This can be _used_ to write in an OO style.

What GP is saying is that while Java is object-centric, R (and CLOS etc) is method-centric: you don't have classes with multiple methods, you have generic functions with multiple methods (each of which implements that function for particular argument types): http://adv-r.had.co.nz/OO-essentials.html#s3

This is not about functional programming at all; the distinction here is completely orthogonal to that.