JS.Operator.Assignment: 'Javascript'-style Binary Operator Assignment

Description Usage Arguments Value Examples

Description

If you are familiar with Javascript, you may miss +=, -=, *=, /= very much. The operator can be +, -, *, /, ^, **, \, mod, root.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
lhs %+=% rhs

lhs %-=% rhs

lhs %*=% rhs

lhs %/=% rhs

lhs %^=% rhs

lhs %**=% rhs

lhs %\=% rhs

lhs %mod=% rhs

lhs %root=% rhs

Arguments

lhs

Left hand side argument.

rhs

Right hand side argument.

envir

Environment for operation to take place. Default parent.frame()

Value

Nothing, but lhs is already modified.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## Not run: 
## update an object entirely
a <- 3
a %+=% 2     # 3 --> (3 + 2) --> 5
a %-=% 2     # 5 --> (5 - 2) --> 3
a %*=% 2     # 3 --> (3 * 2) --> 6
a %/=% 2     # 6 --> (6 / 2) --> 3
a %^=% 2     # 3 --> (3 ^ 2) --> 9
a %**=% 2    # 9 --> (3 ** 2) --> 81
a %\=% 30    # 81 --> (81 %% 30) --> 21
a %mod=% 8   # 21 --> (21 %% 8) --> 5
a %root=% 2  # 5 --> (5 ^ (1/2)) --> 2.236

## object and object
a <- 1:4
b <- 4:1
a %+=% b     # a --> c(5,5,5,5)
a %*=% b     # a --> c(20, 15, 10, 5)  (c(5,5,5,5) * c(4,3,2,1))

## update an object partially
b <- 1:4
b[1] %+=% 1     # b --> c(2,2,3,4)

c <- list(list(A=1, B=2:3), list(C=4))
c[[1]]$A %-=% 1 # c[[1]]$A --> 0, rest elements are not changed

1 %+=% b        # simply print 3 (1 + b[2]), but no variable is changed
1 %+=% 1        # simply print 2 (1 + b[1]), but no variable is changed

## End(Not run)

madlogos/aseshms documentation built on May 21, 2019, 11:03 a.m.