1use std::cell::RefCell;
2
3pub struct Collect {
4 items: RefCell<Vec<i32>>,
5}
6
7impl Collect {
8 pub fn new(items: Vec<i32>)->Collect {
9 Collect { items: RefCell::new(items) }
10 }
11
12 pub fn all(&self) -> Vec<i32> {
13 let mut arr = Vec::new();
14
15 for el in self.items.borrow().iter() {
16 arr.push(el.clone());
17 }
18
19 return arr;
20 }
21
22 pub fn average(&self) -> i32 {
23 let items = self.items.borrow();
24 let mut total: i32 = 0;
25
26 for el in items.iter() {
27 total += el;
28 }
29
30 return total / items.len() as i32;
31 }
32
33 pub fn chunk(&self, chunk_size: usize) -> Vec<Vec<i32>> {
34 let items = self.items.borrow();
35 let mut chunks: Vec<Vec<i32>> = Vec::new();
36 chunks.push(Vec::new());
37
38 let mut prev_chunk: usize = 0;
39 for (index, el) in items.iter().enumerate() {
40 if ((index / chunk_size) as f32).floor() as usize != prev_chunk {
41 chunks.push(Vec::new());
42 prev_chunk += 1;
43 }
44
45 chunks[prev_chunk].push(el.clone());
46 }
47
48 return chunks;
49 }
50
51 pub fn push(&self, value: i32) {
52 self.items.borrow_mut().push(value);
53 }
54}