Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repos:
- id: trailing-whitespace

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.9.3
rev: v0.11.12
hooks:
- id: ruff-format
exclude: ^content/week01/cleanup_bessel\.ipynb$
Expand All @@ -31,12 +31,12 @@ repos:
- id: nbstripout

- repo: https://github.com/rbubley/mirrors-prettier
rev: "v3.4.2"
rev: "v3.5.3"
hooks:
- id: prettier

- repo: https://github.com/codespell-project/codespell
rev: "v2.4.0"
rev: "v2.4.1"
hooks:
- id: codespell
args: ["-L", "hist,whet,classe,nd", "-w"]
Expand Down
23 changes: 11 additions & 12 deletions content/week01/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,37 +124,36 @@ longer have to look at the whole program, but just smaller, digestible parts.
Everyone learning a language will know what these common constructs are. No one
will know what your special constructs are.


#### Example: match

Let's quickly apply this to a much newer example. Take the following code that
provides different printouts for different structures:

```python
if isinstance(x, list):
print(*x)
print(*x)
elif isinstance(x, dict):
print(*(f"{k}={v}" for k, v in x.items()))
print(*(f"{k}={v}" for k, v in x.items()))
else:
print(x)
print(x)
```

versus using pattern matching:

```python
match x:
case [*_]:
print(*x)
case {}:
print(*(f"{k}={v}" for k, v in x.items()))
case _:
print(x)
case [*_]:
print(*x)
case {}:
print(*(f"{k}={v}" for k, v in x.items()))
case _:
print(x)
```

The pattern matching case is more focused, so it takes less reading. You know
from the first line `match x` that this is based on the variable `x`; in the if
chain, you could have switched to some other variable partway down the chain,
so the reader has to look at each branch to verify you are just processing one
chain, you could have switched to some other variable partway down the chain, so
the reader has to look at each branch to verify you are just processing one
variable. The language has built-in support for various situations, while the
manual form would require you write everything out. There's a learning curve to
the more specific structure, but anyone learning the language learns it once,
Expand Down
18 changes: 9 additions & 9 deletions slides/week-01-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,23 @@ for i in range(10):

```python
if isinstance(x, list):
print(*x)
print(*x)
elif isinstance(x, dict):
print(*(f"{k}={v}" for k, v in x.items()))
print(*(f"{k}={v}" for k, v in x.items()))
else:
print(x)
print(x)
```

vs.

```python
match x:
case [*_]:
print(*x)
case {}:
print(*(f"{k}={v}" for k, v in x.items()))
case _:
print(x)
case [*_]:
print(*x)
case {}:
print(*(f"{k}={v}" for k, v in x.items()))
case _:
print(x)
```

---
Expand Down
Loading