{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to metaprogramming: \"Code that creates code\" " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Julia has strong **metaprogramming** capabilities. What does this mean?\n", "\n", "> **meta**: something on a higher level\n", "\n", "**metaprogramming** = \"higher-level programming\"\n", "\n", "i.e. writing code (a program) to manipulate not data, but code (that itself manipulates data)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Motivating example: Interact.jl" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise 1\n", "\n", "1. Install the `Interact.jl` package.\n", "\n", "\n", "2. Run the following code" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The square of 1 is 1\n", "The square of 2 is 4\n", "The square of 3 is 9\n", "The square of 4 is 16\n", "The square of 5 is 25\n", "The square of 6 is 36\n", "The square of 7 is 49\n", "The square of 8 is 64\n", "The square of 9 is 81\n", "The square of 10 is 100\n" ] } ], "source": [ "for i in 1:10\n", " j = i^2\n", " println(\"The square of $i is $(j)\")\n", "end" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "