In Nushell we can use a lot a math related commands to get for example the minimum and maximum values of a list of numbers.
In the math module you can find the commands math min and math max.
You can use these commands to get the minimum and maximum values of a list of numbers, durations, file sizes, a range and tables.
In the following example you can see usages of the math min and math max commands on different input types:
use std/assert
# math min
# Using math min on a list of numbers.
assert equal ([1 2 3] | math min) 1
# Using math min on a range of numbers.
assert equal (5..10 | math min) 5
# Using math min on a list of durations.
assert equal ([1min 61sec 1.5min] | math min) 1min
# Using math min on a list of sizes.
assert equal ([200MB 100MB 0.5GB] | math min) 100MB
# Using math min on a list of strings.
assert equal (["Alice" "Bob" "Charlie"] | math min) "Alice"
# Helper table with columns name, age, height (in cm).
let table = [
[name age height];
[Alice 30 175]
[Bob 25 183]
[Charlie 35 180]
]
# Using math min on a table result in a record with for each column the minimum value.
assert equal ($table | math min) { name: Alice age: 25 height: 175 }
# math max
# Using math max on a list of numbers.
assert equal ([1 2 3] | math max) 3
# Using math max on a range of numbers.
assert equal (5..10 | math max) 10
# Using math max on a list of durations.
assert equal ([1min 61sec 1.5min] | math max) 1.5min
# Using math max on a list of sizes.
assert equal ([200MB 100MB 0.5GB] | math max) 0.5GB
# Using math max on a list of strings.
assert equal (["Alice" "Bob" "Charlie"] | math max) "Charlie"
# Using math max on a table result in a record with for each column the maximum value.
assert equal ($table | math max) { name: Charlie age: 35 height: 183 }
Written with Nushell 0.109.1.