Skip to content

Commit bcf3b6c

Browse files
committed
Refactor map IterNext to match zip style
1 parent e1a7a29 commit bcf3b6c

File tree

1 file changed

+22
-36
lines changed

1 file changed

+22
-36
lines changed

crates/vm/src/builtins/map.rs

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -86,51 +86,37 @@ impl SelfIter for PyMap {}
8686

8787
impl IterNext for PyMap {
8888
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
89-
let strict = zelf.strict.load(atomic::Ordering::Acquire);
9089
let mut next_objs = Vec::new();
91-
let mut stopped_at: Option<usize> = None;
92-
9390
for (idx, iterator) in zelf.iterators.iter().enumerate() {
94-
match iterator.next(vm)? {
95-
PyIterReturn::Return(obj) => {
96-
if let Some(stopped_idx) = stopped_at {
97-
if strict {
98-
let plural = if stopped_idx == 0 { " " } else { "s 1-" };
91+
let item = match iterator.next(vm)? {
92+
PyIterReturn::Return(obj) => obj,
93+
PyIterReturn::StopIteration(v) => {
94+
if zelf.strict.load(atomic::Ordering::Acquire) {
95+
if idx > 0 {
96+
let plural = if idx == 1 { " " } else { "s 1-" };
9997
return Err(vm.new_value_error(format!(
100-
"map() argument {} is longer than argument{}{}",
98+
"map() argument {} is shorter than argument{}{}",
10199
idx + 1,
102100
plural,
103-
stopped_idx + 1,
101+
idx,
104102
)));
105103
}
106-
return Ok(PyIterReturn::StopIteration(None));
107-
}
108-
next_objs.push(obj);
109-
}
110-
PyIterReturn::StopIteration(v) => {
111-
if stopped_at.is_some() {
112-
continue;
113-
}
114-
if strict && idx > 0 {
115-
let plural = if idx == 1 { " " } else { "s 1-" };
116-
return Err(vm.new_value_error(format!(
117-
"map() argument {} is shorter than argument{}{}",
118-
idx + 1,
119-
plural,
120-
idx,
121-
)));
122-
}
123-
if strict {
124-
stopped_at = Some(idx);
125-
} else {
126-
return Ok(PyIterReturn::StopIteration(v));
104+
for (idx, iterator) in zelf.iterators[1..].iter().enumerate() {
105+
if let PyIterReturn::Return(_) = iterator.next(vm)? {
106+
let plural = if idx == 0 { " " } else { "s 1-" };
107+
return Err(vm.new_value_error(format!(
108+
"map() argument {} is longer than argument{}{}",
109+
idx + 2,
110+
plural,
111+
idx + 1,
112+
)));
113+
}
114+
}
127115
}
116+
return Ok(PyIterReturn::StopIteration(v));
128117
}
129-
}
130-
}
131-
132-
if stopped_at.is_some() {
133-
return Ok(PyIterReturn::StopIteration(None));
118+
};
119+
next_objs.push(item);
134120
}
135121

136122
// the mapper itself can raise StopIteration which does stop the map iteration

0 commit comments

Comments
 (0)