Map over expressions?

It might be slightly useful to define

map(f, e::Expr) = Expr(e.head, map(f, e.args)... )

It would be useful for recursive AST functions. Of course, users defining map_expression themselves would probably be fine too, but it’s something I find myself doing repeatedly.

Just out of curiosity, would map!(f, e.args) not be useful?

Definitely in some situations. I tend to avoid mutating in place if at all possible when I program.

2 Likes

While I agree that it would be useful, I would not make it a method of Base.map. I don’t think that map should be defined for composite types, let alone in ways that transform only some of the fields.

That said, I think this would be a useful addition to some macro toolkit packages, maybe one of them will include it. Perhaps submit it as a PR to one of them?

1 Like