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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475

//! Macros for container comprehensions similar to Python's list comprehension.
//!
//! This crate adds vector, set, map, and generator comprehensions.  It is
//! meant to complement [maplit](https://docs.rs/maplit/) which provides
//! macro literals for the same standard containers.
//!
//! ```rust
//! # #![feature(match_default_bindings)]
//! # #[macro_use] extern crate mapcomp;
//! # fn main() {
//! let v = vec![3, 2, 6, 9, 5];
//!
//! let even_squares = vecc![x * x; for x in v.iter(); if x % 2 == 0];
//!
//! assert_eq!(even_squares, vec![4, 36]);
//! # }
//! ```
//!
//! The macro names are the same as maplit's container literal macros but with
//! a **c** at the end for **c**omprehension.  There is an additional macro
//! `iterc!()` for creating lazily evaluated generator expressions.
//!
//! List comprehensions exist [in many languages](https://en.wikipedia.org/wiki/List_comprehension)
//! and in many styles.  This crate uses the same syntax as Python's list
//! comprehensions due to it's ease of use, readability, and established
//! popularity.  If you understand Python's list comprehension, then you
//! understand `mapcomp`'s comprehensions.
//!
//! One minor deviation from Python's syntax is the presence of semicolons
//! between clauses which is a limitation of Rust's macro language.  Another
//! difference is that the map comprehensions use the `=>` token to separate
//! the key from the value instead of a colon the way Python does it.
//!
//! The `for var in iteratable` clause can be nested as many times as you want
//! and the `if conditional` is optional after each loop clause.
//!
//! ```rust
//! # #[macro_use] extern crate mapcomp;
//! # fn main() {
//! let grid = vec![vec![-2, 5], vec![3, -7, 6], vec![4, 2]];
//!
//! let v = vecc![x; for row in grid; if row.len() < 3; for x in row; if x > 0];
//!
//! assert_eq!(v, vec![5, 4, 2]);
//! # }
//! ```


#![feature(generators, generator_trait)]


/// This is an implementation detail used by `iterc!()` and it should not be
/// directly instantiated.
#[doc(hidden)]
pub struct GeneratorIterator<G: ::std::ops::Generator> {
    generator: G,
}

impl<G: ::std::ops::Generator> GeneratorIterator<G> {
    pub fn new(generator: G) -> GeneratorIterator<G> {
        GeneratorIterator { generator }
    }
}

impl<G: ::std::ops::Generator> Iterator for GeneratorIterator<G> {
    type Item = G::Yield;

    fn next(&mut self) -> Option<Self::Item> {
        use ::std::ops::GeneratorState;
        match unsafe { self.generator.resume() } {
            GeneratorState::Yielded(y) => Some(y),
            _ => None,
        }
    }
}



/// Iterator Comprehension
///
/// Returns an iterator over the contents of the comprehension.  It is
/// analogous to [Python's generator comprehensions](https://www.python.org/dev/peps/pep-0289/).
/// Syntactically, it is similar to the `vecc![]` macro but it returns a lazily
/// evaluated iterator instead of a container.  It's use requires the experimental
/// generators feature.
///
/// ```rust
/// #![feature(generators, generator_trait)]
/// #[macro_use]
/// extern crate mapcomp;
///
/// # fn main() {
/// let numbers = [8, 3, 5, 7];
///
/// let mut powers_of_two = iterc!(1 << x; for x in &numbers);
///
/// assert_eq!(Some(256), powers_of_two.next());
/// assert_eq!(Some(8), powers_of_two.next());
/// assert_eq!(Some(32), powers_of_two.next());
/// assert_eq!(Some(128), powers_of_two.next());
/// assert_eq!(None, powers_of_two.next());
/// # }
/// ```
///
/// Since it only creates an iterator and not a fully populated container, the
/// comprehension can be created over an unbounded or infinite iterator.
///
/// ```rust
/// # #![feature(generators, generator_trait)]
/// # #[macro_use] extern crate mapcomp;
/// # fn main() {
/// let mut odd_squares = iterc!(x * x; for x in 1..; if x % 2 == 1);
///
/// assert_eq!(Some(1), odd_squares.next());
/// assert_eq!(Some(9), odd_squares.next());
/// assert_eq!(Some(25), odd_squares.next());
/// assert_eq!(Some(49), odd_squares.next());
/// # }
/// ```
#[macro_export]
macro_rules! iterc {
    (@__ $exp:expr; for $item:pat in $iter:expr; if $cond:expr) => (
        for $item in $iter {
            if $cond {
                yield $exp;
            }
        }
    );

    (@__ $exp:expr; for $item:pat in $iter:expr) => (
        for $item in $iter {
            yield $exp;
        }
    );

    (@__ $exp:expr; for $item:pat in $iter:expr; if $cond:expr; $($tail:tt)+) => (
        for $item in $iter {
            if $cond {
                iterc!(@__ $exp; $($tail)+)
            }
        }
    );

    (@__ $exp:expr; for $item:pat in $iter:expr; $($tail:tt)+) => (
        for $item in $iter {
            iterc!(@__ $exp; $($tail)+)
        }
    );

    ($exp:expr; $($tail:tt)+) => ({
        let mut generator = || {
            iterc!(@__ $exp; $($tail)+)
        };
        ::mapcomp::GeneratorIterator::new(generator)
    });
}



