Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- println!("{:?}", scanl(|x, y| x + y, 0, &[1, 2, 3, 4, 5]));
- }
- fn scanl<T, F>(op: F, initial: T, list: &[T]) -> Vec<T>
- where
- F: Fn(&T, &T) -> T,
- {
- let mut acc = Vec::with_capacity(list.len());
- acc.push(initial);
- list.iter().fold(acc, |mut acc, val| {
- acc.push(op(val, acc.last().unwrap()));
- acc
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement