@@ -73,6 +73,63 @@ impl PyItertoolsChain {
73
73
}
74
74
}
75
75
76
+ #[ pyclass( name = "compress" ) ]
77
+ #[ derive( Debug ) ]
78
+ struct PyItertoolsCompress {
79
+ data : PyObjectRef ,
80
+ selector : PyObjectRef ,
81
+ }
82
+
83
+ impl PyValue for PyItertoolsCompress {
84
+ fn class ( vm : & VirtualMachine ) -> PyClassRef {
85
+ vm. class ( "itertools" , "compress" )
86
+ }
87
+ }
88
+
89
+ #[ pyimpl]
90
+ impl PyItertoolsCompress {
91
+ #[ pymethod( name = "__new__" ) ]
92
+ #[ allow( clippy:: new_ret_no_self) ]
93
+ fn new (
94
+ _cls : PyClassRef ,
95
+ data : PyObjectRef ,
96
+ selector : PyObjectRef ,
97
+ vm : & VirtualMachine ,
98
+ ) -> PyResult {
99
+ let data_iter = get_iter ( vm, & data) ?;
100
+ let selector_iter = get_iter ( vm, & selector) ?;
101
+
102
+ Ok ( PyItertoolsCompress {
103
+ data : data_iter,
104
+ selector : selector_iter,
105
+ }
106
+ . into_ref ( vm)
107
+ . into_object ( ) )
108
+ }
109
+
110
+ #[ pymethod( name = "__next__" ) ]
111
+ fn next ( & self , vm : & VirtualMachine ) -> PyResult {
112
+ loop {
113
+ let selector_iter = get_iter ( vm, & self . selector ) ?;
114
+ let sel_obj = call_next ( vm, & selector_iter) ?;
115
+
116
+ let verdict = objbool:: boolval ( vm, sel_obj. clone ( ) ) ?;
117
+
118
+ let data_iter = get_iter ( vm, & self . data ) ?;
119
+ let data_obj = call_next ( vm, & data_iter) ?;
120
+
121
+ if verdict {
122
+ return Ok ( data_obj) ;
123
+ }
124
+ }
125
+ }
126
+
127
+ #[ pymethod( name = "__iter__" ) ]
128
+ fn iter ( zelf : PyRef < Self > , _vm : & VirtualMachine ) -> PyRef < Self > {
129
+ zelf
130
+ }
131
+ }
132
+
76
133
#[ pyclass]
77
134
#[ derive( Debug ) ]
78
135
struct PyItertoolsCount {
@@ -577,8 +634,8 @@ impl PyItertoolsAccumulate {
577
634
let obj = call_next ( vm, iterable) ?;
578
635
579
636
let next_acc_value = match & * self . acc_value . borrow ( ) {
580
- Option :: None => obj. clone ( ) ,
581
- Option :: Some ( value) => {
637
+ None => obj. clone ( ) ,
638
+ Some ( value) => {
582
639
if self . binop . is ( & vm. get_none ( ) ) {
583
640
vm. _add ( value. clone ( ) , obj. clone ( ) ) ?
584
641
} else {
@@ -602,6 +659,8 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
602
659
603
660
let chain = PyItertoolsChain :: make_class ( ctx) ;
604
661
662
+ let compress = PyItertoolsCompress :: make_class ( ctx) ;
663
+
605
664
let count = ctx. new_class ( "count" , ctx. object ( ) ) ;
606
665
PyItertoolsCount :: extend_class ( ctx, & count) ;
607
666
@@ -626,6 +685,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
626
685
627
686
py_module ! ( vm, "itertools" , {
628
687
"chain" => chain,
688
+ "compress" => compress,
629
689
"count" => count,
630
690
"dropwhile" => dropwhile,
631
691
"repeat" => repeat,
0 commit comments