Why does the following work in R?
> `:=` <- function(x, y) x + y
> 1 := 2
[1] 3
My understanding was that %
was required for user-defined infix operators. Are there other (possibly easier to type) options available?
infix operator (plural infix operators) (computing) an operator that is placed in between the operands like it is commonly used in arithmetical and logical formulae and statements. The plus sign in "2 + 2" is placed as an infix operator in arithmetic.
Infix notation is the notation commonly used in arithmetical and logical formulae and statements. It is characterized by the placement of operators between operands—"infixed operators"—such as the plus sign in 2 + 2.
- Infix operators are a fancy name for operator symbols that go between two variables. Think of them like plus signs or a multiplication sign or division sign. So you can add three plus four, like this, three plus four. And in this case the plus is an infix operator.
Infix operators are used in between two operands, so simple arithmetic operations such as 1 + 2 would be an infix expression where + is the infix operand. Prefix and postfix operators are either applied before or after a single operand.
This is because :=
is, like <-
or <<-
, defined as LEFT_ASSIGN
for the parser of R.
See http://svn.r-project.org/R/trunk/src/main/gram.y
This means that :=
is a special case and you may as well not expect that any other options are available.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With