Spaces:
Running
Running
Commit
·
bfe46e3
1
Parent(s):
463e388
Fixing typo
Browse files
functional_programming/05_functors.py
CHANGED
@@ -4,6 +4,7 @@
|
|
4 |
# "marimo",
|
5 |
# ]
|
6 |
# ///
|
|
|
7 |
import marimo
|
8 |
|
9 |
__generated_with = "0.11.17"
|
@@ -36,7 +37,8 @@ def _(mo):
|
|
36 |
/// details | Notebook metadata
|
37 |
type: info
|
38 |
|
39 |
-
version: 0.1.0 | last modified: 2025-03-13 | author: [métaboulie](https://github.com/metaboulie)
|
|
|
40 |
|
41 |
///
|
42 |
"""
|
@@ -295,6 +297,7 @@ def _(mo):
|
|
295 |
- Each node has a **list of child nodes** (which are also RoseTrees).
|
296 |
|
297 |
This structure is useful for representing hierarchical data, such as:
|
|
|
298 |
- Abstract Syntax Trees (ASTs)
|
299 |
- File system directories
|
300 |
- Recursive computations
|
@@ -554,7 +557,7 @@ def _(Callable, Functor, Generic, a, b, dataclass):
|
|
554 |
return (EvilFunctor,)
|
555 |
|
556 |
|
557 |
-
@app.cell
|
558 |
def _(EvilFunctor):
|
559 |
EvilFunctor([1, 2, 3, 4]).check_functor_law()
|
560 |
return
|
@@ -564,9 +567,9 @@ def _(EvilFunctor):
|
|
564 |
def _(mo):
|
565 |
mo.md(
|
566 |
"""
|
567 |
-
## Final
|
568 |
|
569 |
-
We can now draft the final
|
570 |
|
571 |
```Python
|
572 |
@dataclass
|
@@ -1088,19 +1091,19 @@ def _(mo):
|
|
1088 |
@app.cell
|
1089 |
def _(Generic, a, dataclass):
|
1090 |
@dataclass
|
1091 |
-
class
|
1092 |
value: list[a]
|
1093 |
|
1094 |
@staticmethod
|
1095 |
-
def id() -> "
|
1096 |
-
return
|
1097 |
|
1098 |
@staticmethod
|
1099 |
def compose(
|
1100 |
-
this: "
|
1101 |
-
) -> "
|
1102 |
-
return
|
1103 |
-
return (
|
1104 |
|
1105 |
|
1106 |
@app.cell(hide_code=True)
|
@@ -1193,8 +1196,8 @@ def _(mo):
|
|
1193 |
|
1194 |
|
1195 |
@app.cell
|
1196 |
-
def _(IntAddition,
|
1197 |
-
length(
|
1198 |
return
|
1199 |
|
1200 |
|
@@ -1216,15 +1219,15 @@ def _(mo):
|
|
1216 |
|
1217 |
|
1218 |
@app.cell
|
1219 |
-
def _(
|
1220 |
-
lista =
|
1221 |
-
listb =
|
1222 |
return lista, listb
|
1223 |
|
1224 |
|
1225 |
@app.cell
|
1226 |
-
def _(IntAddition,
|
1227 |
-
length(
|
1228 |
length(lista), length(listb)
|
1229 |
)
|
1230 |
return
|
|
|
4 |
# "marimo",
|
5 |
# ]
|
6 |
# ///
|
7 |
+
|
8 |
import marimo
|
9 |
|
10 |
__generated_with = "0.11.17"
|
|
|
37 |
/// details | Notebook metadata
|
38 |
type: info
|
39 |
|
40 |
+
version: 0.1.0 | last modified: 2025-03-13 | author: [métaboulie](https://github.com/metaboulie)<br/>
|
41 |
+
reviewer: [Haleshot](https://github.com/Haleshot)
|
42 |
|
43 |
///
|
44 |
"""
|
|
|
297 |
- Each node has a **list of child nodes** (which are also RoseTrees).
|
298 |
|
299 |
This structure is useful for representing hierarchical data, such as:
|
300 |
+
|
301 |
- Abstract Syntax Trees (ASTs)
|
302 |
- File system directories
|
303 |
- Recursive computations
|
|
|
557 |
return (EvilFunctor,)
|
558 |
|
559 |
|
560 |
+
@app.cell
|
561 |
def _(EvilFunctor):
|
562 |
EvilFunctor([1, 2, 3, 4]).check_functor_law()
|
563 |
return
|
|
|
567 |
def _(mo):
|
568 |
mo.md(
|
569 |
"""
|
570 |
+
## Final definition of Functor
|
571 |
|
572 |
+
We can now draft the final definition of `Functor` with some utility functions.
|
573 |
|
574 |
```Python
|
575 |
@dataclass
|
|
|
1091 |
@app.cell
|
1092 |
def _(Generic, a, dataclass):
|
1093 |
@dataclass
|
1094 |
+
class ListConcatenation(Generic[a]):
|
1095 |
value: list[a]
|
1096 |
|
1097 |
@staticmethod
|
1098 |
+
def id() -> "ListConcatenation[a]":
|
1099 |
+
return ListConcatenation([])
|
1100 |
|
1101 |
@staticmethod
|
1102 |
def compose(
|
1103 |
+
this: "ListConcatenation[a]", other: "ListConcatenation[a]"
|
1104 |
+
) -> "ListConcatenation[a]":
|
1105 |
+
return ListConcatenation(this.value + other.value)
|
1106 |
+
return (ListConcatenation,)
|
1107 |
|
1108 |
|
1109 |
@app.cell(hide_code=True)
|
|
|
1196 |
|
1197 |
|
1198 |
@app.cell
|
1199 |
+
def _(IntAddition, ListConcatenation, length):
|
1200 |
+
length(ListConcatenation.id()) == IntAddition.id()
|
1201 |
return
|
1202 |
|
1203 |
|
|
|
1219 |
|
1220 |
|
1221 |
@app.cell
|
1222 |
+
def _(ListConcatenation):
|
1223 |
+
lista = ListConcatenation([1, 2])
|
1224 |
+
listb = ListConcatenation([3, 4])
|
1225 |
return lista, listb
|
1226 |
|
1227 |
|
1228 |
@app.cell
|
1229 |
+
def _(IntAddition, ListConcatenation, length, lista, listb):
|
1230 |
+
length(ListConcatenation.compose(lista, listb)) == IntAddition.compose(
|
1231 |
length(lista), length(listb)
|
1232 |
)
|
1233 |
return
|