Convert sum command into subcommand of the math command (#2004)

* Convert sum command into subcommand of the math command

* Add bullet points to math.md documentation
This commit is contained in:
Joseph T. Lyons
2020-06-18 22:02:01 -04:00
committed by GitHub
parent 5f9de80d9b
commit 53a6e9f0bd
26 changed files with 175 additions and 143 deletions

View File

@ -1,95 +1,126 @@
# math
Mathematical functions that generally only operate on a list of numbers (integers, decimals, bytes) and tables.
Currently the following functions are implemented:
`math average` Get the average of a list of number
`math min` Get the minimum of a list of numbers
`math max` Get the maximum of a list of numbers
* `math average`: Finds the average of a list of numbers or tables
* `math min`: Finds the minimum within a list of numbers or tables
* `math max`: Finds the maximum within a list of numbers or tables
* `math sum`: Finds the sum of a list of numbers or tables
However, the mathematical functions like `min` and `max` are more permissive and also work on `Dates`.
## Examples
To get the average of the file sizes in a directory, simply pipe the size column from the ls command to the average command.
### List of Numbers (Integers, Decimals, Bytes)
```shell
> ls
# │ name │ type │ size │ modified
# │ name │ type │ size │ modified
────┼────────────────────┼──────┼──────────┼─────────────
0 │ CODE_OF_CONDUCT.md │ File │ 3.4 KB │ 4 days ago
1 │ CONTRIBUTING.md │ File │ 1.3 KB │ 4 days ago
2 │ Cargo.lock │ File │ 106.3 KB │ 6 mins ago
3 │ Cargo.toml │ File │ 4.6 KB │ 3 days ago
4 │ LICENSE │ File │ 1.1 KB │ 4 days ago
5 │ Makefile.toml │ File │ 449 B │ 4 days ago
6 │ README.md │ File │ 16.0 KB │ 6 mins ago
7 │ TODO.md │ File │ 0 B │ 6 mins ago
8 │ assets │ Dir │ 128 B │ 4 days ago
9 │ build.rs │ File │ 78 B │ 4 days ago
10 │ crates │ Dir │ 672 B │ 3 days ago
11 │ debian │ Dir │ 352 B │ 4 days ago
12 │ docker │ Dir │ 288 B │ 4 days ago
13 │ docs │ Dir │ 160 B │ 4 days ago
14 │ features.toml │ File │ 632 B │ 4 days ago
15 │ images │ Dir │ 160 B │ 4 days ago
16 │ justfile │ File │ 234 B │ 3 days ago
17 │ rustfmt.toml │ File │ 16 B │ 4 days ago
18 │ src │ Dir │ 128 B │ 4 days ago
19 │ target │ Dir │ 192 B │ 8 hours ago
20 │ tests │ Dir │ 192 B │ 4 days ago
0 │ CODE_OF_CONDUCT.md │ File │ 3.4 KB │ 4 days ago
1 │ CONTRIBUTING.md │ File │ 1.3 KB │ 4 days ago
2 │ Cargo.lock │ File │ 106.3 KB │ 6 mins ago
3 │ Cargo.toml │ File │ 4.6 KB │ 3 days ago
4 │ LICENSE │ File │ 1.1 KB │ 4 days ago
5 │ Makefile.toml │ File │ 449 B │ 4 days ago
6 │ README.md │ File │ 16.0 KB │ 6 mins ago
7 │ TODO.md │ File │ 0 B │ 6 mins ago
8 │ assets │ Dir │ 128 B │ 4 days ago
9 │ build.rs │ File │ 78 B │ 4 days ago
10 │ crates │ Dir │ 672 B │ 3 days ago
11 │ debian │ Dir │ 352 B │ 4 days ago
12 │ docker │ Dir │ 288 B │ 4 days ago
13 │ docs │ Dir │ 160 B │ 4 days ago
14 │ features.toml │ File │ 632 B │ 4 days ago
15 │ images │ Dir │ 160 B │ 4 days ago
16 │ justfile │ File │ 234 B │ 3 days ago
17 │ rustfmt.toml │ File │ 16 B │ 4 days ago
18 │ src │ Dir │ 128 B │ 4 days ago
19 │ target │ Dir │ 192 B │ 8 hours ago
20 │ tests │ Dir │ 192 B │ 4 days ago
```
```shell
> ls | get size | math average
───┬────────
06.5 KB
07.2 KB
───┴────────
```
```shell
> ls | get size | math min
───┬─────
00 B
00 B
───┴─────
```
```shell
> ls | get size | math max
───┬──────────
0 │ 106.3 KB
0 │ 113.5 KB
───┴──────────
```
# Dates
```shell
> ls | get size | math sum
───┬──────────
0 │ 143.4 KB
───┴──────────
```
### Dates
```shell
> ls | get modified | math min
2020-06-09 17:25:51.798743222 UTC
```
```shell
> ls | get modified | math max
2020-06-14 05:49:59.637449186 UT
```
### Operations on tables
```shell
> pwd | split row / | size
───┬───────┬───────┬───────┬────────────
# │ lines │ words │ chars │ max length
───┼───────┼───────┼───────┼────────────
00155
10177
20199
30177
───┴───────┴───────┴───────┴────────────
```shell
> pwd | split row / | size
───┬───────┬───────┬───────┬────────────
# │ lines │ words │ chars │ max length
───┼───────┼───────┼───────┼────────────
00155
10177
20199
30177
───┴───────┴───────┴───────┴────────────
```
```shell
> pwd | split row / | size | math max
───────────┬───
lines │ 0
words │ 1
chars │ 9
max length │ 9
lines │ 0
words │ 1
chars │ 9
max length │ 9
────────────┴───
```
```shell
> pwd | split row / | size | math average
────────────┬────────
lines │ 0.0000
words │ 1.0000
chars │ 7.0000
max length │ 7.0000
lines │ 0.0000
words │ 1.0000
chars │ 7.0000
max length │ 7.0000
────────────┴────────
```
## Errors
`math` functions are aggregation functions so empty lists are invalid
```shell
> echo [] | math average
error: Error: Unexpected: Cannot perform aggregate math operation on empty data
@ -102,7 +133,3 @@ then unexpected results can occur.
> echo [1 2 a ] | math average
0
```

View File

@ -41,7 +41,7 @@ Applies the subcommand to a value or a table.
1 │ │ filesystem │
━━━┷━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
> echo "1, 2, 3" | split row "," | str to-int | sum
> echo "1, 2, 3" | split row "," | str to-int | math sum
━━━━━━━━━
<value>
─────────

View File

@ -5,7 +5,7 @@ This command allows you to calculate the sum of values in a column.
To get the sum of the file sizes in a directory, simply pipe the size column from the ls command to the sum command.
```shell
> ls | get size | sum
> ls | get size | math sum
━━━━━━━━━
value
━━━━━━━━━
@ -15,7 +15,7 @@ To get the sum of the file sizes in a directory, simply pipe the size column fro
To get the sum of the characters that make up your present working directory.
```shell
> pwd | split-row / | size | get chars | sum
> pwd | split-row / | size | get chars | math sum
━━━━━━━━━
<value>
━━━━━━━━━
@ -27,15 +27,15 @@ Note that sum only works for integer and byte values. If the shell doesn't recog
One way to solve this is to convert each row to an integer when possible and then pipe the result to `sum`
```shell
> open tests/fixtures/formats/caco3_plastics.csv | get tariff_item | sum
> open tests/fixtures/formats/caco3_plastics.csv | get tariff_item | math sum
error: Unrecognized type in stream: Primitive(String("2509000000"))
- shell:1:0
1 | open tests/fixtures/formats/caco3_plastics.csv | get tariff_item | sum
1 | open tests/fixtures/formats/caco3_plastics.csv | get tariff_item | math sum
| ^^^^ source
```
```shell
> open tests/fixtures/formats/caco3_plastics.csv | get tariff_item | str --to-int | sum
> open tests/fixtures/formats/caco3_plastics.csv | get tariff_item | str --to-int | math sum
━━━━━━━━━━━━━
<value>
─────────────