nushell/docs/commands/reduce.md
Justin Ma f3626f7c3a
Update docs for open and decode command, regenerate all docs (#4815)
* Update docs for open and decode command, regenerate all docs

* Update open.rs

* Update open.md

Co-authored-by: JT <547158+jntrnr@users.noreply.github.com>
2022-03-11 05:39:54 -05:00

1007 B

title layout version
reduce command 0.59.1

Aggregate a list table to a single value using an accumulator block.

Signature

> reduce (block) --fold --numbered

Parameters

  • block: reducing function
  • --fold {any}: reduce with initial value
  • --numbered: iterate with an index

Examples

Sum values of a list (same as 'math sum')

> [ 1 2 3 4 ] | reduce {|it, acc| $it + $acc }

Sum values with a starting value (fold)

> [ 1 2 3 4 ] | reduce -f 10 {|it, acc| $acc + $it }

Replace selected characters in a string with '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

> [ one longest three bar ] | reduce -n { |it, acc|
                    if ($it.item | str length) > ($acc | str length) {
                        $it.item
                    } else {
                        $acc
                    }
                }