List Comprehension
A macro for Haskell-like list comprehensions in Rust
Syntax:
ListComprehensionExp:
comp![ Exp , Qual1 , . . . , Qualn ] (list comprehension, n ≥ 1 )
| comp![ Exp ; Qual1 , . . . , Qualn ] (list comprehension, n ≥ 1 )
| comp![ Exp => Qual1 , . . . , Qualn ] (list comprehension, n ≥ 1 )
| lazy_comp![ Exp , Qual1 , . . . , Qualn ] (lazy list comprehension, n ≥ 1 )
| lazy_comp![ Exp ; Qual1 , . . . , Qualn ] (lazy list comprehension, n ≥ 1 )
| lazy_comp![ Exp => Qual1 , . . . , Qualn ] (lazy list comprehension, n ≥ 1 )
Qual:
Ident <- Exp (generator)
| (for)? Pattern in Exp (generator[2])
| let Decl (local declaration)
| let DeclWithElse (local declaration)
| let (mut)? Decls (local declaration[1])
| Exp(bool) (boolean guard)
Decls:
{ Decl1 ; . . . ; Decln } (n ≥ 0)
Decl:
(mut)? Ident ( : Type )? = Exp
| Pat = Exp ( , else { ... } )?
DeclWithElse:
Pat = Exp else { ... }
[1] if `mut` is used, then all declarations will be added with `mut` unless pattern matching is used
[2] Since parsing the `Pattern in Exp` syntax and the `Exp` (boolean guard) syntax can be ambiguous,
you can now force the `Pattern in Exp` syntax by prefixing the `Pattern` with `for`
Examples:
Update
- v0.2.0:
- Added
lazy_comp
macro, which supports lazy evaluation. Its syntax is the same ascomp!
.
- Added
- v0.1.5:
- Supports original
let else
syntax, but you can't use it in thelet { ... }
syntax. - Now you can force the
Pattern in Exp
syntax by prefixing thePattern
withfor
(See Syntax for details).
- Supports original
- v0.1.4:
- Allow
pattern matching
and a bit differentlet else
inlocal declaration
. - Made some optimizations.
- Corrected a little bit of mistakes in README.
- Allow