2022-01-16 13:52:41 +00:00
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::{
|
|
|
|
ast::Call,
|
|
|
|
engine::{Command, EngineState, Stack},
|
2022-12-07 02:48:03 +00:00
|
|
|
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type,
|
|
|
|
Value,
|
2022-01-16 13:52:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Seq;
|
|
|
|
|
|
|
|
impl Command for Seq {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"seq"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("seq")
|
2022-11-09 23:24:57 +00:00
|
|
|
.input_output_types(vec![(Type::Nothing, Type::List(Box::new(Type::Number)))])
|
2023-12-15 06:32:37 +00:00
|
|
|
.rest("rest", SyntaxShape::Number, "Sequence values.")
|
2022-01-16 13:52:41 +00:00
|
|
|
.category(Category::Generators)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2022-11-09 22:19:02 +00:00
|
|
|
"Output sequences of numbers."
|
2022-01-16 13:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
_input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
seq(engine_state, stack, call)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
2022-11-09 22:19:02 +00:00
|
|
|
description: "sequence 1 to 10",
|
2022-01-16 13:52:41 +00:00
|
|
|
example: "seq 1 10",
|
2023-09-03 14:27:29 +00:00
|
|
|
result: Some(Value::list(
|
|
|
|
vec![
|
2022-01-16 13:52:41 +00:00
|
|
|
Value::test_int(1),
|
|
|
|
Value::test_int(2),
|
|
|
|
Value::test_int(3),
|
|
|
|
Value::test_int(4),
|
|
|
|
Value::test_int(5),
|
|
|
|
Value::test_int(6),
|
|
|
|
Value::test_int(7),
|
|
|
|
Value::test_int(8),
|
|
|
|
Value::test_int(9),
|
|
|
|
Value::test_int(10),
|
|
|
|
],
|
2023-09-03 14:27:29 +00:00
|
|
|
Span::test_data(),
|
|
|
|
)),
|
2022-01-16 13:52:41 +00:00
|
|
|
},
|
|
|
|
Example {
|
2022-11-09 22:19:02 +00:00
|
|
|
description: "sequence 1.0 to 2.0 by 0.1s",
|
2022-01-16 13:52:41 +00:00
|
|
|
example: "seq 1.0 0.1 2.0",
|
2023-09-03 14:27:29 +00:00
|
|
|
result: Some(Value::list(
|
|
|
|
vec![
|
2022-01-16 13:52:41 +00:00
|
|
|
Value::test_float(1.0000),
|
|
|
|
Value::test_float(1.1000),
|
|
|
|
Value::test_float(1.2000),
|
|
|
|
Value::test_float(1.3000),
|
|
|
|
Value::test_float(1.4000),
|
|
|
|
Value::test_float(1.5000),
|
|
|
|
Value::test_float(1.6000),
|
|
|
|
Value::test_float(1.7000),
|
|
|
|
Value::test_float(1.8000),
|
|
|
|
Value::test_float(1.9000),
|
|
|
|
Value::test_float(2.0000),
|
|
|
|
],
|
2023-09-03 14:27:29 +00:00
|
|
|
Span::test_data(),
|
|
|
|
)),
|
2022-01-16 13:52:41 +00:00
|
|
|
},
|
|
|
|
Example {
|
2022-11-09 22:19:02 +00:00
|
|
|
description: "sequence 1 to 5, then convert to a string with a pipe separator",
|
|
|
|
example: "seq 1 5 | str join '|'",
|
2022-11-09 23:24:57 +00:00
|
|
|
result: None,
|
2022-01-16 13:52:41 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn seq(
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
let span = call.head;
|
|
|
|
let rest_nums: Vec<Spanned<f64>> = call.rest(engine_state, stack, 0)?;
|
|
|
|
|
2022-12-07 02:48:03 +00:00
|
|
|
// note that the check for int or float has to occur here. prior, the check would occur after
|
|
|
|
// everything had been generated; this does not work well with ListStreams.
|
|
|
|
// As such, the simple test is to check if this errors out: that means there is a float in the
|
|
|
|
// input, which necessarily means that parts of the output will be floats.
|
|
|
|
let rest_nums_check: Result<Vec<Spanned<i64>>, ShellError> = call.rest(engine_state, stack, 0);
|
|
|
|
let contains_decimals = rest_nums_check.is_err();
|
|
|
|
|
2022-01-16 13:52:41 +00:00
|
|
|
if rest_nums.is_empty() {
|
2023-12-06 23:40:03 +00:00
|
|
|
return Err(ShellError::GenericError {
|
|
|
|
error: "seq requires some parameters".into(),
|
|
|
|
msg: "needs parameter".into(),
|
|
|
|
span: Some(call.head),
|
|
|
|
help: None,
|
|
|
|
inner: vec![],
|
|
|
|
});
|
2022-01-16 13:52:41 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 22:19:02 +00:00
|
|
|
let rest_nums: Vec<f64> = rest_nums.iter().map(|n| n.item).collect();
|
2022-01-16 13:52:41 +00:00
|
|
|
|
2022-12-07 02:48:03 +00:00
|
|
|
run_seq(rest_nums, span, contains_decimals, engine_state)
|
2022-01-16 13:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(Seq {})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 02:48:03 +00:00
|
|
|
pub fn run_seq(
|
|
|
|
free: Vec<f64>,
|
|
|
|
span: Span,
|
|
|
|
contains_decimals: bool,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
2022-11-09 22:19:02 +00:00
|
|
|
let first = free[0];
|
2022-12-07 02:48:03 +00:00
|
|
|
let step = if free.len() > 2 { free[1] } else { 1.0 };
|
2022-11-09 22:19:02 +00:00
|
|
|
let last = { free[free.len() - 1] };
|
2022-01-16 13:52:41 +00:00
|
|
|
|
2022-12-07 02:48:03 +00:00
|
|
|
if !contains_decimals {
|
|
|
|
// integers only
|
|
|
|
Ok(PipelineData::ListStream(
|
2024-01-30 14:41:14 +00:00
|
|
|
nu_protocol::ListStream::from_stream(
|
|
|
|
IntSeq {
|
2022-12-07 02:48:03 +00:00
|
|
|
count: first as i64,
|
|
|
|
step: step as i64,
|
|
|
|
last: last as i64,
|
|
|
|
span,
|
2024-01-30 14:41:14 +00:00
|
|
|
},
|
|
|
|
engine_state.ctrlc.clone(),
|
|
|
|
),
|
2022-12-07 02:48:03 +00:00
|
|
|
None,
|
|
|
|
))
|
2022-01-16 13:52:41 +00:00
|
|
|
} else {
|
2022-12-07 02:48:03 +00:00
|
|
|
// floats
|
|
|
|
Ok(PipelineData::ListStream(
|
2024-01-30 14:41:14 +00:00
|
|
|
nu_protocol::ListStream::from_stream(
|
|
|
|
FloatSeq {
|
2022-12-07 02:48:03 +00:00
|
|
|
first,
|
|
|
|
step,
|
|
|
|
last,
|
|
|
|
index: 0,
|
|
|
|
span,
|
2024-01-30 14:41:14 +00:00
|
|
|
},
|
|
|
|
engine_state.ctrlc.clone(),
|
|
|
|
),
|
2022-12-07 02:48:03 +00:00
|
|
|
None,
|
|
|
|
))
|
2022-01-16 13:52:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 02:48:03 +00:00
|
|
|
struct FloatSeq {
|
|
|
|
first: f64,
|
|
|
|
step: f64,
|
|
|
|
last: f64,
|
|
|
|
index: isize,
|
|
|
|
span: Span,
|
|
|
|
}
|
2022-01-16 13:52:41 +00:00
|
|
|
|
2022-12-07 02:48:03 +00:00
|
|
|
impl Iterator for FloatSeq {
|
|
|
|
type Item = Value;
|
|
|
|
fn next(&mut self) -> Option<Value> {
|
|
|
|
let count = self.first + self.index as f64 * self.step;
|
|
|
|
// Accuracy guaranteed as far as possible; each time, the value is re-evaluated from the
|
|
|
|
// base arguments
|
|
|
|
if (count > self.last && self.step >= 0.0) || (count < self.last && self.step <= 0.0) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
self.index += 1;
|
Reduced LOC by replacing several instances of `Value::Int {}`, `Value::Float{}`, `Value::Bool {}`, and `Value::String {}` with `Value::int()`, `Value::float()`, `Value::boolean()` and `Value::string()` (#7412)
# Description
While perusing Value.rs, I noticed the `Value::int()`, `Value::float()`,
`Value::boolean()` and `Value::string()` constructors, which seem
designed to make it easier to construct various Values, but which aren't
used often at all in the codebase. So, using a few find-replaces
regexes, I increased their usage. This reduces overall LOC because
structures like this:
```
Value::Int {
val: a,
span: head
}
```
are changed into
```
Value::int(a, head)
```
and are respected as such by the project's formatter.
There are little readability concerns because the second argument to all
of these is `span`, and it's almost always extremely obvious which is
the span at every callsite.
# User-Facing Changes
None.
# 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 -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# 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.
2022-12-09 16:37:51 +00:00
|
|
|
Some(Value::float(count, self.span))
|
2022-12-07 02:48:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-16 13:52:41 +00:00
|
|
|
|
2022-12-07 02:48:03 +00:00
|
|
|
struct IntSeq {
|
|
|
|
count: i64,
|
|
|
|
step: i64,
|
|
|
|
last: i64,
|
|
|
|
span: Span,
|
2022-01-16 13:52:41 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 02:48:03 +00:00
|
|
|
impl Iterator for IntSeq {
|
|
|
|
type Item = Value;
|
|
|
|
fn next(&mut self) -> Option<Value> {
|
|
|
|
if (self.count > self.last && self.step >= 0) || (self.count < self.last && self.step <= 0)
|
|
|
|
{
|
|
|
|
return None;
|
2022-01-16 13:52:41 +00:00
|
|
|
}
|
Reduced LOC by replacing several instances of `Value::Int {}`, `Value::Float{}`, `Value::Bool {}`, and `Value::String {}` with `Value::int()`, `Value::float()`, `Value::boolean()` and `Value::string()` (#7412)
# Description
While perusing Value.rs, I noticed the `Value::int()`, `Value::float()`,
`Value::boolean()` and `Value::string()` constructors, which seem
designed to make it easier to construct various Values, but which aren't
used often at all in the codebase. So, using a few find-replaces
regexes, I increased their usage. This reduces overall LOC because
structures like this:
```
Value::Int {
val: a,
span: head
}
```
are changed into
```
Value::int(a, head)
```
and are respected as such by the project's formatter.
There are little readability concerns because the second argument to all
of these is `span`, and it's almost always extremely obvious which is
the span at every callsite.
# User-Facing Changes
None.
# 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 -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# 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.
2022-12-09 16:37:51 +00:00
|
|
|
let ret = Some(Value::int(self.count, self.span));
|
2022-12-07 02:48:03 +00:00
|
|
|
self.count += self.step;
|
|
|
|
ret
|
2022-01-16 13:52:41 +00:00
|
|
|
}
|
|
|
|
}
|