You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/lectures/lecture_07/lab.md
+14-14Lines changed: 14 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -65,7 +65,7 @@ end
65
65
```
66
66
67
67
We can remove this boilerplate code by creating a very simple macro that does this for us.
68
-
::: warning "Exercise"
68
+
!!! warning "Exercise"
69
69
```
70
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.
71
71
```julia
@@ -88,7 +88,7 @@ We can remove this boilerplate code by creating a very simple macro that does th
88
88
What happens if we call `@repeat 3 x = 2`? Is `x` defined?
89
89
90
90
91
-
::: details
91
+
!!! details
92
92
```@repl lab07_repeat
93
93
macro repeat(n::Int, ex)
94
94
return _repeat(n, ex)
@@ -125,7 +125,7 @@ p = @poly 3 2 10
125
125
p(2)
126
126
```
127
127
128
-
::: warning "Exercise"
128
+
!!! warning "Exercise"
129
129
Create macro `@poly` that takes multiple arguments and creates an anonymous function that constructs the unrolled code. Instead of directly defining the macro inside the macro body, create helper function `_poly` with the same signature that can be reused outside of it.
130
130
131
131
Recall Horner's method polynomial evaluation from previous [labs](@ref horner):
@@ -154,7 +154,7 @@ p(2)
154
154
155
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).
156
156
157
-
::: details
157
+
!!! details
158
158
```@repl lab07_poly
159
159
using InteractiveUtils #hide
160
160
macro poly(a...)
@@ -177,7 +177,7 @@ p(2)
177
177
178
178
Moving on to the first/harder case, where we need to parse the mathematical expression.
179
179
180
-
::: warning "Exercise"
180
+
!!! warning "Exercise"
181
181
Create macro `@poly` that takes two arguments first one being the independent variable and second one being the polynomial written in mathematical notation. As in the previous case this macro should define an anonymous function that constructs the unrolled code.
182
182
```julia
183
183
julia> p = @poly x 3x^2+2x^1+10x^0 # the first argument being the independent variable to match
@@ -190,7 +190,7 @@ Moving on to the first/harder case, where we need to parse the mathematical expr
190
190
1. get maximal rank of the polynomial
191
191
2. get coefficient for each power
192
192
193
-
::: note "`MacroTools.jl`"
193
+
!!! note "`MacroTools.jl`"
194
194
Though not the most intuitive, [`MacroTools.jl`](https://fluxml.ai/MacroTools.jl/stable/) pkg help us with writing custom macros. We will use two utilities
195
195
#### `@capture`
196
196
This macro is used to match a pattern in a *single* expression and return values of particular spots. For example
@@ -221,7 +221,7 @@ Moving on to the first/harder case, where we need to parse the mathematical expr
221
221
Note that the `x` or the iteration is required, because by default postwalk/prewalk replaces currently read expression with the output of the body of `do` block.
222
222
223
223
224
-
::: details
224
+
!!! details
225
225
```@example lab07_poly
226
226
using MacroTools
227
227
using MacroTools: postwalk, prewalk
@@ -314,7 +314,7 @@ Our goal is to be able to define new plants and animal species, while having a c
314
314
```
315
315
Unfortunately the current version of `Ecosystem` and `EcosystemCore`, already contains some definitions of species such as `Sheep`, `Wolf` and `Mushroom`, which may collide with definitions during prototyping, therefore we have created a modified version of those pkgs, which will be provided in the lab.
316
316
317
-
::: note "Testing relations"
317
+
!!! note "Testing relations"
318
318
We can test the current definition with the following code that constructs "eating matrix"
319
319
```julia
320
320
using Ecosystem
@@ -350,7 +350,7 @@ Unfortunately the current version of `Ecosystem` and `EcosystemCore`, already co
350
350
🐑 ❌ ❌ ✅ ✅
351
351
🐺 ✅ ❌ ❌ ❌
352
352
```
353
-
::: warning "Exercise"
353
+
!!! warning "Exercise"
354
354
Based on the following example syntax,
355
355
```julia
356
356
@species Plant Broccoli 🥦
@@ -368,7 +368,7 @@ Unfortunately the current version of `Ecosystem` and `EcosystemCore`, already co
368
368
_species(:Animal, :Rabbit, :🐇)
369
369
```
370
370
371
-
::: warning "Exercise"
371
+
!!! warning "Exercise"
372
372
Based on the following example syntax,
373
373
```julia
374
374
@species Plant Broccoli 🥦
@@ -396,7 +396,7 @@ Unfortunately the current version of `Ecosystem` and `EcosystemCore`, already co
396
396
**BONUS**:
397
397
Based on `@species` define also macros `@animal` and `@plant` with two arguments instead of three, where the species type is implicitly carried in the macro's name.
398
398
399
-
::: details
399
+
!!! details
400
400
Macro `@species`
401
401
```julia
402
402
macro species(typ, name, icon)
@@ -428,7 +428,7 @@ Unfortunately the current version of `Ecosystem` and `EcosystemCore`, already co
428
428
429
429
The next exercise applies macros to the agents eating behavior.
430
430
431
-
::: warning "Exercise"
431
+
!!! warning "Exercise"
432
432
Define macro `@eats` inside `Ecosystem` pkg that assigns particular species their eating habits via `eat!` and `eats` functions. The macro should process the following example syntax
433
433
```julia
434
434
@eats Rabbit [Grass => 0.5, Broccoli => 1.0],
@@ -441,7 +441,7 @@ The next exercise applies macros to the agents eating behavior.
441
441
- you can create an empty `quote end` block with `code = Expr(:block)` and push new expressions into its `args` incrementally
442
442
- use dispatch to create specific code for the different combinations of agents eating other agents (there may be catch in that we have to first `eval` the symbols before calling in order to know if they are animals or plants)
443
443
444
-
::: note "Reminder of `EcosystemCore` `eat!` and `eats` functionality"
444
+
!!! note "Reminder of `EcosystemCore` `eat!` and `eats` functionality"
445
445
In order to define that an `Wolf` eats `Sheep`, we have to define two methods
0 commit comments