Macros and generated functions
Julia provides a very powerful macro system that lets include generated code in the body of your program. They are executed at compile time and enable you to operate on the source code's Abstract Syntax Tree (AST).
Getting ready
Macros and generated functions are a built-in language functionality, and hence no installation is required. However, in this recipe, we will be also measuring the code performance. For this purpose, theBenchmarkTools.jl
package will be used. In order to install it press the] key to start the Julia package manager and type:
(v1.0) pkg>addBenchmarkTools
Note
In the GitHub repository for this recipe, you will find the commands.txt
file that contains the presented sequence of Julia commands.
How to do it...
This recipe contains two parts. In the first part, we will illustrate how to use Julia to construct a macro to cache the results of a function. In the second part, we will show you how to write a generated function that speeds up computing...