nu_command/generators/
seq.rs

1use nu_engine::command_prelude::*;
2use nu_protocol::ListStream;
3
4#[derive(Clone)]
5pub struct Seq;
6
7impl Command for Seq {
8    fn name(&self) -> &str {
9        "seq"
10    }
11
12    fn signature(&self) -> Signature {
13        Signature::build("seq")
14            .input_output_types(vec![(Type::Nothing, Type::List(Box::new(Type::Number)))])
15            .rest("rest", SyntaxShape::Number, "Sequence values.")
16            .category(Category::Generators)
17    }
18
19    fn description(&self) -> &str {
20        "Output sequences of numbers."
21    }
22
23    fn run(
24        &self,
25        engine_state: &EngineState,
26        stack: &mut Stack,
27        call: &Call,
28        _input: PipelineData,
29    ) -> Result<PipelineData, ShellError> {
30        seq(engine_state, stack, call)
31    }
32
33    fn examples(&self) -> Vec<Example> {
34        vec![
35            Example {
36                description: "sequence 1 to 10",
37                example: "seq 1 10",
38                result: Some(Value::list(
39                    vec![
40                        Value::test_int(1),
41                        Value::test_int(2),
42                        Value::test_int(3),
43                        Value::test_int(4),
44                        Value::test_int(5),
45                        Value::test_int(6),
46                        Value::test_int(7),
47                        Value::test_int(8),
48                        Value::test_int(9),
49                        Value::test_int(10),
50                    ],
51                    Span::test_data(),
52                )),
53            },
54            Example {
55                description: "sequence 1.0 to 2.0 by 0.1s",
56                example: "seq 1.0 0.1 2.0",
57                result: Some(Value::list(
58                    vec![
59                        Value::test_float(1.0000),
60                        Value::test_float(1.1000),
61                        Value::test_float(1.2000),
62                        Value::test_float(1.3000),
63                        Value::test_float(1.4000),
64                        Value::test_float(1.5000),
65                        Value::test_float(1.6000),
66                        Value::test_float(1.7000),
67                        Value::test_float(1.8000),
68                        Value::test_float(1.9000),
69                        Value::test_float(2.0000),
70                    ],
71                    Span::test_data(),
72                )),
73            },
74            Example {
75                description: "sequence 1 to 5, then convert to a string with a pipe separator",
76                example: "seq 1 5 | str join '|'",
77                result: None,
78            },
79        ]
80    }
81}
82
83fn seq(
84    engine_state: &EngineState,
85    stack: &mut Stack,
86    call: &Call,
87) -> Result<PipelineData, ShellError> {
88    let span = call.head;
89    let rest_nums: Vec<Spanned<f64>> = call.rest(engine_state, stack, 0)?;
90
91    // note that the check for int or float has to occur here. prior, the check would occur after
92    // everything had been generated; this does not work well with ListStreams.
93    // As such, the simple test is to check if this errors out: that means there is a float in the
94    // input, which necessarily means that parts of the output will be floats.
95    let rest_nums_check: Result<Vec<Spanned<i64>>, ShellError> = call.rest(engine_state, stack, 0);
96    let contains_decimals = rest_nums_check.is_err();
97
98    if rest_nums.is_empty() {
99        return Err(ShellError::GenericError {
100            error: "seq requires some parameters".into(),
101            msg: "needs parameter".into(),
102            span: Some(call.head),
103            help: None,
104            inner: vec![],
105        });
106    }
107
108    let rest_nums: Vec<f64> = rest_nums.iter().map(|n| n.item).collect();
109
110    run_seq(rest_nums, span, contains_decimals, engine_state)
111}
112
113pub fn run_seq(
114    free: Vec<f64>,
115    span: Span,
116    contains_decimals: bool,
117    engine_state: &EngineState,
118) -> Result<PipelineData, ShellError> {
119    let first = free[0];
120    let step = if free.len() > 2 { free[1] } else { 1.0 };
121    let last = { free[free.len() - 1] };
122
123    let stream = if !contains_decimals {
124        ListStream::new(
125            IntSeq {
126                count: first as i64,
127                step: step as i64,
128                last: last as i64,
129                span,
130            },
131            span,
132            engine_state.signals().clone(),
133        )
134    } else {
135        ListStream::new(
136            FloatSeq {
137                first,
138                step,
139                last,
140                index: 0,
141                span,
142            },
143            span,
144            engine_state.signals().clone(),
145        )
146    };
147
148    Ok(stream.into())
149}
150
151struct FloatSeq {
152    first: f64,
153    step: f64,
154    last: f64,
155    index: isize,
156    span: Span,
157}
158
159impl Iterator for FloatSeq {
160    type Item = Value;
161    fn next(&mut self) -> Option<Value> {
162        let count = self.first + self.index as f64 * self.step;
163        // Accuracy guaranteed as far as possible; each time, the value is re-evaluated from the
164        // base arguments
165        if (count > self.last && self.step >= 0.0) || (count < self.last && self.step <= 0.0) {
166            return None;
167        }
168        self.index += 1;
169        Some(Value::float(count, self.span))
170    }
171}
172
173struct IntSeq {
174    count: i64,
175    step: i64,
176    last: i64,
177    span: Span,
178}
179
180impl Iterator for IntSeq {
181    type Item = Value;
182    fn next(&mut self) -> Option<Value> {
183        if (self.count > self.last && self.step >= 0) || (self.count < self.last && self.step <= 0)
184        {
185            return None;
186        }
187        let ret = Some(Value::int(self.count, self.span));
188        self.count += self.step;
189        ret
190    }
191}
192
193#[cfg(test)]
194mod tests {
195    use super::*;
196
197    #[test]
198    fn test_examples() {
199        use crate::test_examples;
200
201        test_examples(Seq {})
202    }
203}