Evaluation in calling scope?

Sounds like you could pass functions instead, since you can chain together functions as well. For example, this combines two single-variable functions f1(x) and f2(x) into a two-variable function with the combine(a, b) function:

combine(combiner, f1, f2) = (x,y) -> combiner(f1(x), f2(y))

combine(+, sqrt, exp) # returns a function (x,y) -> sqrt(x) + exp(y)

This is called a higher-order function in computer science, and is much more flexible (in any language) and much faster (in Julia) than working with raw expressions.

I feel like this should be a FAQ or a PSA post. 90% of the people who ask about evaluating expressions or strings in local scope should actually be using a higher-order function, in my experience.

14 Likes