Created
August 3, 2018 04:33
-
-
Save sgrankin/a485885bebecfa1c21e2f617e70c270b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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