Skip to content

Commit fe0fc47

Browse files
committed
Final run-through
1 parent 2cb0982 commit fe0fc47

File tree

8 files changed

+337
-126
lines changed

8 files changed

+337
-126
lines changed

episodes/01_04_Using_the_package_manager.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ environments. That is useful for development time convenience packages like
163163
Melissa now returns to her project environment.
164164

165165
```julia
166-
(trebuchet) pkg> activate projects/trebuchet
166+
(@v1.11) pkg> activate projects/trebuchet
167167
```
168168

169169
## Using and importing packages

episodes/02_01_Functions.md

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ those that are not exported. What would the function call look like?
8686
2. `names(Trebuchets, all = true)` <!---correct-->
8787
3. `names(Trebuchets, all)`
8888
4. `names(Trebuchets; all = true)` <!---correct-->
89-
5. Answer 2 and 4 <!---correct-->
89+
5. Both 2 and 4 <!---correct-->
9090

9191
:::::: solution
9292

@@ -220,7 +220,7 @@ Trebuchets.shoot(5, 0.25pi, 500)[2]
220220
114.88494815382731
221221
```
222222

223-
which means the shot traveled approximately 118 m.
223+
which means the shot traveled approximately 115 m.
224224

225225
### Defining functions
226226

@@ -423,7 +423,7 @@ She can also lookup the docstring using the `@doc` macro
423423
See also: length, ndims, eachindex, sizeof.
424424
425425
Examples
426-
≡≡≡≡≡≡≡≡≡≡
426+
≡≡≡≡≡≡≡≡
427427
428428
julia> A = fill(1, (2,3,4));
429429
@@ -437,25 +437,6 @@ She can also lookup the docstring using the `@doc` macro
437437
438438
Return a tuple with the size of the buffer.
439439
440-
size(g, i)
441-
442-
Return the number of vertices in g if i=1 or i=2, or 1 otherwise.
443-
444-
Examples
445-
≡≡≡≡≡≡≡≡≡≡
446-
447-
julia> using Graphs
448-
449-
julia> g = cycle_graph(4);
450-
451-
julia> size(g, 1)
452-
4
453-
454-
julia> size(g, 2)
455-
4
456-
457-
julia> size(g, 3)
458-
1
459440
```
460441

