unless_keypath

Function unless_keypath 

Source
pub fn unless_keypath<T, V, F, G, R>(
    collection: Vec<T>,
    keypath: KeyPaths<T, V>,
    condition: F,
    operation: G,
) -> KeyPathResult<Vec<R>>
where F: Fn(&V) -> bool, G: FnOnce(IntoIter<T>) -> IntoIter<R>,
Expand description

Inverse conditional operations

ยงExamples

use rust_prelude_plus::prelude::*;
use key_paths_derive::Keypath;
use std::rc::Rc;
 
#[derive(Keypath, Debug, Clone)]
struct Person {
    name: String,
    age: u32,
}
 
let people = vec![
    Rc::new(Person { name: "Alice".to_string(), age: 30 }),
    Rc::new(Person { name: "Bob".to_string(), age: 25 }),
];
 
// Apply operation only when condition is NOT met
let result: Vec<String> = people
    .iter()
    .filter_by_keypath(Person::age(), |&age| age < 30)
    .map_keypath(Person::name(), |name| name.to_uppercase())
    .collect();
 
assert_eq!(result, vec!["BOB"]);