Profile picture
Colin Fay @_ColinFay
, 20 tweets, 6 min read Read on Twitter
[Thread #RStats #Programming ]
Some important points taken from "Object-Oriented Programming, Functional Programming and R", by John M. Chambers
arxiv.org/pdf/1409.3531.…
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…
Some OOP are even implemented just for a specific package (e.g ggplo2 with: cran.r-project.org/web/packages/g…)
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.

Like this thread? Get email updates or save it to PDF!

Subscribe to Colin Fay
Profile picture

Get real-time email alerts when new unrolls are available from this author!

This content may be removed anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member and get exclusive features!

Premium member ($3.00/month or $30.00/year)

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!