Skip to content

Commit 5efd880

Browse files
committed
pkg environments for each folder, fix typos
1 parent ed4040b commit 5efd880

File tree

3 files changed

+93
-4
lines changed

3 files changed

+93
-4
lines changed

docs/make.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ lecture_01 = [
2525
]
2626

2727
lecture_02 = [
28-
"Funcion basics" => "./lecture_02/functions.md",
28+
"Function basics" => "./lecture_02/functions.md",
2929
"Conditional evaluations" => "./lecture_02/conditions.md",
3030
"Loops and iterators" => "./lecture_02/loops.md",
3131
"Soft local scope" => "./lecture_02/scope.md",

docs/src/lecture_02/functions.md

+71-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,77 @@ The function definition consists of multiple parts:
2727
Note that not all parts of the function definition above are mandatory. The following parts are optional:
2828

2929
- type annotations
30-
- separator of positinal and keywor arguments
30+
- separator of positional and keyword arguments
3131
- `return` keyword
3232

33-
However, we highly recommend to use these optional features when writing own functions, since it will improve code readibility and prevent unwanted errors.
33+
However, we highly recommend to use these optional features when writing your own functions, since it will improve code readability and prevent unwanted errors.
3434

35-
We will discuss the function declaration in more detail [later](@ref Functions).
35+
!!! warning "Exercise:"
36+
Test how the function behaves with different input values. Test what happens when the input is not a real number, i.e. `x = "a"`.
37+
38+
!!! details "Solution:"
39+
Positional arguments do not need to be named:
40+
```jldoctest functions
41+
julia> quadratic(2)
42+
(7, 5)
43+
```
44+
45+
but the keyword arguments have to be specified. Their order of being called does not matter.
46+
```jldoctest functions
47+
julia> quadratic(2; b=3, a=1)
48+
(11, 7)
49+
```
50+
51+
When a function has specific type annotation, the function throws a `MethodError`, because the function specifically needs `x` to be of type `Real`.
52+
```jldoctest functions
53+
julia> quadratic("a")
54+
ERROR: MethodError: no method matching quadratic(::String)
55+
56+
Closest candidates are:
57+
quadratic(!Matched::Real; a, b, c)
58+
@ Main none:1
59+
60+
Stacktrace:
61+
[1] top-level scope
62+
@ none:1
63+
```
64+
65+
This is different to a a case when we would define the function without the type annotation:
66+
67+
```jldoctest functions; output = false
68+
function quadratic2(x; a = 1, b = 1, c = 1)
69+
value = a*x^2 + b*x + c
70+
deriv = 2* a*x + b
71+
72+
return value, deriv
73+
end
74+
75+
# output
76+
quadratic2 (generic function with 1 method)
77+
```
78+
79+
which returns a different error when calling for `x = "a"`:
80+
81+
```jldoctest functions
82+
julia> quadratic2("a")
83+
ERROR: MethodError: no method matching *(::Int64, ::String)
84+
85+
Closest candidates are:
86+
*(::Any, ::Any, !Matched::Any, !Matched::Any...)
87+
@ Base operators.jl:587
88+
*(::Real, !Matched::Complex{Bool})
89+
@ Base complex.jl:327
90+
*(!Matched::Missing, ::Union{AbstractChar, AbstractString})
91+
@ Base missing.jl:184
92+
...
93+
94+
Stacktrace:
95+
[1] quadratic2(x::String; a::Int64, b::Int64, c::Int64)
96+
@ Main ./none:2
97+
[2] quadratic2(x::String)
98+
@ Main ./none:1
99+
[3] top-level scope
100+
@ none:1
101+
```
102+
103+
We will discuss the function declaration in more detail [later](@ref Functions).

docs/src/lecture_03/pkg.md

+21
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,24 @@ The `Project.toml` file describes the project on a high level. It contains the p
187187
```
188188

189189
Different users may use different package versions. Since updated packages provide different functionality for some functions, instantiating is extremely useful for reproducing code across multiple computers.
190+
191+
## Scripts environments
192+
193+
The [scripts repository](https://github.com/JuliaTeachingCTU/Julia-for-Optimization-and-Learning-Scripts) contains scripts for each lecture. For every lecture, a `Project.toml` file with necessary packages is provided.
194+
195+
VS Code + Julia extension activate a parent folder default environment automatically when you start the Julia REPL with Cmd + Shift + P and choose `Julia: Start REPL` or use the Shift + Enter keyboard shortcut to run a line of a specific script. Since the parent environment is `Julia-for-Optimization-and-Learning`, this environment is activated.
196+
197+
You can either use
198+
199+
```julia
200+
using Pkg
201+
Pkg.activate(pwd() * "/lecture_03")
202+
```
203+
204+
and change the corresponding lecture number, or go to the REPL package manager and activate the environment with
205+
206+
```julia
207+
(Julia-for-Optimization-and-...) pkg> activate lecture_03
208+
```
209+
210+
Don't forget to check all packages are installed with `Pkg.status()` or `pkg> st` and `instantiate` if some packages are missing.

0 commit comments

Comments
 (0)