Define comma operator as javascript

The Javascript comma operator is useful for inserting commands into your code without interrupting your workflow. The previous expression is executed, but ignored (for example, if it is embedded in a function expecting another object):

> x = 5, 4
< 4
> x
< 5

      

Is there a way to implement this in R? I tried to follow

',' = function(x, y) { x; y }

      

but R seems to have blocked the comma:

> 4, 5
Error: unexpected ',' in "4,"

      

Is there any workaround for this?

+3


source to share


1 answer


(Un) is fortunately ,

one of the few things in R that cannot be overloaded / redefined. Its not an operator in the language, unlike, for example, `{`

and `(`

.

In a similar vein, while you can overload `=`

and `(`

, you cannot indiscriminately change their value; in a function call with parameters (for example, f(a = 1, b = 2)

) cannot be changed (

and =

to change their value, because at the same time they are not operators in this context).



As noted in the comments, ;

seems like a very close equivalent for your purpose .

0


source







All Articles