461442
With that information she can now implement this method:
@@ -508,7 +489,7 @@ She looks up the documentation for that function
508489
syntax Type[]. Element values can be specified using Type[a,b,c,...].
509490
510491
Examples
511-
≡≡≡≡≡≡≡≡≡≡
492+
≡≡≡≡≡≡≡≡
512493
513494
julia> Int8[1, 2, 3]
514495
3-element Vector{Int8}:
@@ -525,13 +506,12 @@ She looks up the documentation for that function
525506
getindex(collection, key...)
526507
527508
Retrieve the value(s) stored at the given key or index within a collection.
528-
The syntax a[i,j,...] is converted by the compiler to getindex(a, i, j,
529-
...).
509+
The syntax a[i,j,...] is converted by the compiler to getindex(a, i, j, ...).
530510
531511
See also get, keys, eachindex.
532512
533513
Examples
534-
≡≡≡≡≡≡≡≡≡≡
514+
≡≡≡≡≡≡≡≡
535515
536516
julia> A = Dict("a" => 1, "b" => 2)
537517
Dict{String, Int64} with 2 entries:
@@ -543,12 +523,21 @@ She looks up the documentation for that function
543523
544524
getindex(A, inds...)
545525
546-
Return a subset of array A as specified by inds, where each ind may be, for
547-
example, an Int, an AbstractRange, or a Vector. See the manual section on
548-
array indexing for details.
526+
Return a subset of array A as selected by the indices inds.
527+
528+
Each index may be any supported index type, such as an Integer,
529+
CartesianIndex, range, or array of supported indices. A : may be used to
530+
select all elements along a specific dimension, and a boolean array (e.g. an
531+
Array{Bool} or a BitArray) may be used to filter for elements where the
532+
corresponding index is true.
533+
534+
When inds selects multiple elements, this function returns a newly allocated
535+
array. To index multiple elements without making a copy, use view instead.
536+
537+
See the manual section on array indexing for details.
549538
550539
Examples
551-
≡≡≡≡≡≡≡≡≡≡
540+
≡≡≡≡≡≡≡≡
552541
553542
julia> A = [1 2; 3 4]
554543
2×2 Matrix{Int64}:
@@ -568,14 +557,35 @@ She looks up the documentation for that function
568557
3
569558
2
570559
4
560+
561+
julia> getindex(A, 2, 1)
562+
3
563+
564+
julia> getindex(A, CartesianIndex(2, 1))
565+
3
566+
567+
julia> getindex(A, :, 2)
568+
2-element Vector{Int64}:
569+
2
570+
4
571+
572+
julia> getindex(A, 2, :)
573+
2-element Vector{Int64}:
574+
3
575+
4
576+
577+
julia> getindex(A, A .> 2)
578+
2-element Vector{Int64}:
579+
3
580+
4
571581
572582
getindex(tree::GitTree, target::AbstractString) -> GitObject
573583
574-
Look up target path in the tree, returning a GitObject (a GitBlob in the
575-
case of a file, or another GitTree if looking up a directory).
584+
Look up target path in the tree, returning a GitObject (a GitBlob in the case
585+
of a file, or another GitTree if looking up a directory).
576586
577587
Examples
578-
≡≡≡≡≡≡≡≡≡≡
588+
≡≡≡≡≡≡≡≡
579589
580590
tree = LibGit2.GitTree(repo, "HEAD^{tree}")
581591
readme = tree["README.md"]
@@ -594,13 +604,21 @@ She looks up the documentation for that function
594604
595605
Returns a vector with all elements of array partition A.
596606
607+
getindex(
608+
c::SciMLBase.AbstractClock,
609+
idx
610+
) -> SciMLBase.IndexedClock
611+
612+
613+
Return a SciMLBase.IndexedClock representing the subset of the time points
614+
that the clock ticked indicated by idx.
615+
597616
v = sd[k]
598617
599618
Argument sd is a SortedDict and k is a key. In an expression, this retrieves
600-
the value (v) associated with the key (or KeyError if none). On the
601-
left-hand side of an assignment, this assigns or reassigns the value
602-
associated with the key. (For assigning and reassigning, see also insert!
603-
below.) Time: O(c log n)
619+
the value (v) associated with the key (or KeyError if none). On the left-hand
620+
side of an assignment, this assigns or reassigns the value associated with the
621+
key. (For assigning and reassigning, see also insert! below.) Time: O(c log n)
604622
605623
cb[i]
606624
@@ -612,13 +630,9 @@ She looks up the documentation for that function
612630
613631
getindex(tree, ind)
614632
615-
Gets the key present at index ind of the tree. Indexing is done in
616-
increasing order of key.
617-
618-
g[iter]
633+
Gets the key present at index ind of the tree. Indexing is done in increasing
634+
order of key.
619635
620-
Return the subgraph induced by iter. Equivalent to induced_subgraph(g,
621-
iter)[1].
622636
```
623637

624638
Note that the documentation for all methods gets shown and Melissa needs to look

episodes/02_02_Interfaces_and_conditionals.md

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,25 @@ Before starting to work in a new document, Melissa has to:
2828

2929
1. Activate her environment
3030

31-
````julia
31+
```julia
3232
using Pkg
3333
Pkg.activate(joinpath(@__DIR__, "projects", "trebuchet"))
3434
Pkg.instantiate()
35-
````
35+
```
3636

37-
````output
37+
```output
3838
Activating project at `~/projects/trebuchet`
39-
````
39+
```
4040

4141
2. Import the package under its modified name
4242

43-
````julia
43+
```julia
4444
import Trebuchet as Trebuchets
45-
````
45+
```
4646

4747
3. Define the structures
4848

49-
````julia
49+
```julia
5050
mutable struct Trebuchet <: AbstractVector{Float64}
5151
counterweight::Float64
5252
release_angle::Float64
@@ -58,12 +58,12 @@ struct Environment
5858
end
5959

