Skip to content
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "Declarative quantifiers for filtering, validation, and testing in
keywords = ["logic", "validation", "math", "quantifiers"]
categories = ["algorithms", "data-structures", "mathematics", "development-tools::testing"]
repository = "https://github.com/nervousnullptr/quantor"
version = "0.9.0"
version = "0.9.1"
edition = "2021"
readme = "readme.md"
license = "MIT OR Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ This is readable, declarative, and robust – and every check returns a Result w
## Installation
Add `quantor` to your `Cargo.toml`:
```
quantor = "0.1"
quantor = "0.9"
```
Optional features:
* `method-api` — Enables `.forall()`, `.exists()`, `.select_where()`, etc. on slices and iterators.
Expand Down
13 changes: 9 additions & 4 deletions src/quantifiers/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::QuantorError;
/// Checks whether for every element in `a`, there exists at least one element in `b` for which the predicate holds.
///
/// Equivalent to **_∀x ∈ a ∃y ∈ b: pred(x, y)_**.
///
/// **Note**: _If `b` is empty and `a` is not, this will always return an error._
/// ## Arguments
/// - `a` - The source collection (outer quantifier).
/// - `b` - The comparison collection.
Expand Down Expand Up @@ -68,7 +70,7 @@ pub fn forallexists<'a, A: 'a, B: 'a>(
/// - `pred` - The binary predicate to check against.
/// ## Returns
/// - `Ok(())` if there exists at least one element in the left-hand collection (`a`) such that the condition holds against all elements in the right-hand collection (`b`).
/// - `Err(QuantorError::ExistsForAllFailed { outer_index })` if no such `a` is found.
/// - `Err(QuantorError::ExistsForAllFailed { outer_index })` if no such element exists, where `outer_index` is the index of the first failing `a` element.
/// ## Example
/// ```
/// use quantor::{quantifiers::existsforall, error::QuantorResultExt};
Expand All @@ -93,10 +95,9 @@ pub fn existsforall<'a, A: 'a, B: 'a>(
pred: impl Fn(&A, &B) -> bool,
) -> Result<(), QuantorError> {
let b_vec: Vec<&'a B> = b.into_iter().collect();
let mut last_index = 0;
let mut first_index = None;

for (index, item_a) in a.into_iter().enumerate() {
last_index = index;
let mut all_match = true;

for item_b in &b_vec {
Expand All @@ -109,7 +110,11 @@ pub fn existsforall<'a, A: 'a, B: 'a>(
if all_match {
return Ok(());
}

if first_index.is_none() {
first_index = Some(index)
}
}

Err(QuantorError::ExistsForAllFailed { outer_index: last_index })
Err(QuantorError::ExistsForAllFailed { outer_index: first_index.unwrap_or(0) })
}
7 changes: 5 additions & 2 deletions src/quantifiers/structured.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ where
None => return Ok(())
};

for (i, curr) in iter.enumerate() {
let mut index = 0;

for curr in iter {
if !pred(prev, curr) {
// Index `i` here refers to the second item in the failing pair.
return Err(QuantorError::PairwiseFailed { index: i });
return Err(QuantorError::PairwiseFailed { index });
}
prev = curr;
index += 1;
}

Ok(())
Expand Down