Skip to content

Commit

Permalink
Implement 3 nested cvec
Browse files Browse the repository at this point in the history
  • Loading branch information
GrayJack committed Jun 30, 2019
1 parent 8635e52 commit c2f31a7
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changes

## Unreleased ~ master
Implement 3 nested `cvec`
Implement 2 nested `cvec`
Improve of `cset` macro using `Iterators` methods (doesn't bring a lot of performance improvements, but the code is cleaner)
Macro `time` now prints to stderr and format the time to 6 digits after the dot
Expand Down
90 changes: 89 additions & 1 deletion src/comprehension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
///
/// Supports 2 types of grammars: Haskell-like and Python-like
///
/// **Limitations:** Only 2 nested comprehensions
/// **Limitations:** Only 3 nested comprehensions
///
/// # Examples:
/// ```
Expand Down Expand Up @@ -83,6 +83,58 @@ macro_rules! cvec {
}
v
}};

($e:expr; $i1:ident <- $iter1:expr, $i2:ident <- $iter2:expr, $i3:ident <- $iter3:expr) => {{
let mut v = Vec::new();
for $i1 in $iter1 {
for $i2 in $iter2 {
for $i3 in $iter3 {
v.push($e);
}
}
}
v
}};

($e:expr; $i1:ident <- $iter1:expr, $i2:ident <- $iter2:expr, $i3:ident <- $iter3:expr, if $cond:expr) => {{
let mut v = Vec::new();
for $i1 in $iter1 {
for $i2 in $iter2 {
for $i3 in $iter3 {
if $cond {
v.push($e);
}
}
}
}
v
}};

($e:expr; for $i1:ident in $iter1:expr, for $i2:ident in $iter2:expr, for $i3:ident in $iter3:expr) => {{
let mut v = Vec::new();
for $i1 in $iter1 {
for $i2 in $iter2 {
for $i3 in $iter3 {
v.push($e);
}
}
}
v
}};

($e:expr; for $i1:ident in $iter1:expr, for $i2:ident in $iter2:expr, for $i3:ident in $iter3:expr, if $cond:expr) => {{
let mut v = Vec::new();
for $i1 in $iter1 {
for $i2 in $iter2 {
for $i3 in $iter3 {
if $cond {
v.push($e);
}
}
}
}
v
}};
}

/// Macro to `HashMap` collection comprehensions
Expand Down Expand Up @@ -212,6 +264,24 @@ mod tests {
assert_eq!(expected, test);
}

#[test]
fn cvec_haskell_3_nested_no_conditional() {
let expected = vec![(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 2, 2), (1, 2, 3), (1, 2, 4), (1, 3, 3), (1, 3, 4), (1, 4, 4), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 3, 3), (2, 3, 4), (2, 4, 4), (3, 3, 3), (3, 3, 4), (3, 4, 4), (4, 4, 4)];
let n: i32 = 4;
let test = cvec![(x,y, z); x <- 1..=n, y <- x..=n, z <- y..=n];

assert_eq!(expected, test);
}

#[test]
fn cvec_haskell_3_nested_with_conditional() {
let expected = vec![(3, 4, 5), (6, 8, 10)];
let n: i32 = 10;
let test = cvec![(x, y, z); x <- 1..=n, y <- x..=n, z <- y..=n, if x.pow(2) + y.pow(2) == z.pow(2)];

assert_eq!(expected, test);
}

#[test]
fn cvec_python_basic_no_conditional() {
let expected = vec![2, 4, 6, 8];
Expand Down Expand Up @@ -246,6 +316,24 @@ mod tests {
assert_eq!(expected, test);
}

#[test]
fn cvec_python_3_nested_no_conditional() {
let expected = vec![(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 2, 2), (1, 2, 3), (1, 2, 4), (1, 3, 3), (1, 3, 4), (1, 4, 4), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 3, 3), (2, 3, 4), (2, 4, 4), (3, 3, 3), (3, 3, 4), (3, 4, 4), (4, 4, 4)];
let n: i32 = 4;
let test = cvec![(x, y, z); for x in 1..=n, for y in x..=n, for z in y..=n];

assert_eq!(expected, test);
}

#[test]
fn cvec_python_3_nested_with_conditional() {
let expected = vec![(3, 4, 5), (6, 8, 10)];
let n: i32 = 10;
let test = cvec![(x,y, z); for x in 1..=n, for y in x..=n, for z in y..=n, if x.pow(2) + y.pow(2) == z.pow(2)];

assert_eq!(expected, test);
}

#[test]
fn cmap_haskell_no_conditional() {
let a = 10;
Expand Down

0 comments on commit c2f31a7

Please sign in to comment.