Skip to content

Commit caf4f0a

Browse files
committed
Lab6: Expanding exercise text.
1 parent 18fb047 commit caf4f0a

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

docs/src/lecture_06/lab.md

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ function implicit_len()
5959
end
6060
nothing #hide
6161
```
62+
For now do not try to understand the details, but focus on the overall differences such as length of the code.
6263

6364
!!! info "Redirecting `stdout`"
6465
If the output of the method introspection tools is too long you can use a general way of redirecting standard output `stdout` to a file
@@ -179,9 +180,36 @@ function polynomial(a, x)
179180
end
180181
```
181182

183+
!!! info "Splatting/slurping operator `...`"
184+
The operator `...` serves two purposes inside function calls [^3][^4]:
185+
- combines multiple arguments into one
186+
```@repl lab06_splat
187+
function printargs(args...)
188+
println(typeof(args))
189+
for (i, arg) in enumerate(args)
190+
println("Arg #$i = $arg")
191+
end
192+
end
193+
printargs(1, 2, 3)
194+
```
195+
- splits one argument into many different arguments
196+
```@repl lab06_splat
197+
function threeargs(a, b, c)
198+
println("a = $a::$(typeof(a))")
199+
println("b = $b::$(typeof(b))")
200+
println("c = $c::$(typeof(c))")
201+
end
202+
threeargs([1,2,3]...) # or with a variable threeargs(x...)
203+
```
204+
205+
[^3]: [https://docs.julialang.org/en/v1/manual/faq/#What-does-the-...-operator-do?](https://docs.julialang.org/en/v1/manual/faq/#What-does-the-...-operator-do?)
206+
[^4]: [https://docs.julialang.org/en/v1/manual/functions/#Varargs-Functions](https://docs.julialang.org/en/v1/manual/functions/#Varargs-Functions)
207+
182208
**HINTS**:
183-
- define two methods `_polynomial(x, a...)` and `_polynomial(x, a)`
184-
- recall that these kind of optimization are run just after type inference
209+
- define two methods `_polynomial!(ac, x, a...)` and `_polynomial!(ac, x, a)` for the case of ≥2 coefficients and the last coefficient
210+
- use splatting together with range indexing `a[1:end-1]...`
211+
- the correctness can be checked using the built-in `evalpoly`
212+
- recall that these kind of optimization are possible just around the type inference stage
185213
- use container of known length to store the coefficients
186214

187215
```@raw html
@@ -200,8 +228,8 @@ a = Tuple(ones(Int, 21)) # everything less than 22 gets inlined
200228
x = 2
201229
polynomial(a,x) == evalpoly(x,a) # compare with built-in function
202230
231+
# @code_llvm polynomial(a,x) # seen here too, but code_typed is a better option
203232
@code_lowered polynomial(a,x) # cannot be seen here as optimizations are not applied
204-
@code_llvm polynomial(a,x) # seen here too, but code_typed is a better option
205233
nothing #hide
206234
```
207235

@@ -216,7 +244,7 @@ nothing #hide
216244
## AST manipulation: The first steps to metaprogramming
217245
Julia is so called homoiconic language, as it allows the language to reason about its code. This capability is inspired by years of development in other languages such as Lisp, Clojure or Prolog.
218246

219-
There are two easy ways to extract/construct the code structure [^3]
247+
There are two easy ways to extract/construct the code structure [^5]
220248
- parsing code stored in string with internal `Meta.parse`
221249
```@repl lab06_meta
222250
code_parse = Meta.parse("x = 2") # for single line expressions (additional spaces are ignored)
@@ -317,15 +345,14 @@ Given a function `replace_i`, which replaces variables `i` for `k` in an express
317345
ex = :(i + i*i + y*i - sin(z))
318346
@test replace_i(ex) == :(k + k*k + y*k - sin(z))
319347
```
320-
write function `sreplace_i(s)` which does the same thing but instead of a parsed expression (AST) it manipulates a string, such as
348+
write a different function `sreplace_i(s)`, which does the same thing but instead of a parsed expression (AST) it manipulates a string, such as
321349
```@repl lab06_meta
322350
s = string(ex)
323351
```
324-
Think of some corner cases, that the method may not handle properly.
325-
326352
**HINTS**:
327353
- Use `Meta.parse` in combination with `replace_i` **ONLY** for checking of correctness.
328-
- You can use the `replace` function.
354+
- You can use the `replace` function in combination with regular expressions.
355+
- Think of some corner cases, that the method may not handle properly.
329356

330357
```@raw html
331358
</div></div>
@@ -394,9 +421,8 @@ eval(ex)
394421

395422
This kind of manipulation is at the core of some pkgs, such as aforementioned [`IntervalArithmetics.jl`](https://github.com/JuliaIntervals/IntervalArithmetic.jl) where every number is replaced with a narrow interval in order to find some bounds on the result of a computation.
396423

397-
398-
[^3]: Once you understand the recursive structure of expressions, the AST can be constructed manually like any other type.
399424
---
425+
[^5]: Once you understand the recursive structure of expressions, the AST can be constructed manually like any other type.
400426

401427
## Resources
402428
- Julia's manual on [metaprogramming](https://docs.julialang.org/en/v1/manual/metaprogramming/)

0 commit comments

Comments
 (0)