/// Vector Comprehension
///
/// Creates a new `Vec` from the contents of the comprehension.  Vector
/// comprehensions are analogous to Python's list comprehension.
///
/// Python code:
///
/// ```python
/// items = [4, 7, 2]
///
/// even_squares = [x*x for x in items if x % 2 == 0]
/// ```
///
/// Rust equivalent code:
///
/// ```
/// # #[macro_use] extern crate mapcomp; fn main() {
/// let items = [4, 7, 2];
///
/// let even_squares = vecc![x*x; for x in &items; if x % 2 == 0];
/// 
/// assert_eq!(even_squares, vec![16, 4]);
/// # }
/// ```
#[macro_export]
macro_rules! vecc {
    (@__ $acc:ident, $exp:expr; for $item:pat in $iter:expr; if $cond:expr) => (
        for $item in $iter {
            if $cond {
                $acc.push($exp);
            }
        }
    );

    (@__ $acc:ident, $exp:expr; for $item:pat in $iter:expr) => (
        for $item in $iter {
            $acc.push($exp);
        }
    );

    (@__ $acc:ident, $exp:expr; for $item:pat in $iter:expr; if $cond:expr; $($tail:tt)+) => (
        for $item in $iter {
            if $cond {
                vecc![@__ $acc, $exp; $($tail)+];
            }
        }
    );

    (@__ $acc:ident, $exp:expr; for $item:pat in $iter:expr; $($tail:tt)+) => (
        for $item in $iter {
            vecc![@__ $acc, $exp; $($tail)+];
        }
    );

    ($exp:expr; $($tail:tt)+) => ({
        let mut ret = ::std::vec::Vec::new();
        vecc![@__ ret, $exp; $($tail)+];
        ret
    });
}



/// Hash Set Comprehension
///
/// Creates a `HashSet` from the contents of the comprehension.  It is
/// analogous to Python's set comprehension.
///
/// Python code:
///
/// ```python
/// matrix = [[3, 8, 7], [9, 5, 3], [4, 5, 6]]
///
/// members = {n for row in matrix for n in row}
/// ```
///
/// Rust equivalent code:
///
/// ```
/// # #[macro_use] extern crate mapcomp;
/// # fn main() {
/// let matrix = [[3, 8, 7], [9, 5, 3], [4, 5, 6]];
///
/// let members = hashsetc!{n; for row in &matrix; for n in row};
///
/// for n in &[3, 8, 7, 9, 5, 4, 6] {
///     assert!(members.contains(n));
/// }
/// # }
/// ```
#[macro_export]
macro_rules! hashsetc {
    (@__ $acc:ident, $exp:expr; for $item:pat in $iter:expr; if $cond:expr) => (
        for $item in $iter {
            if $cond {
                $acc.insert($exp);
            }
        }
    );

    (@__ $acc:ident, $exp:expr; for $item:pat in $iter:expr) => (
        for $item in $iter {
            $acc.insert($exp);
        }
    );

    (@__ $acc:ident, $exp:expr; for $item:pat in $iter:expr; if $cond:expr; $($tail:tt)+) => (
        for $item in $iter {
            if $cond {
                hashsetc!{@__ $acc, $exp; $($tail)+};
            }
        }
    );

    (@__ $acc:ident, $exp:expr; for $item:pat in $iter:expr; $($tail:tt)+) => (
        for $item in $iter {
            hashsetc!{@__ $acc, $exp; $($tail)+};
        }
    );

    ($exp:expr; $($tail:tt)+) => ({
        let mut ret = ::std::collections::HashSet::new();
        hashsetc!{@__ ret, $exp; $($tail)+};
        ret
    });
}



