Skip to content

Commit 0002be8

Browse files
committed
chore: some touchup for typing and patterns
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
1 parent 93f3ad6 commit 0002be8

File tree

6 files changed

+89
-14
lines changed

6 files changed

+89
-14
lines changed

content/week06/integrator_example/integrator/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
import abc
33

44

5+
__all__ = ["EulerIntegrator", "RK4Integrator"]
6+
7+
8+
def __dir__():
9+
return __all__
10+
11+
512
class IntegratorBase(abc.ABC):
613
@abc.abstractmethod
714
def compute_step(self, f, t_n, y_n, h):

content/week06/introoo.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,41 @@ impl Path {
198198
}
199199
```
200200
````
201+
````{tab-item} Swift
202+
```swift
203+
import Foundation
204+
205+
class Path {
206+
let stringLocation: String
207+
208+
init(stringLocation: String) {
209+
self.stringLocation = stringLocation
210+
}
211+
212+
func exists() -> Bool {
213+
return FileManager.default.fileExists(atPath: stringLocation)
214+
}
215+
}
216+
```
217+
````
218+
````{tab-item} Go
219+
```go
220+
import "os"
221+
222+
type Path struct {
223+
stringLocation string
224+
}
225+
226+
func NewPath(stringLocation string) *Path {
227+
return &Path{stringLocation: stringLocation}
228+
}
229+
230+
func (p *Path) Exists() bool {
231+
_, err := os.Stat(p.stringLocation)
232+
return !os.IsNotExist(err)
233+
}
234+
```
235+
````
201236
`````
202237

203238
This gives us several benefits: If we get a Path, we know it has

content/week07/designpatt.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -845,12 +845,14 @@ We didn't cover every pattern (and we can't), so here are a few more you can
845845
look up if you are curious:
846846

847847
- Singleton pattern: There can only be one instance of a class. `None`, `True`,
848-
and `False` are singletons in Python.
849-
- Registries: `logging` uses this design.
848+
and `False` are singletons in Python. There are several ways to achieve these.
849+
- Registries: `logging` uses this design. You collect a global dict of instances
850+
and can access them by name.
850851
- State machine: this is used heavily by Matlab, and sometimes is seen in things
851-
mimicking it, like matplotlib's "easy" interface.
852+
mimicking it, like matplotlib's "easy" interface. You track which item is
853+
"active". Bad for multithreaded environments.
852854
- Factory pattern: We've touched on this lightly, classes `__init__` method, for
853855
example. You can have other factories with `@classmethod`'s that return new
854-
instances. (or static methods in C++, etc)
856+
instances. (or static methods in C++, etc).
855857
- Ascync patterns: Lightly touched on during generators.
856858
- Event loop: A common pattern for reacting to multiple possible inputs.

content/week07/functional.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ int main() {
312312
std::vector<int> items {1, 2, 3, 4, 5};
313313
auto odd_sq = items | std::views::transform([](int i){return i*i;})
314314
| std::views::filter([](int i){return i%2==1;});
315-
sum_sq_odds = std::accumulate(std::begin(odd_sq), std::end(odd_sq), 0, [](int a, int b){return a + b;})
316-
std::cout << result << std::endl;
315+
auto sum_sq_odds = std::accumulate(std::begin(odd_sq), std::end(odd_sq), 0, [](int a, int b){return a + b;})
316+
std::cout << sum_sq_odds << std::endl;
317317
return 0;
318318
}
319319
```
@@ -322,6 +322,23 @@ Not that C++ is slowly gaining support; `std::fold_left` is in C++23, but for
322322
C++20, we have to drop back to a classic `std::accumulate` algorithm & begin
323323
and end iterators.
324324
325+
````
326+
````{tab-item} C++23 Ranges
327+
```cpp
328+
import std;
329+
330+
int main() {
331+
std::vector items {1, 2, 3, 4, 5};
332+
auto odd_sq = items | std::views::transform([](int i){return i*i;})
333+
| std::views::filter([](int i){return i%2==1;});
334+
auto sum_sq_odds = std::ranges::fold_left(odd_sq, 0, [](int a, int b){return a + b;});
335+
std::println("{}", sum_sq_odds);
336+
return 0;
337+
}
338+
```
339+
340+
Not that stdlib module support is not available yet.
341+
325342
````
326343
````{tab-item} Rust
327344
```rust
@@ -335,6 +352,16 @@ fn main() {
335352
}
336353
```
337354
````
355+
356+
````{tab-item} Swift
357+
```swift
358+
let items = [1, 2, 3, 4, 5]
359+
let sum_sq_odds = items.map { $0 * $0 }
360+
.filter { $0 % 2 == 1 }
361+
.reduce(0, +)
362+
print(sum_sq_odds)
363+
```
364+
````
338365
`````
339366

340367
Notice how many of the languages use similar terms and try to read well when
@@ -516,3 +543,7 @@ Numba library, which is imperative. PyTorch is a great ML-focused library. CuPy
516543
is a great GPU NumPy replacement. Etc. JAX here is just intended to be an
517544
example of what thinking in a functional mindset can do.
518545
```
546+
547+
```
548+
549+
```

content/week08/typing.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ If a file starts with:
176176
from __future__ import annotations
177177
```
178178
Then all type annotations in the file will be unevaluated strings. This means
179-
you can use Python 3.13 syntax in them, and even Python 3.7 will happily work!
179+
you can use Python 3.13 syntax in them, and any Python 3.7+ will happily work!
180180
This is great, as every version up to 3.10 has had large improvements for
181181
typing. We will use the new syntax exclusively; add the above import to follow
182182
along on older Python versions.
@@ -249,7 +249,7 @@ general, you should be more strict with typing than with runtime.
249249
Type annotations can appear in any of the following locations:
250250

251251
```python
252-
# Variable annotation (3.6+)
252+
# Variable annotation
253253
x: int = 2
254254

255255
# No assignment is allowed ("declaration")
@@ -965,7 +965,7 @@ even if `from __future__ import annotations` is used, since it's new syntax and
965965
not an annotation.
966966

967967
`````{tab-set}
968-
````{tab-item} Python 3.6+
968+
````{tab-item} Python <3.12
969969
```python
970970
from typing import TypeVar
971971
@@ -976,7 +976,7 @@ def f(x: T) -> T:
976976
return x
977977
```
978978
````
979-
````{tab-item} Python 3.12+
979+
````{tab-item} Python >=3.12
980980
```python
981981
def f[T](x: T) -> T:
982982
return x

slides/week-07-1.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,17 +289,17 @@ fn main() {
289289

290290
---
291291

292-
## Other languages: C++23 (not supported yet)
292+
## Other languages: C++23 (modules not supported yet)
293293

294294
```cpp
295295
import std;
296296

297297
int main() {
298298
std::vector items {1, 2, 3, 4, 5};
299-
auto result = items | std::views::transform([](int i){return i*i;})
299+
auto odd_sq = items | std::views::transform([](int i){return i*i;})
300300
| std::views::filter([](int i){return i%2==1;});
301-
sum_sq_odds = std::fold_left(odd_sq, 0, [](int a, int b){return a + b;});
302-
std::println("{}", result);
301+
auto sum_sq_odds = std::fold_left(odd_sq, 0, [](int a, int b){return a + b;});
302+
std::println("{}", sum_sq_odds);
303303
return 0;
304304
}
305305
```

0 commit comments

Comments
 (0)