From 70283100fcdb34e9629668e6aeb3166502c9b29c Mon Sep 17 00:00:00 2001 From: Ben Yang Date: Fri, 19 Jul 2024 10:38:16 +0800 Subject: [PATCH] added example. --- crates/nu-command/src/filters/reduce.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/filters/reduce.rs b/crates/nu-command/src/filters/reduce.rs index 87fbe3b23e..72c3e53abe 100644 --- a/crates/nu-command/src/filters/reduce.rs +++ b/crates/nu-command/src/filters/reduce.rs @@ -36,7 +36,7 @@ impl Command for Reduce { } fn usage(&self) -> &str { - "Aggregate a list to a single value using an accumulator closure." + "Aggregate a list (starting from the left) to a single value using an accumulator closure." } fn search_terms(&self) -> Vec<&str> { @@ -50,6 +50,11 @@ impl Command for Reduce { description: "Sum values of a list (same as 'math sum')", result: Some(Value::test_int(10)), }, + Example { + example: "[ 1 2 3 4 ] | reduce {|it, acc| $acc - $it }", + description: r#"`reduce` accumulates value from left to right, equivalent to (((1 - 2) - 3) - 4)."#, + result: Some(Value::test_int(-8)), + }, Example { example: "[ 8 7 6 ] | enumerate | reduce --fold 0 {|it, acc| $acc + $it.item + $it.index }",