Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Last active January 29, 2017 14:27
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 deque-blog/c2ee7a05a64df116e96338303d6b4ed5 to your computer and use it in GitHub Desktop.
Save deque-blog/c2ee7a05a64df116e96338303d6b4ed5 to your computer and use it in GitHub Desktop.
template<typename A, typename M>
auto fmap(M map, expression_r<A> const& e)
{
using B = decltype(map(std::declval<A>()));
using Out = expression_r<B>;
if (auto* o = get_as_add(e))
return Out(add_op<B>(o->rands() | transformed(map)));
if (auto* o = get_as_mul(e))
return Out(mul_op<B>(o->rands() | transformed(map)));
if (auto* i = get_as_cst(e)) return Out(*i);
if (auto* v = get_as_var(e)) return Out(*v);
throw_missing_pattern_matching_clause();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment