Skip to content

Commit

Permalink
refactor: cleaned up expression-related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AurumTheEnd committed Sep 28, 2023
1 parent 87dc913 commit 8cab787
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/expressions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,23 @@ impl<T: Debug + Clone + Eq + Hash> Expression<T> {
}

pub fn n_ary_and(es: Vec<Expression<T>>) -> Expression<T> {
And(es.into_iter().map(|e| Box::new(e)).collect())
And(es.into_iter().map(Box::new).collect())
}

pub fn binary_or(e1: Expression<T>, e2: Expression<T>) -> Expression<T> {
Or(vec![Box::new(e1), Box::new(e2)])
}

pub fn n_ary_or(es: Vec<Expression<T>>) -> Expression<T> {
Or(es.into_iter().map(|e| Box::new(e)).collect())
Or(es.into_iter().map(Box::new).collect())
}

pub fn evaluate(&self, literal_values: &HashMap<T, bool>) -> bool {
match self {
Literal(ref t) => *literal_values.get(t).unwrap_or(&false),
Constant(ref value) => *value,
And(ref values) => values
.iter()
.fold(true, |acc, e| acc && e.evaluate(literal_values)),
Or(ref values) => values
.iter()
.fold(false, |acc, e| acc || e.evaluate(literal_values)),
And(ref values) => values.iter().all(|e| e.evaluate(literal_values)),
Or(ref values) => values.iter().any(|e| e.evaluate(literal_values)),
Not(ref x) => !x.evaluate(literal_values),
}
}
Expand Down

0 comments on commit 8cab787

Please sign in to comment.