Skip to contents

Binary arithmetic operators for Container() objects and derived classes.

Usage

# S3 method for class 'Container'
x + y

# S3 method for class 'Container'
x - y

# S3 method for class 'Deque'
x + y

# S3 method for class 'Deque'
x - y

# S3 method for class 'Dict'
x + y

# S3 method for class 'Dict'
x - y

# S3 method for class 'Set'
x + y

# S3 method for class 'Set'
x - y

Arguments

x, y

Depending on the operator at least one must be of class Container() or the respective derived class and the other at least be coercible to the respective class.

Value

For Container, x + y combines x and y into a new container by appending y to x.

For Container, x - y element-wise discards all items of y from x, given the element was contained in x. The result is always a container.

For Deque, x + y combines x and y into a new deque by appending y to x.

For Deque, x - y element-wise removes all items of y from x, given the element was contained in x.

For Dict, x + y combines x and y into a new dict by updating x by y (see also [update()]).

For Dict, x - y removes all keys from x that appear in y.

For Set, x + y performs the set union.

For Set, x - y performs the set difference.

Examples

c1 = container(1, 1:2)
c2 = container(2, 1:2)
c1 + c2     # same as c(c1, c2)
#> [1, (1L 2L), 2, (1L 2L)]
c2 + c1     # same as c(c2, c1)
#> [2, (1L 2L), 1, (1L 2L)]

c1 - c2
#> [1]
c2 - c1
#> [2]
c1 - c1
#> []
# Arithmetic
d1 = deque(1, 1:2)
d2 = deque(2, 1:2)
d1 + d2     # same as c(d1, d2)
#> |1, (1L 2L), 2, (1L 2L)|
d2 + d1     # same as c(d2, d1)
#> |2, (1L 2L), 1, (1L 2L)|

d1 - d2
#> |1|
d2 - d1
#> |2|
d1 - d1
#> ||

# Arithmetic
d1 = dict(a = 1, b = list(1, 2))
d2 = dict(a = 2, b = list(1, 2))
d1 + d2      # same as update(d, d2)
#> {a = 2, b = list(1, 2)}
d2 + d1      # same as update(d2, d)
#> {a = 1, b = list(1, 2)}
try({
c(d1, d2)    # duplicated keys are not allowed for Dict
})
#> Error : duplicated keys are not allowed
d1 - d2
#> {}
d2 - d1
#> {}
d1 - d1
#> {}

# Arithmetic
s1 = setnew(1, 1:2)
s2 = setnew(2, 1:2)
s1 + s2     # same as s1 | s2 or c(c1, s2)
#> {1, (1L 2L), 2}
s2 + s1     # same
#> {2, (1L 2L), 1}

s1 - s2
#> {1}
s2 - s1
#> {2}