Last active
August 13, 2017 18:00
-
-
Save Mr-Byte/6015478 to your computer and use it in GitHub Desktop.
Proof of concept macro to implement LINQ syntax in rust.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro_rules! query( | |
(from $v:ident in $c:ident $(where $mw:expr)* select $ms:expr) => | |
($c.filter_mapped(|&$v| if(true $(&& $mw)*) { Some($ms) } else { None })) | |
) | |
fn main() | |
{ | |
let nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
let result1 = query!(from x in nums select x * 2); | |
let result2 = query!(from x in nums | |
where x%2 == 0 | |
select x * 2); | |
let result3 = query!(from x in nums | |
where x%2 == 0 | |
where x%4 == 0 | |
select x * 2); | |
for result1.iter().advance |&num| { | |
println(fmt!("%d", num)); | |
} | |
println("----"); | |
for result2.iter().advance |&num| { | |
println(fmt!("%d", num)); | |
} | |
println("----"); | |
for result3.iter().advance |&num| { | |
println(fmt!("%d", num)); | |
} | |
println("----"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment