Playing Around with Methods Overloading, C-language and Operators (1)
来源:互联网 发布:c语言自定义结构体 编辑:程序博客网 时间:2024/05/16 08:29
Introduction
R is an object-oriented (OO) language. This basically means that R is able to recognize the type of objects generate from analysis and to apply the right operation on different objects.
For example, the summary(x)
method performs different operations depending on the so-called “class” of x
:
the outputs reported, and the calculations performed, by the summary()
method are really different for the x
and the md
objects.
This behavior is one of characteristics of OO languages, and it is called methods overloading.
The methods overloading can be applied not only for methods in “function form” (i.e., for methods like summary()
), but also for operators; indeed, “behind the scenes”, the operators are functions/methods. For example, if we try to write
in the R console we obtain:+
That means that the +
operator actually is a function/method that requires two arguments: e1
ande2
, which are respectively the left and right argument of operator itself.
The +
operator is present in R base, and can be overloaded as well, like in ggplot2
package, where the +
operator is used to “build” the graph characteristics, as in following example:
The +
operator, then, is applied differently with ggplot2
objects (with respect other object types), where it “concatenates” or “assembles” parts of final graph.
In this small post, and following ones, I would like to produce some “jokes” with objects, operators, overloading, and similar “amenities”..
C-language +=
operator and its emulation in R
In C language there are several useful operators that allow the programmer to save some typing and to produce some more efficient and easiest to read code. The first operator that I would like to discuss is the +=
one.
+=
is an operator that does operations like: a = a + k
.
In C, the above sentence can be summarized with a += k
. Of course, the sentence can be something of more complex, like
a += (x-log(2))^2
In this case, the code line shall be “translated” to a = a + (x-log(2))^2
.
If I would like to have in R a new operator that acts similarly to C’s +=
, I whould have to create it.
Unfortunately, not all the names are allowed in R for new operators: if I want to produce a new operator name I can only use operators with names like %mynewoperator%
where the %
symbols are mandatory.
Indeed, for this example, I will create a new %+=%
operator that acts similarly to the C’s +=
.
This new operator has to be able to get the values of variables passed as arguments, to sum them, and then, more importantly, to update the value of the first variable with the new value.
The first version of the new operator can be the following:
The function retrieves the name of variable to update by deparsing the x
input variable (first argument of operator), and then calculates the sum between the first argument and the second argument values.
Finally, the function assigns the result of sum to the variable whose name has been previously retrieved, but in the environment from which the %+=%
operator has been called (the environment is found at the line env <- parent.env(environment())
).
Let’s test the operator:
It works!
Now a more complex test:
It works fine as well.
Now a more sophisticated test
It works correctly!
It seems that the new operator works as expected. It recognizes the object type and applies the correct method to different object types.
Some benchmarks
To test the performance of new operator, we need the microbenchmark
library.
Now, we can test the new operator versus the “standard” operator. Let’s try with a simple summation:
Our new operator is about 60 times slower than the “base” operation!
Now we can test the same operator, but with more complex objects:
The two above functions draw the same graph, but the first uses the “base” +
operator, whereas the second one uses the%+=%
operator. Let’s compare them:
In this case, the function with new operator performs like (if not better than) the function that uses the “standard” notations.
Conclusions
We played with environments, and operators, and we created a new operator that “simulates” the C’s+=
one.
This new operator allows the developer to reduce the code typing activity. Of course, by using a shorter name (for example, %+%
) we can reduce again the typing.
As we created the +=
operator, we can build other operators like %-=%
, %*=%
, %/=%
, and so on.
Now some considerations about performances: if we compare the new operator with the standard +
-based coding applied on “base” objects, it results very slow; but if we compare the new operator with the +
operator applied to more complex objects (in our example, a ggplot2
one), the new operator does not perform worse than the standard coding.
More enhancements can also be applied to code optimimzations, but I think that for this example the above code is good enough.
In future posts, I will deepen some aspects of overloading, C operators and classes.
- Playing Around with Methods Overloading, C-language and Operators (1)
- Playing around with (old?)SEH
- Overloading the increment and decrement operators (非常好)
- overloading stream insertion and stream extraction operators
- Creating And Playing With Branches
- Reactive Programming with RxJava-Chapter3:Operators and Transformations(1)
- Overloading with Widening、Boxing and Var-args
- Overloading Methods in WCF
- Arithmetic: Creating Expressions With Variables And Operators
- Type conversions with implicit and explicit operators
- UVALive 5059 C Playing With Stones
- Overloading unary operators (C++ only)
- Comparison between overloading operators in member function and in friend function
- [Clojure] A Room-Escape game, playing with telnet and pure-text commands - Part 1
- asm and c language
- 操作符重载(Overloading operators)
- Reactive Programming with RxJava-Chapter3:Operators and Transformations(2)
- Reactive Programming with RxJava-Chapter3:Operators and Transformations(3)
- 自定义View实现空调遥控器切换度数
- 想一下
- keepalived vrrp iptables问题
- 关于Mysql事务REPEATABLE_READ隔离级别下多版本并发控制(MVCC)重复读产生的坑的你踩过么?
- 第15周 啊哈算法 栈
- Playing Around with Methods Overloading, C-language and Operators (1)
- canvas绘制旋转转盘
- PDF文件怎样合并分割
- Android Studio错误Error:(23, 17) Failed to resolve: junit:junit:4.12解决方案
- VC6.0 Window7结束调试时程序无法结束的问题解决办法
- HDU 2188 悼念512汶川大地震遇难同胞――选拔志愿者(巴什博奕)
- 【iOS】iOS下高斯模糊效果的实现
- 快照技术原理
- Kaggle 手写识别题