1use super::utils::chain_error_with_input;
2use nu_engine::{ClosureEval, ClosureEvalOnce, command_prelude::*};
3use nu_protocol::engine::Closure;
4
5#[derive(Clone)]
6pub struct Each;
7
8impl Command for Each {
9 fn name(&self) -> &str {
10 "each"
11 }
12
13 fn description(&self) -> &str {
14 "Run a closure on each row of the input list, creating a new list with the results."
15 }
16
17 fn extra_description(&self) -> &str {
18 r#"Since tables are lists of records, passing a table into 'each' will
19iterate over each record, not necessarily each cell within it.
20
21Avoid passing single records to this command. Since a record is a
22one-row structure, 'each' will only run once, behaving similar to 'do'.
23To iterate over a record's values, use 'items' or try converting it to a table
24with 'transpose' first.
25
26
27By default, for each input there is a single output value.
28If the closure returns a stream rather than value, the stream is collected
29completely, and the resulting value becomes one of the items in `each`'s output.
30
31To receive items from those streams without waiting for the whole stream to be
32collected, `each --flatten` can be used.
33Instead of waiting for the stream to be collected before returning the result as
34a single item, `each --flatten` will return each item as soon as they are received.
35
36This "flattens" the output, turning an output that would otherwise be a
37list of lists like `list<list<string>>` into a flat list like `list<string>`."#
38 }
39
40 fn search_terms(&self) -> Vec<&str> {
41 vec!["for", "loop", "iterate", "map"]
42 }
43
44 fn signature(&self) -> nu_protocol::Signature {
45 Signature::build("each")
46 .input_output_types(vec![
47 (
48 Type::List(Box::new(Type::Any)),
49 Type::List(Box::new(Type::Any)),
50 ),
51 (Type::table(), Type::List(Box::new(Type::Any))),
52 (Type::Any, Type::Any),
53 ])
54 .required(
55 "closure",
56 SyntaxShape::Closure(Some(vec![SyntaxShape::Any])),
57 "The closure to run.",
58 )
59 .switch("keep-empty", "Keep empty result cells.", Some('k'))
60 .switch(
61 "flatten",
62 "Combine outputs into a single stream instead of collecting them to separate values.",
63 Some('f'),
64 )
65 .allow_variants_without_examples(true)
66 .category(Category::Filters)
67 }
68
69 fn examples(&self) -> Vec<Example<'_>> {
70 vec![
71 Example {
72 example: "[1 2 3] | each {|e| 2 * $e }",
73 description: "Multiplies elements in the list.",
74 result: Some(Value::test_list(vec![
75 Value::test_int(2),
76 Value::test_int(4),
77 Value::test_int(6),
78 ])),
79 },
80 Example {
81 example: "{major:2, minor:1, patch:4} | values | each {|| into string }",
82 description: "Produce a list of values in the record, converted to string.",
83 result: Some(Value::test_list(vec![
84 Value::test_string("2"),
85 Value::test_string("1"),
86 Value::test_string("4"),
87 ])),
88 },
89 Example {
90 example: r#"[1 2 3 2] | each {|e| if $e == 2 { "two" } }"#,
91 description: "'null' items will be dropped from the result list. It has the same effect as 'filter_map' in other languages.",
92 result: Some(Value::test_list(vec![
93 Value::test_string("two"),
94 Value::test_string("two"),
95 ])),
96 },
97 Example {
98 example: r#"[1 2 3] | enumerate | each {|e| if $e.item == 2 { $"found 2 at ($e.index)!"} }"#,
99 description: "Iterate over each element, producing a list showing indexes of any 2s.",
100 result: Some(Value::test_list(vec![Value::test_string("found 2 at 1!")])),
101 },
102 Example {
103 example: r#"[1 2 3] | each --keep-empty {|e| if $e == 2 { "found 2!"} }"#,
104 description: "Iterate over each element, keeping null results.",
105 result: Some(Value::test_list(vec![
106 Value::nothing(Span::test_data()),
107 Value::test_string("found 2!"),
108 Value::nothing(Span::test_data()),
109 ])),
110 },
111 Example {
112 example: r#"$env.name? | each { $"hello ($in)" } | default "bye""#,
113 description: "Update value if not null, otherwise do nothing.",
114 result: None,
115 },
116 Example {
117 description: "Scan through multiple files without pause.",
118 example: "\
119 ls *.txt \
120 | each --flatten {|f| open $f.name | lines } \
121 | find -i 'note: ' \
122 | str join \"\\n\"\
123 ",
124 result: None,
125 },
126 ]
127 }
128
129 fn run(
130 &self,
131 engine_state: &EngineState,
132 stack: &mut Stack,
133 call: &Call,
134 mut input: PipelineData,
135 ) -> Result<PipelineData, ShellError> {
136 let head = call.head;
137 let closure: Closure = call.req(engine_state, stack, 0)?;
138 let keep_empty = call.has_flag(engine_state, stack, "keep-empty")?;
139 let flatten = call.has_flag(engine_state, stack, "flatten")?;
140
141 let result = match input {
142 PipelineData::Empty | PipelineData::Value(Value::Nothing { .. }, ..) => {
143 return Ok(input);
144 }
145 PipelineData::Value(Value::Range { .. }, ..)
146 | PipelineData::Value(Value::List { .. }, ..)
147 | PipelineData::ListStream(..) => {
148 let metadata = input.take_metadata();
149 let mut closure = ClosureEval::new(engine_state, stack, closure);
150
151 let out = if flatten {
152 input
153 .into_iter()
154 .flat_map(move |value| {
155 closure.run_with_value(value).unwrap_or_else(|error| {
156 Value::error(error, head).into_pipeline_data()
157 })
158 })
159 .into_pipeline_data(head, engine_state.signals().clone())
160 } else {
161 input
162 .into_iter()
163 .map(move |value| {
164 each_map(value, &mut closure, head)
165 .unwrap_or_else(|error| Value::error(error, head))
166 })
167 .into_pipeline_data(head, engine_state.signals().clone())
168 };
169 Ok(out.set_metadata(metadata))
170 }
171 #[expect(deprecated)]
173 PipelineData::Value(Value::Custom { ref val, .. }, ..) if val.is_iterable() => {
174 let metadata = input.take_metadata();
175 let mut closure = ClosureEval::new(engine_state, stack, closure);
176
177 let out = if flatten {
178 input
179 .into_iter()
180 .flat_map(move |value| {
181 closure.run_with_value(value).unwrap_or_else(|error| {
182 Value::error(error, head).into_pipeline_data()
183 })
184 })
185 .into_pipeline_data(head, engine_state.signals().clone())
186 } else {
187 input
188 .into_iter()
189 .map(move |value| {
190 each_map(value, &mut closure, head)
191 .unwrap_or_else(|error| Value::error(error, head))
192 })
193 .into_pipeline_data(head, engine_state.signals().clone())
194 };
195 Ok(out.set_metadata(metadata))
196 }
197 PipelineData::ByteStream(stream, metadata) => {
198 let Some(chunks) = stream.chunks() else {
199 return Ok(PipelineData::empty());
200 };
201
202 let mut closure = ClosureEval::new(engine_state, stack, closure);
203 let out = if flatten {
204 chunks
205 .flat_map(move |result| {
206 result
207 .and_then(|value| closure.run_with_value(value))
208 .unwrap_or_else(|error| {
209 Value::error(error, head).into_pipeline_data()
210 })
211 })
212 .into_pipeline_data(head, engine_state.signals().clone())
213 } else {
214 chunks
215 .map(move |result| {
216 result
217 .and_then(|value| each_map(value, &mut closure, head))
218 .unwrap_or_else(|error| Value::error(error, head))
219 })
220 .into_pipeline_data(head, engine_state.signals().clone())
221 };
222 Ok(out.set_metadata(metadata))
223 }
224 PipelineData::Value(value, metadata) => {
227 ClosureEvalOnce::new(engine_state, stack, closure)
228 .run_with_value_with_metadata(value, metadata)
229 }
230 };
231
232 if keep_empty {
233 result
234 } else {
235 result.and_then(|x| x.filter(|v| !v.is_nothing(), engine_state.signals()))
236 }
237 }
238}
239
240#[inline]
241fn each_map(value: Value, closure: &mut ClosureEval, head: Span) -> Result<Value, ShellError> {
242 let span = value.span();
243 let is_error = value.is_error();
244 closure
245 .run_with_value(value)
246 .and_then(|pipeline_data| pipeline_data.into_value(head))
247 .map_err(|error| chain_error_with_input(error, is_error, span))
248}
249
250#[cfg(test)]
251mod test {
252 use super::*;
253
254 #[test]
255 fn test_examples() -> nu_test_support::Result {
256 nu_test_support::test().examples(Each)
257 }
258}