Skip to content

Commit 2c061c0

Browse files
committed
Implement reduce for islice
1 parent 8cb2b82 commit 2c061c0

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

vm/src/stdlib/itertools.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,35 @@ mod decl {
924924
.into_ref_with_type(vm, cls)
925925
.map(Into::into)
926926
}
927+
928+
#[pymethod(magic)]
929+
fn reduce(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyTupleRef> {
930+
let cls = zelf.class().to_owned();
931+
let itr = zelf.iterable.clone();
932+
let cur = zelf.cur.take();
933+
let next = zelf.next.take();
934+
let step = zelf.step;
935+
match zelf.stop {
936+
Some(stop) => Ok(vm.new_tuple((cls, (itr, next, stop, step), (cur,)))),
937+
_ => Ok(vm.new_tuple((cls, (itr, next, vm.new_pyobj(()), step), (cur,)))),
938+
}
939+
}
940+
941+
#[pymethod(magic)]
942+
fn setstate(zelf: PyRef<Self>, state: PyTupleRef, vm: &VirtualMachine) -> PyResult<()> {
943+
let args = state.as_slice();
944+
if args.len() != 1 {
945+
let msg = format!("function takes exactly 1 argument ({} given)", args.len());
946+
return Err(vm.new_type_error(msg));
947+
}
948+
let cur = &args[0];
949+
if let Ok(cur) = usize::try_from_object(vm, cur.clone()) {
950+
zelf.cur.store(cur);
951+
} else {
952+
return Err(vm.new_type_error(String::from("Argument must be usize.")));
953+
}
954+
Ok(())
955+
}
927956
}
928957

929958
impl IterNextIterable for PyItertoolsIslice {}

0 commit comments

Comments
 (0)