nushell/crates/nu-command/tests/commands/math
Ian Manske 4d3283e235
Change append operator to concatenation operator (#14344)
# Description

The "append" operator currently serves as both the append operator and
the concatenation operator. This dual role creates ambiguity when
operating on nested lists.

```nu
[1 2] ++ 3     # appends a value to a list [1 2 3]
[1 2] ++ [3 4] # concatenates two lists    [1 2 3 4]

[[1 2] [3 4]] ++ [5 6]
# does this give [[1 2] [3 4] [5 6]]
# or             [[1 2] [3 4] 5 6]  
```

Another problem is that `++=` can change the type of a variable:
```nu
mut str = 'hello '
$str ++= ['world']
($str | describe) == list<string>
```

Note that appending is only relevant for lists, but concatenation is
relevant for lists, strings, and binary values. Additionally, appending
can be expressed in terms of concatenation (see example below). So, this
PR changes the `++` operator to only perform concatenation.

# User-Facing Changes

Using the `++` operator with a list and a non-list value will now be a
compile time or runtime error.
```nu
mut list = []
$list ++= 1 # error
```
Instead, concatenate a list with one element:
```nu
$list ++= [1]
```
Or use `append`:
```nu
$list = $list | append 1
```

# After Submitting

Update book and docs.

---------

Co-authored-by: Douglas <32344964+NotTheDr01ds@users.noreply.github.com>
2024-11-24 10:59:54 -08:00
..
abs.rs Make the math commands const (#13566) 2024-08-07 22:20:33 +02:00
avg.rs Make the math commands const (#13566) 2024-08-07 22:20:33 +02:00
ceil.rs Make the math commands const (#13566) 2024-08-07 22:20:33 +02:00
floor.rs Make the math commands const (#13566) 2024-08-07 22:20:33 +02:00
log.rs Make the math commands const (#13566) 2024-08-07 22:20:33 +02:00
max.rs Make the math commands const (#13566) 2024-08-07 22:20:33 +02:00
median.rs Make the math commands const (#13566) 2024-08-07 22:20:33 +02:00
min.rs Make the math commands const (#13566) 2024-08-07 22:20:33 +02:00
mod.rs Change append operator to concatenation operator (#14344) 2024-11-24 10:59:54 -08:00
mode.rs Make the math commands const (#13566) 2024-08-07 22:20:33 +02:00
product.rs Make the math commands const (#13566) 2024-08-07 22:20:33 +02:00
round.rs Make the math commands const (#13566) 2024-08-07 22:20:33 +02:00
sqrt.rs Make the math commands const (#13566) 2024-08-07 22:20:33 +02:00
stddev.rs Make the math commands const (#13566) 2024-08-07 22:20:33 +02:00
sum.rs Make the math commands const (#13566) 2024-08-07 22:20:33 +02:00
variance.rs Make the math commands const (#13566) 2024-08-07 22:20:33 +02:00