pub fn product_by<T, R>(collection: &[T], iteratee: impl Fn(&T) -> R) -> RExpand description
Calculate the product of values obtained by applying a function to each element in a collection.
If the collection is empty, returns 1 (multiplicative identity).
Works with any numeric type that implements std::ops::Mul and can be copied.
§Arguments
collection- A slice of items.iteratee- A function that takes an item from the collection and returns a number.
§Returns
T- The product of all numbers generated by the iteratee function.
§Examples
use lowdash::product_by;
let numbers = vec![1, 2, 3, 4];
let result = product_by(&numbers, |x| x * 2);
assert_eq!(result, 384); // (1*2) * (2*2) * (3*2) * (4*2)use lowdash::product_by;
#[derive(Debug)]
struct Rectangle {
width: f64,
height: f64,
}
let rectangles = vec![
Rectangle { width: 2.0, height: 3.0 },
Rectangle { width: 4.0, height: 5.0 },
];
let total_area = product_by(&rectangles, |r| r.width * r.height);
assert_eq!(total_area, 120.0); // (2*3) * (4*5)