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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//! Macros for Python-like list comprehension creation of iterators.
//!
//! Another Crate that tries to bring the simplicty of Python's syntax to Rust iterators.
//!
//! The main macro is [`iter`](macro@iter), and other macros are extensions of the latter.
//!
//! # Examples
//!
//! Below, small examples of how the macros work:
//! ```rust
//! use comptools::*;
//!
//! let vec: Vec<u64> = vect![x*x; for x in 0..=10; if x % 2 == 0];
//! assert_eq!(vec, vec![0, 4, 16, 36, 64, 100]);
//!
//! let sum: u64 = sum![x*x; for x in 1..; while x*x*x < 1234];
//! assert_eq!(sum, 385);
//! ```
/// Create an iterator using Python's list-comprehension style.
///
/// # Basic usage
///
/// ```rust
/// # #[macro_use] extern crate comptools;
/// // iter![f(x); for x in iter];
/// // Create an iterator
/// let iter = iter![x*x; for x in 1..10];
/// assert_eq!(iter.collect::<Vec<_>>(), vec![1, 4, 9, 16, 25, 36, 49, 64, 81]);
/// ```
///
/// # Variants
///
/// Below are all the possible variants you can use with the [`iter`](macro@iter) macro.
///
/// ## Filter by value
///
/// **Warning:** filter condition uses references
///
/// ```rust
/// # #[macro_use] extern crate comptools;
/// // iter![f(x); for x in iter; if cond(x)];
/// let iter = iter![x*x; for x in 1..10; if x < &5];
/// assert_eq!(iter.collect::<Vec<_>>(), vec![1, 4, 9, 16]);
/// // or:
/// // iter![f(x); if cond(x); for x in iter];
/// let iter = iter![x*x; if x < &5; for x in 1..10];
/// assert_eq!(iter.collect::<Vec<_>>(), vec![1, 4, 9, 16]);
/// // Same as filter_map
/// ```
///
/// ## Conditional mapping
/// ```rust
/// # #[macro_use] extern crate comptools;
/// // iter![f(x); for x in iter; if cond(x); else g(x)];
/// let iter = iter![x*x; for x in 1..10; if x < 5; else 0];
/// assert_eq!(iter.collect::<Vec<_>>(), vec![1, 4, 9, 16, 0, 0, 0, 0, 0]);
/// // or
/// // iter![f(x); if cond(x); else g(x); for x in iter];
/// let iter = iter![x*x; if x < 5; else 0; for x in 1..10];
/// assert_eq!(iter.collect::<Vec<_>>(), vec![1, 4, 9, 16, 0, 0, 0, 0, 0]);
/// ```
///
/// ## Map while
///
/// **Warning:** while condition uses references
///
/// ```rust
/// # #[macro_use] extern crate comptools;
/// // iter![f(x); for x in iter; while cond(x)];
/// let iter = iter![x*x; for x in 1..10; while x < &5];
/// assert_eq!(iter.collect::<Vec<_>>(), vec![1, 4, 9, 16]);
/// // or
/// // iter![f(x); while cond(x); for x in iter];
/// let iter = iter![x*x; while x < &5; for x in 1..10];
/// assert_eq!(iter.collect::<Vec<_>>(), vec![1, 4, 9, 16]);
/// ```
/// Return sum of values of an iterator using Python's list-comprehension style.
///
/// # Basic usage
///
/// ```rust
/// # #[macro_use] extern crate comptools;
/// // sum![f(x); for x in iter];
/// // Create an iterator and sum its values
/// let sum: u64 = sum![x*x; for x in 1..10];
/// assert_eq!(sum, 285);
/// // Same as iter![...].sum()
/// ```
///
/// For more details, refer to the documentation of [`iter`](macro@iter).
/// Return product of values of an iterator using Python's list-comprehension style.
///
/// # Basic usage
///
/// ```rust
/// # #[macro_use] extern crate comptools;
/// // product![f(x); for x in iter];
/// // Create an iterator and multiply its values
/// let product: u64 = product![x*x; for x in 1..10];
/// assert_eq!(product, 131681894400);
/// // Same as iter![...].product()
/// ```
///
/// For more details, refer to the documentation of [`iter`](macro@iter).
/// Create a collection using Python's list-comprehension style.
///
/// # Basic usage
///
/// ```rust
/// # #[macro_use] extern crate comptools;
/// // vect![f(x); for x in iter];
/// // Create a collection
/// let vec: Vec<u64> = vect![x*x; for x in 1..10];
/// assert_eq!(vec, vec![1, 4, 9, 16, 25, 36, 49, 64, 81]);
/// // Same as iter![...].collect()
/// ```
///
/// For more details, refer to the documentation of [`iter`](macro@iter).