6060
Base.size(::Trebuchet) = tuple(2)
61-
````
61+
```
6262

6363
Now that Melissa knows that she has to add a method for
64-
````
64+
```
6565
getindex(trebuchet::Trebuchet, i::Int)
66-
````
66+
```
6767
she thinks about the implementation.
6868

6969
If the index is `1` she wants to get the `counterweight` field and if the index
@@ -73,7 +73,7 @@ to specify conditions are `if`, `elseif` and `else`, closed with an `end`.
7373

7474
Thus she writes
7575

76-
````julia
76+
```julia
7777
function Base.getindex(trebuchet::Trebuchet, i::Int)
7878
if i === 1
7979
return trebuchet.counterweight
@@ -83,19 +83,19 @@ function Base.getindex(trebuchet::Trebuchet, i::Int)
8383
error("Trebuchet only accepts indices 1 and 2, yours is $i")
8484
end
8585
end
86-
````
86+
```
8787

8888
And tries again:
8989

90-
````julia
90+
```julia
9191
trebuchet = Trebuchet(500, 0.25pi)
92-
````
92+
```
9393

94-
````output
94+
```output
9595
2-element Trebuchet:
9696
500.0
9797
0.7853981633974483
98-
````
98+
```
9999

100100
Notice, that the printing is different from our `trebuchet` in [the former
101101
episode](01_03_Julia_type_system.md).
@@ -120,54 +120,55 @@ Now, that Melissa implemented the mandatory methods for this interface for the
120120
`AbstractArray`. She tries a few things that now work without her writing
121121
explicit code for it:
122122

123-
````julia
123+
```julia
124124
trebuchet + trebuchet
125-
````
125+
```
126126

127-
````output
127+
```output
128128
2-element Vector{Float64}:
129129
1000.0
130130
1.5707963267948966
131-
````
131+
```
132132

133-
````julia
133+
```julia
134134
using LinearAlgebra
135135
dot(trebuchet, trebuchet)
136-
````
136+
```
137137

138-
````output
138+
```output
139139
250000.61685027508
140-
````
140+
```
141141

142-
````julia
142+
```julia
143143
trebuchet * transpose(trebuchet)
144-
````
144+
```
145145

146-
````output
146+
```output
147147
2×2 Matrix{Float64}:
148148
250000.0 392.699
149149
392.699 0.61685
150-
````
150+
```
151151

152152
That is, it now behaves like you would expect from an ordinary matrix.
153153

154-
Now she goes about implementing the missing optional method for `setindex!` of the `AbstractArray` interface.
154+
Now she goes about implementing the missing optional method for `setindex!` of
155+
the `AbstractArray` interface.
155156

156157
:::::: challenge
157158

158159
## Implement `setindex!`
159160

160-
Write the missing method for `setindex(trebuchet::Trebuchet, v, i::Int)` similar
161-
to Melissas `getindex` function.
161+
Write the missing method for `setindex!(trebuchet::Trebuchet, v, i::Int)`
162+
similar to Melissas `getindex` function.
162163

163164
:::::: solution
164165

165166
## Solution
166167

167168
```julia
168169
function Base.setindex!(trebuchet::Trebuchet, v, i::Int)
169-
if i === 1
170-
trebuchet.counterweight = v
170+
if i === 1
171+
trebuchet.counterweight = v
171172
elseif i === 2
172173
trebuchet.release_angle = v
173174
else
@@ -184,23 +185,23 @@ end
184185
With the new `Trebuchet` defined with a complete `AbstractArray` interface,
185186
Melissa tries her new method to modify a counterweight by index:
186187

187-
````julia
188+
```julia
188189
trebuchet[1] = 2
189-
````
190+
```
190191

191-
````output
192+
```output
192193
2
193-
````
194+
```
194195

195-
````julia
196+
```julia
196197
trebuchet
197-
````
198+
```
198199

199-
````output
200+
```output
200201
2-element Trebuchet:
201202
2.0
202203
0.7853981633974483
203-
````
204+
```
204205

205206
:::::: keypoints
206207

0 commit comments

Comments
 (0)