Iterate on expression

Why would one ever want to iterate over expressions? Well its like that: I have a recursive function on expr, to replace a Symbol with a given expression.

ersatz(ex::Symbol,a::Symbol,b) = ex==a ? deepcopy(b) : ex
ersatz(ex::Expr  ,a::Symbol,b) = Expr(ex.head,ersatz.(ex.args,a,b)...)  

show(ersatz(:(12+a*c^3),:a,:(2+2)))

You notice line 2, I can’t be bothered with a for loop, so I use the dot syntax for functions. Now the dot syntax must examine the size and iterative over, all its argument. The last argument, b, tends to be an Expression… for which neither length nor iterate are defined. Boom (Julia 1.0).

My claim: to make dot syntax universally applicable, any type in Julia must be iterable - even though that may be just one step.

And this is not a request for assistance, thank you, I will cope with a for loop! :grinning:

Ref or friends

2 Likes

Ref(b) is iterable, and it works! Thank you!