Skip to content

itertools.compress #1400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/snippets/stdlib_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,11 @@ def assert_matches_seq(it, seq):
assert 0 == next(it)
with assert_raises(StopIteration):
next(it)

# itertools.compress
assert list(itertools.compress("ABCDEF", [1,0,1,0,1,1])) == list("ACEF")
assert list(itertools.compress("ABCDEF", [0,0,0,0,0,0])) == list("")
assert list(itertools.compress("ABCDEF", [1,1,1,1,1,1])) == list("ABCDEF")
assert list(itertools.compress("ABCDEF", [1,0,1])) == list("AC")
assert list(itertools.compress("ABC", [0,1,1,1,1,1])) == list("BC")
assert list(itertools.compress("ABCDEF", [True,False,"t","",1,9])) == list("ACEF")
60 changes: 58 additions & 2 deletions vm/src/stdlib/itertools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,59 @@ impl PyItertoolsChain {
}
}

#[pyclass(name = "compress")]
#[derive(Debug)]
struct PyItertoolsCompress {
data: PyObjectRef,
selector: PyObjectRef,
}

impl PyValue for PyItertoolsCompress {
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.class("itertools", "compress")
}
}

#[pyimpl]
impl PyItertoolsCompress {
#[pymethod(name = "__new__")]
#[allow(clippy::new_ret_no_self)]
fn new(
_cls: PyClassRef,
data: PyObjectRef,
selector: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult {
let data_iter = get_iter(vm, &data)?;
let selector_iter = get_iter(vm, &selector)?;

Ok(PyItertoolsCompress {
data: data_iter,
selector: selector_iter,
}
.into_ref(vm)
.into_object())
}

#[pymethod(name = "__next__")]
fn next(&self, vm: &VirtualMachine) -> PyResult {
loop {
let sel_obj = call_next(vm, &self.selector)?;
let verdict = objbool::boolval(vm, sel_obj.clone())?;
let data_obj = call_next(vm, &self.data)?;

if verdict {
return Ok(data_obj);
}
}
}

#[pymethod(name = "__iter__")]
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
zelf
}
}

#[pyclass]
#[derive(Debug)]
struct PyItertoolsCount {
Expand Down Expand Up @@ -577,8 +630,8 @@ impl PyItertoolsAccumulate {
let obj = call_next(vm, iterable)?;

let next_acc_value = match &*self.acc_value.borrow() {
Option::None => obj.clone(),
Option::Some(value) => {
None => obj.clone(),
Some(value) => {
if self.binop.is(&vm.get_none()) {
vm._add(value.clone(), obj.clone())?
} else {
Expand All @@ -602,6 +655,8 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {

let chain = PyItertoolsChain::make_class(ctx);

let compress = PyItertoolsCompress::make_class(ctx);

let count = ctx.new_class("count", ctx.object());
PyItertoolsCount::extend_class(ctx, &count);

Expand All @@ -626,6 +681,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {

py_module!(vm, "itertools", {
"chain" => chain,
"compress" => compress,
"count" => count,
"dropwhile" => dropwhile,
"repeat" => repeat,
Expand Down