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
/// Calls a provided custom comparator with element of collection, returns a vector of unique element.
///
/// * comparator function signature: ```fn(item1: &T, item2: &T) -> bool```
///
/// # Arguments
///
/// * `collection` - The collection to inspect.
///
/// * `comparator` - The iteratee invoked per element.
///
/// # Returns
///
/// Returns a new duplicate free vector.
///
/// # Examples
///
/// ```
/// use rufl::collection;
///
/// assert_eq!(vec![1, 2, 3], collection::unique_by([1, 2, 2, 3, 3, 3], &|a: &i32, b: &i32| { a == b }));
///
/// assert_eq!(vec![1.0, 2.0, 3.0], collection::unique_by(vec![1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 3.2], &|a: &f64, b: &f64| { a.floor() == b.floor()}));
///
/// ```
///
pub fn unique_by<C: AsRef<[T]>, T: Clone + PartialEq>(
collection: C,
comparator: impl Fn(&T, &T) -> bool,
) -> Vec<T> {
let mut result = collection.as_ref().to_vec().clone();
for i in (0..result.len()).rev() {
for j in (i + 1..result.len()).rev() {
if comparator(&result[i], &result[j]) {
result.remove(j);
}
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_unique_by() {
assert_eq!(
vec![1, 2, 3],
unique_by([1, 2, 3], &|a: &i32, b: &i32| { a == b })
);
assert_eq!(
vec![1, 2, 3],
unique_by([1, 2, 2, 3, 3, 3], &|a: &i32, b: &i32| { a == b })
);
assert_eq!(
vec![1.0, 2.0, 3.0],
unique_by(
vec![1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 3.2],
&|a: &f64, b: &f64| { a.floor() == b.floor() }
)
);
}
}