-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomprehension.rs
68 lines (63 loc) · 2.23 KB
/
comprehension.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
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
/**
* COMPREHENSION (Similar to List Comprehension in python)
*
* Rustlang do not have default list comprehension feature, however we are
* assigned a task to create our own syntax similar to list comprehension.
*
* NOTE: the example from `macros/declarative.rs` contains the most compatible
* version that complies with python's list comprehension.
*
* Structure:
*
* Pattern 1:
* comprehension!{foreach <iterable>; apply <function>}
* Pattern 2:
* comprehension!{foreach <iterable>; apply <function>; where <condition>}
*
*
* To run the code, run the following:
* =============================================================================
*
* cargo run --bin m3
* cargo test --bin m3
*
* =============================================================================
**/
macro_rules! comprehension {
(foreach $iterable:expr_2021; apply $function:expr_2021) => {
$iterable.iter().map($function).collect::<Vec<_>>()
};
(foreach $iterable:expr_2021; apply $function:expr_2021; where $condition:expr_2021) => {
$iterable
.iter()
.filter($condition)
.map($function)
.collect::<Vec<_>>()
};
}
// to execute, run: cargo run --bin comprehension
fn main() {
let arr = vec![1, 2, 3, 4, 5];
let out = comprehension!(foreach arr; apply |x|{x * x} );
println!("Comprehension output: {out:?}");
let string_len = comprehension!(foreach vec!["apple", "orange", "banana"]; apply |x| {x.len()});
println!("Comprehension Output [strings]: {string_len:?}");
let numbers = (0..=10).collect::<Vec<_>>();
let even_squared = comprehension!(foreach numbers; apply |x|{x*x}; where |&&x|{x%2==0});
println!("Comprehension Output with filter: {:?}", even_squared);
}
#[cfg(test)]
mod tests {
#[test]
fn test_squares() {
let original = (0..5).collect::<Vec<_>>();
let squares = comprehension!(foreach original; apply |x|{x * x});
assert_eq!(squares, vec![0, 1, 4, 9, 16])
}
#[test]
fn test_squares_with_filter() {
let original = (0..5).collect::<Vec<_>>();
let squares = comprehension!(foreach original; apply |x|{x * x}; where |&&x|{x % 2 == 0});
assert_eq!(squares, vec![0, 4, 16])
}
}