@@ -106,15 +106,21 @@ impl DestructuringVisitor<'_> {
106106 }
107107 }
108108
109- /// Folds a list of boolean expressions into a left-associated chain of
110- /// `&&` operations (e.g. `[a, b, c]` → `(a && b) && c`).
111- /// Panics if the list is empty. Inserts boolean types for all generated nodes.
112- pub fn fold_with_and ( & mut self , pieces : Vec < Expression > ) -> Expression {
109+ /// Folds an iterator of expressions into a left-associated chain using `op`.
110+ ///
111+ /// Given expressions `[e1, e2, e3]`, this produces `((e1 op e2) op e3)`.
112+ /// Each intermediate node is assigned a fresh ID and recorded as `Boolean`
113+ /// in the type table.
114+ ///
115+ /// Panics if the iterator is empty.
116+ pub fn fold_with_op < I > ( & mut self , op : BinaryOperation , pieces : I ) -> Expression
117+ where
118+ I : Iterator < Item = Expression > ,
119+ {
113120 pieces
114- . into_iter ( )
115121 . reduce ( |left, right| {
116122 let expr: Expression = BinaryExpression {
117- op : BinaryOperation :: And ,
123+ op,
118124 left,
119125 right,
120126 span : Default :: default ( ) ,
@@ -125,29 +131,7 @@ impl DestructuringVisitor<'_> {
125131 self . state . type_table . insert ( expr. id ( ) , Type :: Boolean ) ;
126132 expr
127133 } )
128- . expect ( "fold_with_and called with empty vec" )
129- }
130-
131- /// Folds a list of boolean expressions into a left-associated chain of
132- /// `||` operations (e.g. `[a, b, c]` → `(a || b) || c`).
133- /// Panics if the list is empty. Inserts boolean types for all generated nodes.
134- pub fn fold_with_or ( & mut self , pieces : Vec < Expression > ) -> Expression {
135- pieces
136- . into_iter ( )
137- . reduce ( |left, right| {
138- let expr: Expression = BinaryExpression {
139- op : BinaryOperation :: Or ,
140- left,
141- right,
142- span : Default :: default ( ) ,
143- id : self . state . node_builder . next_id ( ) ,
144- }
145- . into ( ) ;
146-
147- self . state . type_table . insert ( expr. id ( ) , Type :: Boolean ) ;
148- expr
149- } )
150- . expect ( "fold_with_or called with empty vec" )
134+ . expect ( "fold_with_op called with empty iterator" )
151135 }
152136
153137 // Given the `expression` of tuple type, make a definition assigning variable to its members.
0 commit comments