forked from extern/nushell
@ -303,7 +303,7 @@ The `each` command gives us a way of working with each individual row or
|
||||
element of a list one at a time. It reads these in from the pipeline and
|
||||
runs a block on each element. A block is a group of pipelines.
|
||||
```
|
||||
echo 1 2 3 | each { $it + 10}
|
||||
echo 1 2 3 | each { |it| $it + 10}
|
||||
```
|
||||
This example iterates over each element sent by `echo`, giving us three new
|
||||
values that are the original value + 10. Here, the `$it` is a variable that
|
||||
|
@ -46,7 +46,7 @@ impl Command for Each {
|
||||
];
|
||||
|
||||
vec![Example {
|
||||
example: "[1 2 3] | each { 2 * $it }",
|
||||
example: "[1 2 3] | each { |it| 2 * $it }",
|
||||
description: "Multiplies elements in list",
|
||||
result: Some(Value::List {
|
||||
vals: stream_test_1,
|
||||
|
@ -42,7 +42,7 @@ impl Command for EachGroup {
|
||||
];
|
||||
|
||||
vec![Example {
|
||||
example: "echo [1 2 3 4] | each group 2 { $it.0 + $it.1 }",
|
||||
example: "echo [1 2 3 4] | each group 2 { |it| $it.0 + $it.1 }",
|
||||
description: "Echo the sum of each pair",
|
||||
result: Some(Value::List {
|
||||
vals: stream_test_1,
|
||||
|
@ -68,7 +68,7 @@ impl Command for EachWindow {
|
||||
|
||||
vec![
|
||||
Example {
|
||||
example: "echo [1 2 3 4] | each window 2 { $it.0 + $it.1 }",
|
||||
example: "echo [1 2 3 4] | each window 2 { |it| $it.0 + $it.1 }",
|
||||
description: "A sliding window of two elements",
|
||||
result: Some(Value::List {
|
||||
vals: stream_test_1,
|
||||
|
@ -72,7 +72,7 @@ impl Command for Empty {
|
||||
},
|
||||
Example {
|
||||
description: "use a block if setting the empty cell contents is wanted",
|
||||
example: "[[2020/04/16 2020/07/10 2020/11/16]; ['' [27] [37]]] | empty? 2020/04/16 -b { [33 37] }",
|
||||
example: "[[2020/04/16 2020/07/10 2020/11/16]; ['' [27] [37]]] | empty? 2020/04/16 -b { |_| [33 37] }",
|
||||
result: Some(
|
||||
Value::List {
|
||||
vals: vec![
|
||||
|
@ -60,7 +60,7 @@ impl Command for Find {
|
||||
},
|
||||
Example {
|
||||
description: "Find the first odd value",
|
||||
example: "echo [2 4 3 6 5 8] | find --predicate { ($it mod 2) == 1 }",
|
||||
example: "echo [2 4 3 6 5 8] | find --predicate { |it| ($it mod 2) == 1 }",
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::test_int(3), Value::test_int(5)],
|
||||
span: Span::test_data()
|
||||
@ -68,7 +68,7 @@ impl Command for Find {
|
||||
},
|
||||
Example {
|
||||
description: "Find if a service is not running",
|
||||
example: "echo [[version patch]; [0.1.0 $false] [0.1.1 $true] [0.2.0 $false]] | find -p { $it.patch }",
|
||||
example: "echo [[version patch]; [0.1.0 $false] [0.1.1 $true] [0.2.0 $false]] | find -p { |it| $it.patch }",
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::test_record(
|
||||
vec!["version", "patch"],
|
||||
|
@ -32,7 +32,7 @@ impl Command for ParEach {
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
example: "[1 2 3] | par-each { 2 * $it }",
|
||||
example: "[1 2 3] | par-each { |it| 2 * $it }",
|
||||
description: "Multiplies elements in list",
|
||||
result: None,
|
||||
}]
|
||||
|
@ -24,7 +24,7 @@ impl Command for Reduce {
|
||||
)
|
||||
.required(
|
||||
"block",
|
||||
SyntaxShape::Block(Some(vec![SyntaxShape::Any])),
|
||||
SyntaxShape::Block(Some(vec![SyntaxShape::Any, SyntaxShape::Any])),
|
||||
"reducing function",
|
||||
)
|
||||
.switch("numbered", "iterate with an index", Some('n'))
|
||||
@ -37,7 +37,7 @@ impl Command for Reduce {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
example: "[ 1 2 3 4 ] | reduce { $it.acc + $it.item }",
|
||||
example: "[ 1 2 3 4 ] | reduce {|it, acc| $it + $acc }",
|
||||
description: "Sum values of a list (same as 'math sum')",
|
||||
result: Some(Value::Int {
|
||||
val: 10,
|
||||
@ -45,7 +45,7 @@ impl Command for Reduce {
|
||||
}),
|
||||
},
|
||||
Example {
|
||||
example: "[ 1 2 3 4 ] | reduce -f 10 { $it.acc + $it.item }",
|
||||
example: "[ 1 2 3 4 ] | reduce -f 10 {|it, acc| $acc + $it }",
|
||||
description: "Sum values with a starting value (fold)",
|
||||
result: Some(Value::Int {
|
||||
val: 20,
|
||||
@ -53,7 +53,7 @@ impl Command for Reduce {
|
||||
}),
|
||||
},
|
||||
Example {
|
||||
example: r#"[ i o t ] | reduce -f "Arthur, King of the Britons" { $it.acc | str find-replace -a $it.item "X" }"#,
|
||||
example: r#"[ i o t ] | reduce -f "Arthur, King of the Britons" {|it, acc| $acc | str find-replace -a $it "X" }"#,
|
||||
description: "Replace selected characters in a string with 'X'",
|
||||
result: Some(Value::String {
|
||||
val: "ArXhur, KXng Xf Xhe BrXXXns".to_string(),
|
||||
@ -61,11 +61,11 @@ impl Command for Reduce {
|
||||
}),
|
||||
},
|
||||
Example {
|
||||
example: r#"[ one longest three bar ] | reduce -n {
|
||||
if ($it.item | str length) > ($it.acc | str length) {
|
||||
example: r#"[ one longest three bar ] | reduce -n { |it, acc|
|
||||
if ($it.item | str length) > ($acc | str length) {
|
||||
$it.item
|
||||
} else {
|
||||
$it.acc
|
||||
$acc
|
||||
}
|
||||
}"#,
|
||||
description: "Find the longest string and its index",
|
||||
@ -147,32 +147,28 @@ impl Command for Reduce {
|
||||
if let Some(var_id) = &var.var_id {
|
||||
let it = if numbered {
|
||||
Value::Record {
|
||||
cols: vec![
|
||||
"index".to_string(),
|
||||
"acc".to_string(),
|
||||
"item".to_string(),
|
||||
],
|
||||
cols: vec!["index".to_string(), "item".to_string()],
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: idx as i64 + off,
|
||||
span,
|
||||
},
|
||||
acc,
|
||||
x,
|
||||
],
|
||||
span,
|
||||
}
|
||||
} else {
|
||||
Value::Record {
|
||||
cols: vec!["acc".to_string(), "item".to_string()],
|
||||
vals: vec![acc, x],
|
||||
span,
|
||||
}
|
||||
x
|
||||
};
|
||||
|
||||
stack.add_var(*var_id, it);
|
||||
}
|
||||
}
|
||||
if let Some(var) = block.signature.get_positional(1) {
|
||||
if let Some(var_id) = &var.var_id {
|
||||
stack.add_var(*var_id, acc);
|
||||
}
|
||||
}
|
||||
|
||||
let v = match eval_block(engine_state, &mut stack, block, PipelineData::new(span)) {
|
||||
Ok(v) => v.into_value(span),
|
||||
|
Reference in New Issue
Block a user