#comprehension #coroutine #vector #macro #python #hash-map

nightly mapcomp

Python-like list comprehensions for standard containers

8 releases

0.3.0 Aug 27, 2024
0.2.2 Mar 9, 2020
0.2.1 Nov 7, 2019
0.1.3 Sep 16, 2018
0.1.1 Apr 16, 2018

#1259 in Rust patterns

Download history 164/week @ 2024-08-26 16/week @ 2024-09-16 14/week @ 2024-09-23 6/week @ 2024-09-30 1/week @ 2024-10-14 72/week @ 2024-12-09

72 downloads per month

MIT/Apache

16KB
265 lines

mapcomp

crates.io Build Status

Python-like list comprehension via macros for the following standard containers:

  • Vec
  • HashMap
  • HashSet
  • BTreeMap
  • BTreeSet

Also provides another macro for coroutine comprehension.

For more info please read the documentation here.

License

Dual-licensed under Apache License, Version 2.0 and the MIT License.


lib.rs:

Macros for container comprehensions similar to Python's list comprehension.

This crate adds vector, set, map, and coroutine comprehensions. It is meant to complement maplit which provides macro literals for the same standard containers.

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 comprehension. There is an additional macro iterc!() for creating lazily evaluated coroutine expressions.

List comprehensions exist in many languages 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.

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]);

No runtime deps

Features