Skip to content

Commit 0d816b3

Browse files
committed
after merge
2 parents 92b9d90 + 1b074b0 commit 0d816b3

File tree

4 files changed

+795
-6
lines changed

4 files changed

+795
-6
lines changed

docs/src/lecture_07/lab.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ end
6565
```
6666

6767
We can remove this boilerplate code by creating a very simple macro that does this for us.
68-
6968
!!! warning "Exercise"
7069
```
7170
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,7 +152,7 @@ p(2)
153152
@test p(2) == evalpoly(2, [10,2,3]) # reversed coefficients
154153
```
155154

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).
157156

158157
!!! details
159158
```@repl lab07_poly
@@ -208,9 +207,9 @@ Moving on to the first/harder case, where we need to parse the mathematical expr
208207
```julia
209208
julia> using MacroTools: prewalk, postwalk
210209
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
214213
215214
julia> postwalk(ex) do x
216215
@capture(x, fun_(arg_)) && println("Function: ", fun, " with argument: ", arg)
@@ -351,6 +350,23 @@ Unfortunately the current version of `Ecosystem` and `EcosystemCore`, already co
351350
🐑 ❌ ❌ ✅ ✅
352351
🐺 ✅ ❌ ❌ ❌
353352
```
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+
```
354370

355371
!!! warning "Exercise"
356372
Based on the following example syntax,

interactive_notebooks/Manifest.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
julia_version = "1.10.0"
44
manifest_format = "2.0"
5-
project_hash = "c54b50ce5185d4f54f0523c22e16470302bde542"
5+
project_hash = "ca141a29eb4222ca691ad805463e630faabbbeca"
66

77
[[deps.AbstractTrees]]
88
git-tree-sha1 = "2d9c9a55f9c93e8887ad391fbae72f8ef55e1177"

interactive_notebooks/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
33
GraphRecipes = "bd48cda9-67a9-57be-86fa-5b3c104eda73"
44
IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a"
5+
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
56
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"

0 commit comments

Comments
 (0)