Skip to content

Instantly share code, notes, and snippets.

@sgrankin
Created August 3, 2018 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgrankin/a485885bebecfa1c21e2f617e70c270b to your computer and use it in GitHub Desktop.
Save sgrankin/a485885bebecfa1c21e2f617e70c270b to your computer and use it in GitHub Desktop.
// inspired by clojure's ->, pipeline.rs, and s-expressions
macro_rules! pipe_fun {
// [as t] => it as t
([as $typ:ty], $ret:expr) => {
$ret as $typ;
};
// ? => try!(it)
(?, $ret:expr) => {
try!($ret)
};
// [.a b c] => it.a(b, c)
([. $fun:tt $($args:tt)*], $ret:expr) => {
$ret.$fun($($args),*);
};
// [a ; b c] => a(it, b, c)
([$fun:tt ; $($rargs:expr)*], $ret:expr) => {
$fun($ret, $($rargs),*);
};
// [a b c ; d] => a(b, c, it, d)
([$fun:tt $($largs:expr)+ ; $($rargs:expr)*], $ret:expr) => {
$fun($($largs),*, $ret, $($rargs),*);
};
// [a b c] => a(it, b, c)
([$fun:tt $($args:expr)*], $ret:expr) => {
$fun($ret, $($args),*);
};
// (a) => a(it)
($fun:tt, $ret:expr) => {
$fun($ret);
};
}
macro_rules! pipe {
($expr:expr => $($funs:tt)+) => {
{
let ret = $expr;
$( let ret = pipe_fun!($funs, ret);)*
ret
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment