An article about what is Functional Programming (FP), and how does it differ from Object Oriented Programming (OOP) ?
Note that both these paradigms are used in R, and interact with each others.
Here’s the definition of what FP is :
- Programming == creating function
- Functions returns values computed from arguments (and only these)
- A function has no side effect
Side note: a function is said to have side effect when it has an impact outside of its scope, that is to say when a function has an effect outside its argument.
Wikipedia has a good page about this : en.wikipedia.org/wiki/Side_effe…
A function without side effect is said to be « pure ».
The pure function concept is also at the root of the naming of the {purrr} package, as explained here : github.com/tidyverse/purr…
R does not enforce functions to be pure. The example from the article being the Random number generation, which is implemented as 'a distinctly “state-based” model in which an object in the global environment (.Random.seed) represents the current state of the generators.'
On the other hand, OOP is :
- Programming == creating objects
- Objects have class, and are defined by properties
- An object inherits from another superclass
- Computation is made through methods defined in the object
So, is R an OOP language? « Not from its inception », but there are many OOP paradigms that has been implemented in R: S3, S4, RC…
The more modern being R6: cran.r-project.org/web/packages/R…
The specificity of R OOP is that object methods are function calls, hence the use of "functional OOP". It opposes to "encapsulated (OOP), in which methods are part of the class definition". In other words "Methods belong to functions, not to classes; the functions are generic".
You may have heard it before, but:
- Everything that exists is an object.
- Everything that happens is a function call.
What that means, basically, is that contrary to other languages as Java or C++, every reference in R is a reference to a single internal structure type: a data.frame, a vector or a function refer, internally, to the same data structure.
But what does "reference" mean? A reference is the combination of a name and a context: the context being an environment, a reference is then name + environment.
This concept of environment is central for R programming, as it defines the context in which a name is looked for, and can lead to surprising behaviors if you don’t understand them correctly, especially with packages.
This is the concept of "local reference": unless otherwise specified, every object is evaluated in the environment it is called.
This also allows you to use the same name in different environments/functions.
So, to sum up, R OOP can be considered as a Functional OOP as methods, which perform actions on the objects, are implemented as functions, not as part of the object.
The big difference with other OOP paradigms is that methods are generic: for example, you can call print or plot on every model object, regardless if it’s a lm, glm, or any other — this kind of methods are not contained in the model object.
To sum up what a functional OOP requires :
- Classes for objects
- Generic function that dispatch methods
- Methods for each class
This is what S3 does: creating functions that dispatch methods to class. adv-r.had.co.nz/S3.html
And, to conclude ;)
(Conclusion 2: Computer Science is Awesome)
• • •
Missing some Tweet in this thread? You can try to
force a refresh
[Thread]
Why do we use arrow `<-` instead of equal `=` for assignment in #RStats :
It’s an historical choice: R comes from S, which used <- for assignment. S uses `<-` partly because it is inspired by APL, which had the ← operator for assignment, as it was developed for this keyboard, which has a key for arrow : en.wikipedia.org/wiki/APL_(prog…
Not that at that time, with APL, the arrow was chosen because it distinguished from the equal operator (there were no `==` for testing equality).
See : softwarepreservation.org/projects/apl/B…