mirror of
https://github.com/nushell/nushell
synced 2024-12-28 14:03:09 +00:00
parent
77f42931ff
commit
f9f74a0f7d
2 changed files with 26 additions and 1 deletions
|
@ -155,7 +155,17 @@ fn reduce(raw_args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||||
let result = process_row(block, &*context, row);
|
let result = process_row(block, &*context, row);
|
||||||
context.scope.exit_scope();
|
context.scope.exit_scope();
|
||||||
|
|
||||||
result
|
// we make sure that result is an indexed item
|
||||||
|
result.and_then(|mut acc| {
|
||||||
|
let values = acc.drain_vec();
|
||||||
|
let value = values
|
||||||
|
.get(0)
|
||||||
|
.ok_or_else(|| ShellError::unexpected("No value to update with"))?;
|
||||||
|
Ok(InputStream::one(match value.value {
|
||||||
|
UntaggedValue::Primitive(_) => each::make_indexed_item(0, value.clone()),
|
||||||
|
_ => value.clone(),
|
||||||
|
}))
|
||||||
|
})
|
||||||
})?
|
})?
|
||||||
.to_action_stream())
|
.to_action_stream())
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -61,6 +61,21 @@ fn reduce_numbered_example() {
|
||||||
assert_eq!(actual.out, "1");
|
assert_eq!(actual.out, "1");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn reduce_numbered_integer_addition_example() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".", pipeline(
|
||||||
|
r#"
|
||||||
|
echo [1 2 3 4]
|
||||||
|
| reduce -n {= $acc.item + $it.item }
|
||||||
|
| get item
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(actual.out, "10");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn folding_with_tables() {
|
fn folding_with_tables() {
|
||||||
let actual = nu!(
|
let actual = nu!(
|
||||||
|
|
Loading…
Reference in a new issue