added example.

This commit is contained in:
Ben Yang 2024-07-19 10:38:16 +08:00
parent 22379c9846
commit 70283100fc

View File

@ -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 }",