/// Hash Map Comprehension
///
/// Creates a `HashMap` from the contents of the comprehension.  It is
/// analogous to Python's dictionary comprehension except that it uses the `=>`
/// token instead of a colon.
///
/// Python code:
///
/// ```python
/// numbers = [6, 4, 18]
///
/// halves = {str(x): x / 2 for x in numbers}
/// ```
///
/// Rust equivalent code:
///
/// ```rust
/// # #[macro_use] extern crate mapcomp;
/// # fn main() {
/// let numbers = [6, 4, 18];
///
/// let halves = hashmapc!{x.to_string() => x / 2; for x in numbers.iter()};
///
/// for &(k, v) in &[("6", 3), ("4", 2), ("18", 9)] {
///     assert_eq!(halves[k], v);
/// }
/// # }
/// ```
#[macro_export]
macro_rules! hashmapc {
    (@__ $acc:ident, $key:expr => $val:expr; for $item:pat in $iter:expr; if $cond:expr) => (
        for $item in $iter {
            if $cond {
                $acc.insert($key, $val);
            }
        }
    );

    (@__ $acc:ident, $key:expr => $val:expr; for $item:pat in $iter:expr) => (
        for $item in $iter {
            $acc.insert($key, $val);
        }
    );

    (@__ $acc:ident, $key:expr => $val:expr; for $item:pat in $iter:expr; if $cond:expr; $($tail:tt)+) => (
        for $item in $iter {
            if $cond {
                hashmapc!{@__ $acc, $key => $val; $($tail)+};
            }
        }
    );

    (@__ $acc:ident, $key:expr => $val:expr; for $item:pat in $iter:expr; $($tail:tt)+) => (
        for $item in $iter {
            hashmapc!{@__ $acc, $key => $val; $($tail)+};
        }
    );

    ($key:expr => $val:expr; $($tail:tt)+) => ({
        let mut ret = ::std::collections::HashMap::new();
        hashmapc!{@__ ret, $key => $val; $($tail)+};
        ret
    });
}



/// BTree Set Comprehension
///
/// Creates a `BTreeSet` from the contents of the comprehension.  Same syntax as
/// `hashsetc!{}`.
///
/// ```rust
/// # #![feature(match_default_bindings)]
/// # #[macro_use] extern crate mapcomp;
/// # fn main() {
/// let pairs = btreesetc!{(i, j); for i in 4..7; for j in 10..12};
///
/// for i in 4..7 {
///     for j in 10..12 {
///         assert!(pairs.contains(&(i, j)));
///     }
/// }
/// # }
/// ```
#[macro_export]
macro_rules! btreesetc {
    (@__ $acc:ident, $exp:expr; for $item:pat in $iter:expr; if $cond:expr) => (
        for $item in $iter {
            if $cond {
                $acc.insert($exp);
            }
        }
    );

    (@__ $acc:ident, $exp:expr; for $item:pat in $iter:expr) => (
        for $item in $iter {
            $acc.insert($exp);
        }
    );

    (@__ $acc:ident, $exp:expr; for $item:pat in $iter:expr; if $cond:expr; $($tail:tt)+) => (
        for $item in $iter {
            if $cond {
                btreesetc!{@__ $acc, $exp; $($tail)+};
            }
        }
    );

    (@__ $acc:ident, $exp:expr; for $item:pat in $iter:expr; $($tail:tt)+) => (
        for $item in $iter {
            btreesetc!{@__ $acc, $exp; $($tail)+};
        }
    );

    ($exp:expr; $($tail:tt)+) => ({
        let mut ret = ::std::collections::BTreeSet::new();
        btreesetc!{@__ ret, $exp; $($tail)+};
        ret
    });
}



/// BTree Map Comprehension
///
/// Creates a `BTreeMap` from the contents of the comprehension.  Same syntax
/// as `hashmapc!{}`.
///
/// ```rust
/// # #[macro_use] extern crate mapcomp;
/// # fn main() {
/// let array = [5, 3, 9, 6];
///
/// let index_map = hashmapc!{x => i; for (i, x) in array.iter().enumerate()};
///
/// for (i, x) in array.iter().enumerate() {
///     assert_eq!(index_map[x], i);
/// }
/// # }
/// ```
#[macro_export]
macro_rules! btreemapc {
    (@__ $acc:ident, $key:expr => $val:expr; for $item:pat in $iter:expr; if $cond:expr) => (
        for $item in $iter {
            if $cond {
                $acc.insert($key, $val);
            }
        }
    );

    (@__ $acc:ident, $key:expr => $val:expr; for $item:pat in $iter:expr) => (
        for $item in $iter {
            $acc.insert($key, $val);
        }
    );

    (@__ $acc:ident, $key:expr => $val:expr; for $item:pat in $iter:expr; if $cond:expr; $($tail:tt)+) => (
        for $item in $iter {
            if $cond {
                btreemapc!{@__ $acc, $key => $val; $($tail)+};
            }
        }
    );

    (@__ $acc:ident, $key:expr => $val:expr; for $item:pat in $iter:expr; $($tail:tt)+) => (
        for $item in $iter {
            btreemapc!{@__ $acc, $key => $val; $($tail)+};
        }
    );

    ($key:expr => $val:expr; $($tail:tt)+) => ({
        let mut ret = ::std::collections::BTreeMap::new();
        btreemapc!{@__ ret, $key => $val; $($tail)+};
        ret
    });
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        let _v = vecc![x * x; for x in 1..10; if x % 2 == 0];
    }
}