Require block params (#4505)

* Require block params

* Improve errors
This commit is contained in:
JT
2022-02-17 06:40:24 -05:00
committed by GitHub
parent f169a9be3b
commit 6e733f49bc
27 changed files with 149 additions and 102 deletions

View File

@ -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

View File

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

View File

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

View File

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

View File

@ -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![

View File

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

View File

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

View File

@ -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),

View File

@ -5,7 +5,7 @@ fn each_works_separately() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo [1 2 3] | each { echo $it 10 | math sum } | to json -r
echo [1 2 3] | each { |it| echo $it 10 | math sum } | to json -r
"#
));
@ -17,7 +17,7 @@ fn each_group_works() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo [1 2 3 4 5 6] | each group 3 { $it } | to json --raw
echo [1 2 3 4 5 6] | each group 3 { |it| $it } | to json --raw
"#
));
@ -29,7 +29,7 @@ fn each_window() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo [1 2 3 4] | each window 3 { $it } | to json --raw
echo [1 2 3 4] | each window 3 { |it| $it } | to json --raw
"#
));
@ -41,7 +41,7 @@ fn each_window_stride() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo [1 2 3 4 5 6] | each window 3 -s 2 { echo $it } | to json --raw
echo [1 2 3 4 5 6] | each window 3 -s 2 { |it| echo $it } | to json --raw
"#
));
@ -65,7 +65,7 @@ fn each_implicit_it_in_block() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo [[foo bar]; [a b] [c d] [e f]] | each { nu --testbin cococo $it.foo } | str collect
echo [[foo bar]; [a b] [c d] [e f]] | each { |it| nu --testbin cococo $it.foo } | str collect
"#
));

View File

@ -17,7 +17,7 @@ fn echo_range_handles_inclusive() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo 1..3 | each { $it } | to json --raw
echo 1..3 | each { |x| $x } | to json --raw
"#
));
@ -29,7 +29,7 @@ fn echo_range_handles_exclusive() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo 1..<3 | each { $it } | to json --raw
echo 1..<3 | each { |x| $x } | to json --raw
"#
));
@ -41,7 +41,7 @@ fn echo_range_handles_inclusive_down() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo 3..1 | each { $it } | to json --raw
echo 3..1 | each { |it| $it } | to json --raw
"#
));
@ -53,7 +53,7 @@ fn echo_range_handles_exclusive_down() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo 3..<1 | each { $it } | to json --raw
echo 3..<1 | each { |it| $it } | to json --raw
"#
));

View File

@ -5,7 +5,7 @@ fn into_int_filesize() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo 1kb | into int | each { $it / 1000 }
echo 1kb | into int | each { |it| $it / 1000 }
"#
));
@ -17,7 +17,7 @@ fn into_int_filesize2() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo 1kib | into int | each { $it / 1024 }
echo 1kib | into int | each { |it| $it / 1024 }
"#
));
@ -29,7 +29,7 @@ fn into_int_int() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo 1024 | into int | each { $it / 1024 }
echo 1024 | into int | each { |it| $it / 1024 }
"#
));

View File

@ -87,7 +87,7 @@ fn lists_all_files_in_directories_from_stream() {
cwd: dirs.test(), pipeline(
r#"
echo dir_a dir_b
| each { ls $it }
| each { |it| ls $it }
| flatten | length
"#
));

View File

@ -22,7 +22,7 @@ mod simple {
r#"
open key_value_separated_arepa_ingredients.txt
| lines
| each { echo $it | parse "{Name}={Value}" }
| each { |it| echo $it | parse "{Name}={Value}" }
| flatten
| get 1
| get Value

View File

@ -8,7 +8,7 @@ fn reduce_table_column() {
echo "[{month:2,total:30}, {month:3,total:10}, {month:4,total:3}, {month:5,total:60}]"
| from json
| get total
| reduce -f 20 { $it.item + (math eval $"($it.acc)^1.05")}
| reduce -f 20 { |it, acc| $it + (math eval $"($acc)^1.05")}
| into string -d 1
"#
)
@ -23,7 +23,7 @@ fn reduce_table_column_with_path() {
cwd: ".", pipeline(
r#"
[{month:2,total:30}, {month:3,total:10}, {month:4,total:3}, {month:5,total:60}]
| reduce -f 20 { $it.item.total + (math eval $"($it.acc)^1.05")}
| reduce -f 20 { |it, acc| $it.total + (math eval $"($acc)^1.05")}
| into string -d 1
"#
)
@ -38,7 +38,7 @@ fn reduce_rows_example() {
cwd: ".", pipeline(
r#"
[[a,b]; [1,2] [3,4]]
| reduce -f 1.6 { $it.acc * ($it.item.a | into int) + ($it.item.b | into int) }
| reduce -f 1.6 { |it, acc| $acc * ($it.a | into int) + ($it.b | into int) }
"#
)
);
@ -54,7 +54,7 @@ fn reduce_numbered_example() {
cwd: ".", pipeline(
r#"
echo one longest three bar
reduce -n { if ($it.item | str length) > ($acc.item | str length) {echo $it} {echo $acc}}
reduce -n { |it, acc| if ($it | str length) > ($acc | str length) {echo $it} else {echo $acc}}
| get index
"#
)
@ -69,7 +69,7 @@ fn reduce_numbered_integer_addition_example() {
cwd: ".", pipeline(
r#"
echo [1 2 3 4]
| reduce -n { $it.acc + $it.item }
| reduce -n { |it, acc| $acc + $it.item }
| get item
"#
)
@ -84,9 +84,9 @@ fn folding_with_tables() {
cwd: ".", pipeline(
r#"
echo [10 20 30 40]
| reduce -f [] {
with-env [value $it.item] {
echo $it.acc | append (10 * ($env.value | into int))
| reduce -f [] { |it, acc|
with-env [value $it] {
echo $acc | append (10 * ($env.value | into int))
}
}
| math sum
@ -102,7 +102,7 @@ fn error_reduce_fold_type_mismatch() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo a b c | reduce -f 0 { $it.acc + $it.item }
echo a b c | reduce -f 0 { |it, acc| $acc + $it }
"#
)
);
@ -115,7 +115,7 @@ fn error_reduce_empty() {
let actual = nu!(
cwd: ".", pipeline(
r#"
reduce { $it.$acc + $it.item }
reduce { |it, acc| $acc + $it }
"#
)
);

View File

@ -135,7 +135,7 @@ mod columns {
transpose bit --ignore-titles
| get bit
| reverse
| each --numbered {
| each --numbered { |it|
$it.item * (2 ** $it.index)
}
| math sum
@ -155,7 +155,7 @@ mod columns {
pipeline(
r#"
split chars
| each { $it | into int }
| each { |it| $it | into int }
| rotate --ccw
| rename bit1 bit2 bit3 bit4 bit5 bit6 bit7 bit8
"#

View File

@ -44,7 +44,7 @@ fn sum_one_to_four() {
let actual = nu!(
cwd: ".", pipeline(
r#"
1..4 | each { $it } | into string | str collect "+" | math eval
1..4 | each { |it| $it } | into string | str collect "+" | math eval
"#
)
);