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
/// Find the last item in a collection that satisfies a predicate and return its index.
/// If no item satisfies the predicate, return None.
///
/// # Arguments
/// * `collection` - A collection of items.
/// * `predicate` - A function that takes an item from the collection and returns a boolean.
///
/// # Returns
/// * `Option<(&T, usize)>` - A tuple containing the last item in the collection that satisfies the predicate and its index, or None if no item satisfies the predicate.
///
/// # Examples
/// ```rust
/// use lowdash::find_last_index_of;
/// let numbers = vec![1, 2, 3, 4, 5, 3];
/// let predicate = |x: &i32| *x == 3;
/// let result = find_last_index_of(&numbers, predicate);
/// assert_eq!(result, Some((&3, 5)));
/// ```
///
/// ```rust
/// use lowdash::find_last_index_of;
/// let numbers = vec![10, 20, 30, 40, 30];
/// let result = find_last_index_of(&numbers, |x| *x > 25);
/// assert_eq!(result, Some((&30, 4)));
/// ```
///
/// ```rust
/// use lowdash::find_last_index_of;
///
/// #[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 },
/// Person { name: "Dave".to_string(), age: 35 },
/// ];
///
/// let result = find_last_index_of(&people, |p| p.age > 30);
/// assert_eq!(result, Some((&Person { name: "Dave".to_string(), age: 35 }, 3)));
/// ```
pub fn find_last_index_of<T, F>(collection: &[T], predicate: F) -> Option<(&T, usize)>
where
F: Fn(&T) -> bool,
{
for (i, item) in collection.iter().enumerate().rev() {
if predicate(item) {
return Some((item, i));
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_find_last_index_of() {
let collection = vec![1, 2, 3, 4, 5, 3];
let predicate = |x: &i32| *x == 3;
let result = find_last_index_of(&collection, predicate);
assert_eq!(result, Some((&3, 5)));
}
#[test]
fn test_find_last_index_of_with_inline_predicate() {
let numbers = vec![10, 20, 30, 40, 30];
let result = find_last_index_of(&numbers, |x| *x > 25);
assert_eq!(result, Some((&30, 4)));
}
#[test]
fn test_find_last_index_of_not_found() {
let collection = vec![1, 2, 3, 4, 5];
let predicate = |x: &i32| *x == 6;
let result = find_last_index_of(&collection, predicate);
assert_eq!(result, None);
}
#[test]
fn test_find_last_index_of_not_found_with_inline_predicate() {
let numbers = vec![10, 20, 30, 40];
let result = find_last_index_of(&numbers, |x| *x > 50);
assert_eq!(result, None);
}
#[test]
fn test_find_last_index_of_empty_collection() {
let collection: Vec<i32> = vec![];
let predicate = |x: &i32| *x == 3;
let result = find_last_index_of(&collection, predicate);
assert_eq!(result, None);
}
#[test]
fn test_find_last_index_of_with_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,
},
Person {
name: "Dave".to_string(),
age: 35,
},
];
let result = find_last_index_of(&people, |p| p.age > 30);
assert_eq!(
result,
Some((
&Person {
name: "Dave".to_string(),
age: 35
},
3
))
);
let name_result = find_last_index_of(&people, |p| p.name.starts_with("B"));
assert_eq!(
name_result,
Some((
&Person {
name: "Bob".to_string(),
age: 30
},
1
))
);
let not_found = find_last_index_of(&people, |p| p.age > 40);
assert_eq!(not_found, None);
}
#[test]
fn test_find_last_index_of_floats() {
let floats = vec![1.1, 2.2, 3.3, 4.4, 5.5, 4.4];
let predicate = |x: &f64| *x > 3.0;
let result = find_last_index_of(&floats, predicate);
assert_eq!(result, Some((&4.4, 5)));
let not_found = find_last_index_of(&floats, |x| *x > 6.0);
assert_eq!(not_found, None);
}
}