diff --git a/Cargo.toml b/Cargo.toml index cb9bc34..d97a77f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/readme.md b/readme.md index 517a4f3..e5ce935 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/src/quantifiers/nested.rs b/src/quantifiers/nested.rs index 351a046..1513662 100644 --- a/src/quantifiers/nested.rs +++ b/src/quantifiers/nested.rs @@ -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. @@ -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}; @@ -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 { @@ -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) }) } \ No newline at end of file diff --git a/src/quantifiers/structured.rs b/src/quantifiers/structured.rs index dff8eb9..ace6d4f 100644 --- a/src/quantifiers/structured.rs +++ b/src/quantifiers/structured.rs @@ -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(())