Skip to content

Instantly share code, notes, and snippets.

@stevenblenkinsop
Forked from anonymous/playground.rs
Last active August 29, 2015 14:23
Show Gist options
  • Save stevenblenkinsop/d13da4d79ca73bec00cb to your computer and use it in GitHub Desktop.
Save stevenblenkinsop/d13da4d79ca73bec00cb to your computer and use it in GitHub Desktop.
#![allow(dead_code)]
macro_rules! pipe {
((); $e:expr; $($args:tt)*) => ($e);
(($m:ident | $($ms:ident)|+) ; $($args:tt)*) => ($m!(($($ms)|+) ; $($args)*));
(($m:ident) ; $($args:tt)*) => ($m!((); $($args)*));
($($ms:ident)|*) => (pipe!(($($ms)|*);));
}
macro_rules! zero {
(($($ms:ident)|*) ; $($args:tt)*) => (pipe!(($($ms)|*) ; ; $($args)*));
}
macro_rules! thirty_one {
(($($ms:ident)|*) ; $($args:tt)*) => (pipe!(($($ms)|*) ; ()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()() ; $($args)*));
}
macro_rules! range_rec {
(($($ms:ident)|*) ; $n:expr ; [$($ns:expr),*] ; () $($args:tt)*) => (range_rec!(($($ms)|*) ; $n + 1 ; [$($ns,)* $n] ; $($args)*));
(($($ms:ident)|*) ; $n:expr ; [$($ns:expr),*] ; ; $($args:tt)*) => (pipe!(($($ms)|*) ; [$($ns),*] ; $($args)*));
}
macro_rules! range_decr {
(($($ms:ident)|*) ; $n:expr ; []; () $($args:tt)*) => (pipe!(($($ms)|*) ; $n ; []; $($args)*));
}
macro_rules! range_lead {
(($($ms:ident)|*) ; $n:expr ; () $($args:tt)*) => (range_lead!((range_decr $(| $ms)*) ; $n + 1 ; $($args)*));
(($($ms:ident)|*) ; $n:expr ; ; $($args:tt)*) => (pipe!(($($ms)|*) ; $n ; [] ; $($args)*));
}
macro_rules! range {
(($($ms:ident)|*) ; $($args:tt)*) => (range_lead!((range_rec $(| $ms)*) ; 0 ; $($args)*));
}
macro_rules! map_rule_repr {
($x: expr) => (95.52 / (8128.0 / ($x as f32) + 100.0));
}
macro_rules! map_rule {
(($($ms:ident)|*) ; $($args:tt)*) => (pipe!(($($ms)|*) ; map_rule_repr ; $($args)*));
}
macro_rules! map {
(($($ms:ident)|*) ; $f:ident ; [$($ns:expr),*] ; $($args:tt)*) => (pipe!(($($ms)|*) ; [$($f!($ns)),*] ; $($args)*));
}
fn main() {
println!("{:?}", pipe!(thirty_one | zero | range | map_rule | map));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment