mirror of
https://github.com/nushell/nushell.git
synced 2025-08-14 12:09:00 +02:00
@ -20,26 +20,26 @@ Aggregate a list table to a single value using an accumulator block.
|
||||
|
||||
Sum values of a list (same as 'math sum')
|
||||
```shell
|
||||
> [ 1 2 3 4 ] | reduce { $it.acc + $it.item }
|
||||
> [ 1 2 3 4 ] | reduce {|it, acc| $it + $acc }
|
||||
```
|
||||
|
||||
Sum values with a starting value (fold)
|
||||
```shell
|
||||
> [ 1 2 3 4 ] | reduce -f 10 { $it.acc + $it.item }
|
||||
> [ 1 2 3 4 ] | reduce -f 10 {|it, acc| $acc + $it }
|
||||
```
|
||||
|
||||
Replace selected characters in a string with 'X'
|
||||
```shell
|
||||
> [ i o t ] | reduce -f "Arthur, King of the Britons" { $it.acc | str find-replace -a $it.item "X" }
|
||||
> [ i o t ] | reduce -f "Arthur, King of the Britons" {|it, acc| $acc | str find-replace -a $it "X" }
|
||||
```
|
||||
|
||||
Find the longest string and its index
|
||||
```shell
|
||||
> [ one longest three bar ] | reduce -n {
|
||||
if ($it.item | str length) > ($it.acc | str length) {
|
||||
> [ one longest three bar ] | reduce -n { |it, acc|
|
||||
if ($it.item | str length) > ($acc | str length) {
|
||||
$it.item
|
||||
} else {
|
||||
$it.acc
|
||||
$acc
|
||||
}
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user