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
188
189
190
191
192
193
194
195
196
197
/// Fill a collection with a specified value, returning a new vector with all elements set to the initial value.
///
/// This function takes a slice of items and an initial value, then returns a new `Vec<T>` containing the initial value
/// repeated for the length of the input collection. The original collection remains unmodified.
///
/// **Time Complexity:**
/// O(n), where n is the number of elements in the collection.
///
/// # Arguments
///
/// * `collection` - A slice of items indicating the size of the resulting vector.
/// * `initial` - The value to fill the new vector with.
///
/// # Type Parameters
///
/// * `T` - The type of elements in the collection. Must implement the `Clone` trait to allow duplication.
///
/// # Returns
///
/// * `Vec<T>` - A new vector containing the initial value repeated for the length of the input collection.
///
/// # Examples
///
/// ```rust
/// use lowdash::fill;
///
/// let numbers = vec![1, 2, 3, 4, 5];
/// let filled = fill(&numbers, 0);
/// assert_eq!(filled, vec![0, 0, 0, 0, 0]);
/// ```
///
/// ```rust
/// use lowdash::fill;
///
/// #[derive(Debug, PartialEq, Clone)]
/// 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 filled_people = fill(&people, Person { name: "Dave".to_string(), age: 40 });
/// assert_eq!(
/// filled_people,
/// vec![
/// Person { name: "Dave".to_string(), age: 40 },
/// Person { name: "Dave".to_string(), age: 40 },
/// Person { name: "Dave".to_string(), age: 40 },
/// ]
/// );
/// ```
pub fn fill<T>(collection: &[T], initial: T) -> Vec<T>
where
T: Clone,
{
let mut result = Vec::with_capacity(collection.len());
for _ in collection {
result.push(initial.clone());
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
#[test]
fn test_fill_integers() {
let numbers = vec![1, 2, 3, 4, 5];
let filled = fill(&numbers, 0);
assert_eq!(filled, vec![0, 0, 0, 0, 0]);
}
#[test]
fn test_fill_strings() {
let strings = vec!["apple", "banana", "cherry"];
let filled = fill(&strings, "fruit");
assert_eq!(
filled,
vec![
"fruit".to_string(),
"fruit".to_string(),
"fruit".to_string()
]
);
}
#[test]
fn test_fill_with_structs() {
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 filled_people = fill(
&people,
Person {
name: "Dave".to_string(),
age: 40,
},
);
assert_eq!(
filled_people,
vec![
Person {
name: "Dave".to_string(),
age: 40,
},
Person {
name: "Dave".to_string(),
age: 40,
},
Person {
name: "Dave".to_string(),
age: 40,
},
]
);
}
#[test]
fn test_fill_empty_collection() {
let empty: Vec<i32> = vec![];
let filled = fill(&empty, 100);
assert_eq!(filled, Vec::<i32>::new());
}
#[test]
fn test_fill_single_element() {
let single = vec![42];
let filled = fill(&single, 99);
assert_eq!(filled, vec![99]);
}
#[test]
fn test_fill_preserves_length() {
let original = vec![true, false, true, false];
let filled = fill(&original, true);
assert_eq!(filled.len(), original.len());
assert_eq!(filled, vec![true, true, true, true]);
}
#[test]
fn test_fill_with_duplicates() {
let numbers = vec![1, 1, 2, 2, 3, 3];
let filled = fill(&numbers, 0);
assert_eq!(filled, vec![0, 0, 0, 0, 0, 0]);
}
#[test]
fn test_fill_with_optionals() {
let collection = vec![Some(1), None, Some(2), None];
let filled = fill(&collection, None);
assert_eq!(filled, vec![None, None, None, None]);
}
#[test]
fn test_fill_with_floats() {
let float_collection = vec![1.1, 2.2, 3.3];
let filled = fill(&float_collection, 0.0);
assert_eq!(filled, vec![0.0, 0.0, 0.0]);
}
#[test]
fn test_fill_with_nan_floats() {
let float_collection = vec![std::f64::NAN, 2.2, std::f64::INFINITY];
let filled = fill(&float_collection, std::f64::NAN);
assert_eq!(filled.len(), float_collection.len());
for &value in &filled {
assert!(value.is_nan());
}
}
}