Skip to content

Instantly share code, notes, and snippets.

Created October 11, 2015 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/2050bed351f33117825d to your computer and use it in GitHub Desktop.
Save anonymous/2050bed351f33117825d to your computer and use it in GitHub Desktop.
Shared via Rust Playground
macro_rules! halve {
(@count $mac:tt $xs:tt [] $acc:tt)
=> { halve!(@split $mac $xs [] $acc) };
(@count $mac:tt $xs:tt [$head:expr] $acc:tt)
=> { halve!(@split $mac $xs [] ($acc)) };
(@count $mac:tt $xs:tt [$head:expr, $($tail:expr),+] $acc:tt)
=> { halve!(@count $mac $xs [$($tail),+] ($acc)) };
(@split [$mac:ident!($($arg:expr),*)] $left:tt $right:tt ())
=> { ($mac!($($arg,)* $right), $mac!($($arg,)* $left)) };
(@split $mac:tt [$head:expr, $($tail:expr),+] [$($right:expr),*] (($acc:tt)))
=> { halve!(@split $mac [$($tail),+] [$($right,)* $head] $acc) };
($mac:ident!($($arg:expr),*): $($x:expr),*)
=> { halve!(@count [$mac!($($arg),*)] [$($x),*] [$($x),*] ()) }
}
fn main() {
halve!(println!("{:?}"): 1, 2, 3, 4, 5, 6);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment