Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.34 KB | None | 0 0
  1. fn main() {
  2. println!("{:?}", scanl(|x, y| x + y, 0, &[1, 2, 3, 4, 5]));
  3. }
  4.  
  5. fn scanl<T, F>(op: F, initial: T, list: &[T]) -> Vec<T>
  6. where
  7. F: Fn(&T, &T) -> T,
  8. {
  9. let mut acc = Vec::with_capacity(list.len());
  10. acc.push(initial);
  11.  
  12. list.iter().fold(acc, |mut acc, val| {
  13. acc.push(op(val, acc.last().unwrap()));
  14. acc
  15. })
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement