nushell/docs/commands/reduce.md
JT bff81f24aa
Autogenerate missing docs (#3514)
* Autogenerate missing docs

* Update ansi.md

* Rename question mark command docs

* Delete empty?.md
2021-05-30 12:57:04 +12:00

964 B

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

> reduce <block> {flags} 

Parameters

  • <block> reducing function

Flags

  • -h, --help: Display this help message
  • -f, --fold : reduce with initial value
  • -n, --numbered: returned a numbered item ($it.index and $it.item)

Examples

Simple summation (equivalent to math sum)

> echo 1 2 3 4 | reduce { $acc + $it }

Summation from starting value using fold

> echo 1 2 3 4 | reduce -f (-1) { $acc + $it }

Folding with rows

> <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

> echo one longest three bar | reduce -n { if ($it.item | str length) > ($acc.item | str length) {echo $it} {echo $acc}} | get index