|
65 | 65 | ``` |
66 | 66 |
|
67 | 67 | We can remove this boilerplate code by creating a very simple macro that does this for us. |
68 | | - |
69 | 68 | !!! warning "Exercise" |
70 | 69 | ``` |
71 | 70 | Define macro `@repeat` that takes two arguments, first one being the number of times a code is to be run and the other being the actual code. |
|
153 | 152 | @test p(2) == evalpoly(2, [10,2,3]) # reversed coefficients |
154 | 153 | ``` |
155 | 154 |
|
156 | | - [^1]: Explanation of the Horner schema can be found on [https://en.wikipedia.org/wiki/Horner%27s\_method](https://en.wikipedia.org/wiki/Horner%27s_method). |
| 155 | +[^1]: Explanation of the Horner schema can be found on [https://en.wikipedia.org/wiki/Horner%27s\_method](https://en.wikipedia.org/wiki/Horner%27s_method). |
157 | 156 |
|
158 | 157 | !!! details |
159 | 158 | ```@repl lab07_poly |
@@ -208,9 +207,9 @@ Moving on to the first/harder case, where we need to parse the mathematical expr |
208 | 207 | ```julia |
209 | 208 | julia> using MacroTools: prewalk, postwalk |
210 | 209 | julia> ex = quote |
211 | | - x = f(y, g(z)) |
212 | | - return h(x) |
213 | | - end |
| 210 | + x = f(y, g(z)) |
| 211 | + return h(x) |
| 212 | + end |
214 | 213 | |
215 | 214 | julia> postwalk(ex) do x |
216 | 215 | @capture(x, fun_(arg_)) && println("Function: ", fun, " with argument: ", arg) |
@@ -351,6 +350,23 @@ Unfortunately the current version of `Ecosystem` and `EcosystemCore`, already co |
351 | 350 | 🐑 ❌ ❌ ✅ ✅ |
352 | 351 | 🐺 ✅ ❌ ❌ ❌ |
353 | 352 | ``` |
| 353 | +!!! warning "Exercise" |
| 354 | + Based on the following example syntax, |
| 355 | + ```julia |
| 356 | + @species Plant Broccoli 🥦 |
| 357 | + @species Animal Rabbit 🐇 |
| 358 | + ``` |
| 359 | + write macro `@species` inside `Ecosystem` pkg, which defines the abstract type, its show function and exports the type. For example `@species Plant Broccoli 🥦` should generate code: |
| 360 | + ```julia |
| 361 | + abstract type Broccoli <: PlantSpecies end |
| 362 | + Base.show(io::IO,::Type{Broccoli}) = print(io,"🥦") |
| 363 | + export Broccoli |
| 364 | + ``` |
| 365 | + Define first helper function `_species` to inspect the macro's output. This is indispensable, as we are defining new types/constants and thus we may otherwise encounter errors during repeated evaluation (though only if the type signature changed). |
| 366 | + ```julia |
| 367 | + _species(:Plant, :Broccoli, :🥦) |
| 368 | + _species(:Animal, :Rabbit, :🐇) |
| 369 | + ``` |
354 | 370 |
|
355 | 371 | !!! warning "Exercise" |
356 | 372 | Based on the following example syntax, |
|
0 commit comments