sum_by

Function sum_by 

Source
pub fn sum_by<T, R, F>(collection: &[T], iteratee: F) -> R
where F: Fn(&T) -> R, R: Add<Output = R> + Default + Copy,
Expand description

Calculates the sum of values obtained by applying a function to each element in a collection.

§Arguments

  • collection - A slice of items to process.
  • iteratee - A function that maps each item to a numeric value.

§Returns

  • R - The sum of all values produced by the iteratee function.

§Examples

use lowdash::sum_by;

let numbers = vec![1, 2, 3, 4];
let result = sum_by(&numbers, |x| x * 2);
assert_eq!(result, 20); // (1*2 + 2*2 + 3*2 + 4*2)