-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevery_value.rs
43 lines (39 loc) · 1.1 KB
/
every_value.rs
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
//! Checks if `predicate` returns truthy for **all** properties of `HashMap`.
//! Iteration is stopped once `predicate` returns falsy. The predicate is
//!
//! **Note:** This method returns `true` for
//! [empty HashMaps](https://en.wikipedia.org/wiki/Empty_set) because
//! [everything is true](https://en.wikipedia.org/wiki/Vacuous_truth) of
//! elements of empty HashMap.
//!
//! Example
//! ```
//! use lodash_rust::every_value;
//! use std::collections::HashMap;
//!
//! let mut map: HashMap<char, u64> = HashMap::new();
//! map.insert('a', 1);
//! map.insert('b', 2);
//! map.insert('c', 3);
//!
//! let res = every_value::new(&map, |&x| x % 2 == 1);
//! println!("{res}") // false
//! ```
use std::collections::HashMap;
pub fn new<K, V, F: Fn(&V) -> bool>(h: &HashMap<K, V>, p: F) -> bool {
for v in h.values() {
if !p(v) {
return false;
}
}
true
}
#[test]
fn test_new() {
let mut map: HashMap<char, u64> = HashMap::new();
map.insert('a', 1);
map.insert('b', 2);
map.insert('c', 3);
assert!(!new(&map, |&x| x % 2 == 1));
assert!(new(&map, |&x| x >= 1));
}