reject_map

Function reject_map 

Source
pub fn reject_map<T, R, F>(collection: &[T], callback: F) -> Vec<R>
where F: FnMut(&T, usize) -> (R, bool),
Expand description

Applies a callback function to each item in a collection along with its index and collects the results where the callback returns false.

This function iterates over a slice of items, applies the provided callback to each item and its index, and collects the results (R) for which the callback returns false.

Time Complexity: O(n), where n is the number of elements in the collection.

§Arguments

  • collection - A slice of items to iterate over.
  • callback - A mutable function that takes a reference to an item and its index, returning a tuple (R, bool). If the second element of the tuple is false, the first element (R) is collected.

§Type Parameters

  • T - The type of elements in the input collection.
  • R - The type of elements in the resulting collection.
  • F - The type of the callback function.

§Returns

  • Vec<R> - A vector containing the results from the callback where the predicate is false.

§Examples

use lowdash::reject_map;

let numbers = vec![1, 2, 3, 4, 5];
// Collect squares of odd numbers
let result = reject_map(&numbers, |&x, _| (x * x, x % 2 == 0));
assert_eq!(result, vec![1, 9, 25]);
use lowdash::reject_map;

#[derive(Debug, PartialEq)]
struct Person {
    name: String,
    age: u32,
}

let people = vec![
    Person { name: "Alice".to_string(), age: 25 },
    Person { name: "Bob".to_string(), age: 30 },
    Person { name: "Carol".to_string(), age: 35 },
];

// Collect names of people who are not above 30
let result = reject_map(&people, |person, _| (person.name.clone(), person.age > 30));
assert_eq!(result, vec!["Alice".to_string(), "Bob".to_string()]);