daily pastebin goal
34%
SHARE
TWEET

Untitled

a guest May 17th, 2018 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #![feature(iterator_repeat_with)]
  2.  
  3. use std::iter;
  4.  
  5. fn rotating_window_sums<'a>(
  6.     values: &'a [i32],
  7.     size: usize,
  8. ) -> impl Iterator<Item = (i32, i32)> + 'a {
  9.     let mut numbers = values.iter().cycle();
  10.     let other_size = values.len() - size;
  11.  
  12.     iter::repeat_with(move || {
  13.         let sum1: i32 = numbers.by_ref().take(size).sum();
  14.         let sum2: i32 = numbers.by_ref().take(other_size).sum();
  15.  
  16.         // Skip one so the next iteration is offset by one
  17.         numbers.next();
  18.  
  19.         (sum1, sum2)
  20.     }).take(values.len())
  21. }
  22.  
  23. fn main() {
  24.     let d = [1, 2, 3, 4, 5, 6, 7, 8];
  25.  
  26.     let max = rotating_window_sums(&d, 3)
  27.         .map(|(sum1, sum2)| i32::abs(3 * sum1 + 5 * sum2))
  28.         .max();
  29.  
  30.     println!("{:?}", max);
  31. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top