mirror of
https://github.com/nushell/nushell.git
synced 2024-11-25 18:03:51 +01:00
Add support for the argument to zip
being a closure (#11924)
<!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> [Related conversation on Discord](https://discord.com/channels/601130461678272522/615329862395101194/1209951539901366292) # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> This allows `zip` to consume two streams at the same time without having to choose to fully consume one of them. Helpful for combining infinite streams, or just large ones. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> Provides a way to consume another (possibly infinite) stream in `zip`, rather than that being limited to open ranges. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
This commit is contained in:
parent
6ff3a4180b
commit
cf68334fa0
@ -10,7 +10,7 @@ pub fn test_examples(cmd: impl Command + 'static) {
|
||||
mod test_examples {
|
||||
use super::super::{
|
||||
Ansi, Date, Enumerate, Filter, First, Flatten, From, Get, Into, IntoDatetime, IntoString,
|
||||
Math, MathRound, ParEach, Path, PathParse, Random, Sort, SortBy, Split, SplitColumn,
|
||||
Math, MathRound, ParEach, Path, PathParse, Random, Seq, Sort, SortBy, Split, SplitColumn,
|
||||
SplitRow, Str, StrJoin, StrLength, StrReplace, Update, Url, Values, Wrap,
|
||||
};
|
||||
use crate::{Each, To};
|
||||
@ -87,6 +87,7 @@ mod test_examples {
|
||||
working_set.add_decl(Box::new(PathParse));
|
||||
working_set.add_decl(Box::new(ParEach));
|
||||
working_set.add_decl(Box::new(Random));
|
||||
working_set.add_decl(Box::new(Seq));
|
||||
working_set.add_decl(Box::new(Sort));
|
||||
working_set.add_decl(Box::new(SortBy));
|
||||
working_set.add_decl(Box::new(Split));
|
||||
|
@ -1,4 +1,4 @@
|
||||
use nu_engine::CallExt;
|
||||
use nu_engine::{eval_block_with_early_return, CallExt};
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
@ -71,6 +71,14 @@ impl Command for Zip {
|
||||
Example {
|
||||
example: "1..3 | zip 4..6",
|
||||
description: "Zip two ranges",
|
||||
result: Some(Value::list(
|
||||
vec![test_row_1.clone(), test_row_2.clone(), test_row_3.clone()],
|
||||
Span::test_data(),
|
||||
)),
|
||||
},
|
||||
Example {
|
||||
example: "seq 1 3 | zip { seq 4 600000000 }",
|
||||
description: "Zip two streams",
|
||||
result: Some(Value::list(
|
||||
vec![test_row_1, test_row_2, test_row_3],
|
||||
Span::test_data(),
|
||||
@ -91,14 +99,31 @@ impl Command for Zip {
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let other: Value = call.req(engine_state, stack, 0)?;
|
||||
let head = call.head;
|
||||
let ctrlc = engine_state.ctrlc.clone();
|
||||
let metadata = input.metadata();
|
||||
|
||||
let other: PipelineData = match call.req(engine_state, stack, 0)? {
|
||||
// If a closure was provided, evaluate it and consume its stream output
|
||||
Value::Closure { val, .. } => {
|
||||
let block = engine_state.get_block(val.block_id);
|
||||
let mut stack = stack.captures_to_stack(val.captures);
|
||||
eval_block_with_early_return(
|
||||
engine_state,
|
||||
&mut stack,
|
||||
block,
|
||||
PipelineData::Empty,
|
||||
true,
|
||||
false,
|
||||
)?
|
||||
}
|
||||
// If any other value, use it as-is.
|
||||
val => val.into_pipeline_data(),
|
||||
};
|
||||
|
||||
Ok(input
|
||||
.into_iter()
|
||||
.zip(other.into_pipeline_data())
|
||||
.zip(other)
|
||||
.map(move |(x, y)| Value::list(vec![x, y], head))
|
||||
.into_pipeline_data_with_metadata(metadata, ctrlc))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user