1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/// Execute a function on each item in a collection.
///
/// This function iterates over a collection, applying the provided `iteratee` function
/// to each item along with its index.
///
/// # Arguments
/// * `collection` - A slice of items.
/// * `iteratee` - A function that takes a reference to an item and its index.
///
/// # Examples
/// ```rust
/// use lowdash::foreach;
/// let numbers = vec![1, 2, 3, 4, 5];
/// let mut sum = 0;
/// foreach(&numbers, |x, _| sum += x);
/// assert_eq!(sum, 15);
/// ```
///
/// ```rust
/// use lowdash::foreach;
///
/// #[derive(Debug, PartialEq)]
/// struct Person {
/// name: String,
/// age: u32,
/// }
///
/// let people = vec![
/// Person { name: "Alice".to_string(), age: 25 },
/// Person { name: "Bob".to_string(), age: 30 },
/// Person { name: "Carol".to_string(), age: 35 },
/// ];
///
/// let mut names = Vec::new();
/// foreach(&people, |p, _| names.push(p.name.clone()));
/// assert_eq!(names, vec!["Alice", "Bob", "Carol"]);
/// ```
pub fn foreach<T, F>(collection: &[T], mut iteratee: F)
where
F: FnMut(&T, usize),
{
for (index, item) in collection.iter().enumerate() {
iteratee(item, index);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_foreach_sum() {
let numbers = vec![1, 2, 3, 4, 5];
let mut sum = 0;
foreach(&numbers, |x, _| sum += x);
assert_eq!(sum, 15);
}
#[test]
fn test_foreach_with_index() {
let numbers = vec![10, 20, 30];
let mut result = Vec::new();
foreach(&numbers, |x, index| result.push(*x + index as i32));
assert_eq!(result, vec![10, 21, 32]);
}
#[test]
fn test_foreach_struct() {
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person {
name: "Alice".to_string(),
age: 25,
},
Person {
name: "Bob".to_string(),
age: 30,
},
Person {
name: "Carol".to_string(),
age: 35,
},
];
let mut names = Vec::new();
foreach(&people, |p, _| names.push(p.name.clone()));
assert_eq!(names, vec!["Alice", "Bob", "Carol"]);
}
#[test]
fn test_foreach_empty_collection() {
let collection: Vec<i32> = vec![];
let mut called = false;
foreach(&collection, |_, _| called = true);
assert!(!called);
}
#[test]
fn test_foreach_multiple_types() {
let chars = vec!['a', 'b', 'c'];
let mut collected = String::new();
foreach(&chars, |c, _| collected.push(*c));
assert_eq!(collected, "abc");
}
#[test]
fn test_foreach_with_floats() {
let float_collection = vec![1.1, 2.2, 3.3];
let mut product: f64 = 1.0;
foreach(&float_collection, |x, _| product *= x);
assert!((product - 7.986).abs() < 1e-10); // 1.1 * 2.2 * 3.3 = 7.986
}
#[test]
fn test_foreach_with_optionals() {
let numbers = vec![Some(1), None, Some(3), Some(4)];
let mut sum = 0;
foreach(&numbers, |x, _| {
if let Some(n) = x {
sum += n;
}
});
assert_eq!(sum, 8);
}
#[test]
fn test_foreach_with_index_and_condition() {
let numbers = vec![1, 2, 3, 4, 5];
let mut sum = 0;
foreach(&numbers, |x, index| {
if *x % 2 != 0 {
sum += *x * (index as i32 + 1);
}
});
// Calculation:
// index 0: 1 * (0 + 1) = 1
// index 2: 3 * (2 + 1) = 9
// index 4: 5 * (4 + 1) = 25
// Total sum = 1 + 9 + 25 = 35
assert_eq!(sum, 35);
}
#[test]
fn test_foreach_with_strings() {
let strings = vec!["Hello", " ", "World", "!"];
let mut concatenated = String::new();
foreach(&strings, |s, _| concatenated.push_str(s));
assert_eq!(concatenated, "Hello World!");
}
#[test]
fn test_foreach_with_structs_complex_logic() {
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person {
name: "Alice".to_string(),
age: 25,
},
Person {
name: "Bob".to_string(),
age: 30,
},
Person {
name: "Carol".to_string(),
age: 35,
},
];
let mut total_age = 0;
foreach(&people, |person, _| {
if person.age > 20 {
total_age += person.age;
}
});
assert_eq!(total_age, 90);
}
}