Skip to content

Instantly share code, notes, and snippets.

@stevenblenkinsop
Last active August 29, 2015 14:24
Show Gist options
  • Save stevenblenkinsop/5ab263ae71deea8bc518 to your computer and use it in GitHub Desktop.
Save stevenblenkinsop/5ab263ae71deea8bc518 to your computer and use it in GitHub Desktop.
macro_rules! pipe {
(($isn:ident | $($rest:ident)|+): $($args:tt)*) => ($isn!(($($rest)|+): $($args)*));
(($isn:ident): $($args:tt)*) => ($isn!((): $($args)*));
((): $result:expr, $($rest:tt)*) => ($result);
}
macro_rules! succ {
($isns:tt: [$($n:expr),*], $e:expr, $($rest:tt)*) => (pipe!($isns: [$($n,)* $e], $e, $($rest)*));
}
macro_rules! double {
((double $(| $isns:ident)*): [$($n:expr),*], $($rest:tt)*) => (pipe!(($($isns)|*): [$($n),* $(,$n)* $(,$n)* $(,$n)*], $($rest)*));
($isns:tt: [$($n:expr),*], $($rest:tt)*) => (pipe!($isns: [$($n),* $(,$n)*], $($rest)*));
}
macro_rules! zero {
($isns:tt: $e:expr, $($rest:tt)*) => (pipe!($isns: [], $e, $($rest)*));
}
macro_rules! one {
($isns:tt: $e:expr, $($rest:tt)*) => (pipe!($isns: [$e], $e, $($rest)*));
}
macro_rules! four {
(($($isns:ident)|*): $($args:tt)*) => (one!((double | double $(| $isns)*): $($args)*));
}
macro_rules! repeat {
[$e:expr; $n:ident] => ($n!((): $e,))
}
fn main() {
println!("{:?}", repeat![Box::new(5); four]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment