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
/// Removes elements from the end of a collection as long as a predicate returns `true`,
/// and returns the remaining elements. As soon as the predicate returns `false`, the function stops dropping elements.
///
/// **Time Complexity:** O(n), where n is the number of elements until the predicate returns `false`.
///
/// # Arguments
///
/// * `collection` - A slice of items from which elements will be dropped.
/// * `predicate` - A function that takes an item and returns `true` or `false`.
///
/// # Type Parameters
///
/// * `T` - The type of elements in the collection. Must implement `Clone`.
/// * `F` - The type of the predicate function. Must implement `Fn(&T) -> bool`.
///
/// # Returns
///
/// * `Vec<T>` - A vector containing the elements after dropping from the end while the predicate holds true.
///
/// # Examples
///
/// ```rust
/// use lowdash::drop_right_while;
///
/// let numbers = vec![1, 2, 3, 4, 5];
/// let result = drop_right_while(&numbers, |&x| x > 3);
/// assert_eq!(result, vec![1, 2, 3]);
/// ```
///
/// ```rust
/// use lowdash::drop_right_while;
///
/// let letters = vec!['a', 'b', 'c', 'd', 'e'];
/// let result = drop_right_while(&letters, |&c| c != 'c');
/// assert_eq!(result, vec!['a', 'b', 'c']);
/// ```
pub fn drop_right_while<T, F>(collection: &[T], predicate: F) -> Vec<T>
where
T: Clone,
F: Fn(&T) -> bool,
{
let mut i = collection.len();
while i > 0 && predicate(&collection[i - 1]) {
i -= 1;
}
collection[..i].to_vec()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_drop_right_while_basic() {
let numbers = vec![1, 2, 3, 4, 5];
let result = drop_right_while(&numbers, |&x| x > 3);
assert_eq!(result, vec![1, 2, 3]);
}
#[test]
fn test_drop_right_while_all_true() {
let numbers = vec![5, 4, 3, 2, 1];
let result = drop_right_while(&numbers, |_| true);
assert_eq!(result, Vec::<i32>::new());
}
#[test]
fn test_drop_right_while_all_false() {
let numbers = vec![1, 2, 3, 4, 5];
let result = drop_right_while(&numbers, |_| false);
assert_eq!(result, vec![1, 2, 3, 4, 5]);
}
#[test]
fn test_drop_right_while_empty_collection() {
let empty: Vec<i32> = vec![];
let result = drop_right_while(&empty, |_| true);
assert_eq!(result, Vec::<i32>::new());
}
#[test]
fn test_drop_right_while_with_structs() {
#[derive(Clone, Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person {
name: "Alice".to_string(),
age: 20,
},
Person {
name: "Bob".to_string(),
age: 25,
},
Person {
name: "Charlie".to_string(),
age: 30,
},
];
let result = drop_right_while(&people, |person| person.age > 20);
let expected = vec![Person {
name: "Alice".to_string(),
age: 20,
}];
assert_eq!(result, expected);
}
#[test]
fn test_drop_right_while_no_elements_dropped() {
let numbers = vec![1, 2, 3];
let result = drop_right_while(&numbers, |&x| x > 5);
assert_eq!(result, vec![1, 2, 3]);
}
#[test]
fn test_drop_right_while_all_elements_dropped() {
let numbers = vec![1, 2, 3];
let result = drop_right_while(&numbers, |&x| x > 0);
assert_eq!(result, Vec::<i32>::new());
}
#[test]
fn test_drop_right_while_with_chars() {
let chars = vec!['a', 'b', 'c', 'd', 'e'];
let result = drop_right_while(&chars, |&c| c != 'c');
assert_eq!(result, vec!['a', 'b', 'c']);
}
}