Skip to main content

nu_command/generators/
seq.rs

1use nu_engine::command_prelude::*;
2use nu_protocol::{ListStream, shell_error::generic::GenericError};
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::Generic(GenericError::new(
100            "seq requires some parameters",
101            "needs parameter",
102            call.head,
103        )));
104    }
105
106    let rest_nums: Vec<f64> = rest_nums.iter().map(|n| n.item).collect();
107
108    run_seq(rest_nums, span, contains_decimals, engine_state)
109}
110
111pub fn run_seq(
112    free: Vec<f64>,
113    span: Span,
114    contains_decimals: bool,
115    engine_state: &EngineState,
116) -> Result<PipelineData, ShellError> {
117    let first = free[0];
118    let step = if free.len() > 2 { free[1] } else { 1.0 };
119    let last = { free[free.len() - 1] };
120
121    let stream = if !contains_decimals {
122        ListStream::new(
123            IntSeq {
124                count: first as i64,
125                step: step as i64,
126                last: last as i64,
127                span,
128            },
129            span,
130            engine_state.signals().clone(),
131        )
132    } else {
133        ListStream::new(
134            FloatSeq {
135                first,
136                step,
137                last,
138                index: 0,
139                span,
140            },
141            span,
142            engine_state.signals().clone(),
143        )
144    };
145
146    Ok(stream.into())
147}
148
149struct FloatSeq {
150    first: f64,
151    step: f64,
152    last: f64,
153    index: isize,
154    span: Span,
155}
156
157impl Iterator for FloatSeq {
158    type Item = Value;
159    fn next(&mut self) -> Option<Value> {
160        let count = self.first + self.index as f64 * self.step;
161        // Accuracy guaranteed as far as possible; each time, the value is re-evaluated from the
162        // base arguments
163        if (count > self.last && self.step >= 0.0) || (count < self.last && self.step <= 0.0) {
164            return None;
165        }
166        self.index += 1;
167        Some(Value::float(count, self.span))
168    }
169}
170
171struct IntSeq {
172    count: i64,
173    step: i64,
174    last: i64,
175    span: Span,
176}
177
178impl Iterator for IntSeq {
179    type Item = Value;
180    fn next(&mut self) -> Option<Value> {
181        if (self.count > self.last && self.step >= 0) || (self.count < self.last && self.step <= 0)
182        {
183            return None;
184        }
185        let ret = Some(Value::int(self.count, self.span));
186        self.count += self.step;
187        ret
188    }
189}
190
191#[cfg(test)]
192mod tests {
193    use super::*;
194
195    #[test]
196    fn test_examples() -> nu_test_support::Result {
197        nu_test_support::test().examples(Seq)
198    }
199}