mirror of
https://github.com/nushell/nushell.git
synced 2025-08-15 23:17:53 +02:00
Autogenerate missing docs (#3514)
* Autogenerate missing docs * Update ansi.md * Rename question mark command docs * Delete empty?.md
This commit is contained in:
39
docs/commands/reduce.md
Normal file
39
docs/commands/reduce.md
Normal file
@ -0,0 +1,39 @@
|
||||
# reduce
|
||||
Aggregate a list table to a single value using an accumulator block.
|
||||
|
||||
Block must be (A, A) -> A unless --fold is selected, in which case it may be A, B -> A.
|
||||
|
||||
## Usage
|
||||
```shell
|
||||
> reduce <block> {flags}
|
||||
```
|
||||
|
||||
## Parameters
|
||||
* `<block>` reducing function
|
||||
|
||||
## Flags
|
||||
* -h, --help: Display this help message
|
||||
* -f, --fold <any>: reduce with initial value
|
||||
* -n, --numbered: returned a numbered item ($it.index and $it.item)
|
||||
|
||||
## Examples
|
||||
Simple summation (equivalent to math sum)
|
||||
```shell
|
||||
> echo 1 2 3 4 | reduce { $acc + $it }
|
||||
```
|
||||
|
||||
Summation from starting value using fold
|
||||
```shell
|
||||
> echo 1 2 3 4 | reduce -f (-1) { $acc + $it }
|
||||
```
|
||||
|
||||
Folding with rows
|
||||
```shell
|
||||
> <table> | reduce -f 1.6 { $acc * (echo $it.a | str to-int) + (echo $it.b | str to-int) }
|
||||
```
|
||||
|
||||
Numbered reduce to find index of longest word
|
||||
```shell
|
||||
> echo one longest three bar | reduce -n { if ($it.item | str length) > ($acc.item | str length) {echo $it} {echo $acc}} | get index
|
||||
```
|
||||
|
Reference in New Issue
Block a user