Spaces:
Running
Running
Commit
·
463e388
1
Parent(s):
53563a5
Improve notebook and README as suggested by Haleshot
Browse files
functional_programming/05_functors.py
CHANGED
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import marimo
|
2 |
|
3 |
__generated_with = "0.11.17"
|
@@ -723,19 +729,24 @@ def _(mo):
|
|
723 |
return
|
724 |
|
725 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
726 |
@app.cell(hide_code=True)
|
727 |
-
def _(
|
728 |
with mo.redirect_stdout():
|
729 |
-
mftree = Maybe(Just(ftree))
|
730 |
-
mint = Maybe(Just(1))
|
731 |
-
mnone = Maybe(None)
|
732 |
print(mftree.check_functor_law())
|
733 |
print(mint.check_functor_law())
|
734 |
print(mnone.check_functor_law())
|
735 |
print(mftree.fmap(inc))
|
736 |
print(mint.fmap(lambda x: x + 1))
|
737 |
print(mnone.fmap(lambda x: x + 1))
|
738 |
-
return
|
739 |
|
740 |
|
741 |
@app.cell(hide_code=True)
|
@@ -1205,15 +1216,18 @@ def _(mo):
|
|
1205 |
|
1206 |
|
1207 |
@app.cell
|
1208 |
-
def _(
|
1209 |
lista = ListConcatentation([1, 2])
|
1210 |
listb = ListConcatentation([3, 4])
|
|
|
1211 |
|
1212 |
|
|
|
|
|
1213 |
length(ListConcatentation.compose(lista, listb)) == IntAddition.compose(
|
1214 |
length(lista), length(listb)
|
1215 |
)
|
1216 |
-
return
|
1217 |
|
1218 |
|
1219 |
@app.cell(hide_code=True)
|
@@ -1248,9 +1262,7 @@ def _(mo):
|
|
1248 |
@app.cell(hide_code=True)
|
1249 |
def _():
|
1250 |
import marimo as mo
|
1251 |
-
|
1252 |
-
from marimo import md, mermaid
|
1253 |
-
return md, mermaid, mo
|
1254 |
|
1255 |
|
1256 |
@app.cell(hide_code=True)
|
@@ -1263,11 +1275,15 @@ def _():
|
|
1263 |
def _():
|
1264 |
from dataclasses import dataclass
|
1265 |
from typing import Callable, Generic, TypeVar
|
|
|
|
|
1266 |
|
|
|
|
|
1267 |
a = TypeVar("a")
|
1268 |
b = TypeVar("b")
|
1269 |
c = TypeVar("c")
|
1270 |
-
return
|
1271 |
|
1272 |
|
1273 |
if __name__ == "__main__":
|
|
|
1 |
+
# /// script
|
2 |
+
# requires-python = ">=3.9"
|
3 |
+
# dependencies = [
|
4 |
+
# "marimo",
|
5 |
+
# ]
|
6 |
+
# ///
|
7 |
import marimo
|
8 |
|
9 |
__generated_with = "0.11.17"
|
|
|
729 |
return
|
730 |
|
731 |
|
732 |
+
@app.cell
|
733 |
+
def _(Just, Maybe, ftree):
|
734 |
+
mftree = Maybe(Just(ftree))
|
735 |
+
mint = Maybe(Just(1))
|
736 |
+
mnone = Maybe(None)
|
737 |
+
return mftree, mint, mnone
|
738 |
+
|
739 |
+
|
740 |
@app.cell(hide_code=True)
|
741 |
+
def _(inc, mftree, mint, mnone, mo):
|
742 |
with mo.redirect_stdout():
|
|
|
|
|
|
|
743 |
print(mftree.check_functor_law())
|
744 |
print(mint.check_functor_law())
|
745 |
print(mnone.check_functor_law())
|
746 |
print(mftree.fmap(inc))
|
747 |
print(mint.fmap(lambda x: x + 1))
|
748 |
print(mnone.fmap(lambda x: x + 1))
|
749 |
+
return
|
750 |
|
751 |
|
752 |
@app.cell(hide_code=True)
|
|
|
1216 |
|
1217 |
|
1218 |
@app.cell
|
1219 |
+
def _(ListConcatentation):
|
1220 |
lista = ListConcatentation([1, 2])
|
1221 |
listb = ListConcatentation([3, 4])
|
1222 |
+
return lista, listb
|
1223 |
|
1224 |
|
1225 |
+
@app.cell
|
1226 |
+
def _(IntAddition, ListConcatentation, length, lista, listb):
|
1227 |
length(ListConcatentation.compose(lista, listb)) == IntAddition.compose(
|
1228 |
length(lista), length(listb)
|
1229 |
)
|
1230 |
+
return
|
1231 |
|
1232 |
|
1233 |
@app.cell(hide_code=True)
|
|
|
1262 |
@app.cell(hide_code=True)
|
1263 |
def _():
|
1264 |
import marimo as mo
|
1265 |
+
return (mo,)
|
|
|
|
|
1266 |
|
1267 |
|
1268 |
@app.cell(hide_code=True)
|
|
|
1275 |
def _():
|
1276 |
from dataclasses import dataclass
|
1277 |
from typing import Callable, Generic, TypeVar
|
1278 |
+
return Callable, Generic, TypeVar, dataclass
|
1279 |
+
|
1280 |
|
1281 |
+
@app.cell(hide_code=True)
|
1282 |
+
def _(TypeVar):
|
1283 |
a = TypeVar("a")
|
1284 |
b = TypeVar("b")
|
1285 |
c = TypeVar("c")
|
1286 |
+
return a, b, c
|
1287 |
|
1288 |
|
1289 |
if __name__ == "__main__":
|
functional_programming/README.md
CHANGED
@@ -1,24 +1,33 @@
|
|
1 |
-
# Functional Programming
|
2 |
|
3 |
-
_🚧 This collection is a
|
|
|
4 |
|
5 |
-
This series of marimo notebooks introduces the powerful paradigm of functional
|
|
|
|
|
|
|
6 |
|
7 |
## What You'll Learn
|
8 |
|
9 |
-
**Using only Python's standard library**, we'll construct functional programming
|
|
|
10 |
|
11 |
Topics include:
|
12 |
-
|
13 |
-
-
|
14 |
-
-
|
15 |
-
-
|
|
|
16 |
|
17 |
## Timeline & Collaboration
|
18 |
|
19 |
-
I'm currently studying functional programming and Haskell, estimating about 2
|
|
|
|
|
20 |
|
21 |
-
If you're interested in collaborating or have questions, please reach out to me
|
|
|
22 |
|
23 |
**Running notebooks.** To run a notebook locally, use
|
24 |
|
@@ -33,51 +42,20 @@ uvx marimo edit https://github.com/marimo-team/learn/blob/main/Functional_progra
|
|
33 |
```
|
34 |
|
35 |
You can also open notebooks in our online playground by appending `marimo.app/`
|
36 |
-
to a notebook's URL:
|
37 |
-
|
38 |
-
## Current series structure
|
39 |
-
|
40 |
-
|
41 |
-
| Notebook | Description | Status | Author |
|
42 |
-
|----------|-------------|--------|--------|
|
43 |
-
| Functional Programming Fundamentals | Core FP principles in Python, comparison with imperative programming, and Haskell-inspired thinking patterns | 🚧 | |
|
44 |
-
| Higher-Order Functions and Currying | Functions as first-class values, composition patterns, and implementing Haskell-style currying in Python | 🚧 | |
|
45 |
-
| Python's Functional Toolkit: functools, itertools and operator | Leveraging Python's built-in functional programming utilities, advanced iterator operations, and function transformations | 🚧 | |
|
46 |
-
| Recursion and Tail Recursion | Recursive problem solving, implementing tail-call optimization in Python, and trampoline techniques to avoid stack overflow | 🚧 | |
|
47 |
-
| Category Theory and Functors | Introduction to categories, morphisms, functor patterns, and implementing the functor interface and practical use cases | `0.1.0` | |
|
48 |
-
| Applicatives and Effectful Programming | Combining independent computations with effects, implementing the applicative interface and practical use cases | 🚧 | |
|
49 |
-
| Kleisli Category and Monads | Understanding monadic computation, composing impure functions, and implementing basic monads | 🚧 | |
|
50 |
-
| Monad Fail, Transformers and Fix | Error handling with MonadFail, combining monads with transformers, and handling recursive structures | 🚧 | |
|
51 |
-
| Monadic Parsing | Building a parser combinator library, implementing recursive descent parsers, and practical text processing | 🚧 | |
|
52 |
-
| Semigroups and Monoids | Composable operations, algebraic structures, laws, and practical applications for data aggregation | 🚧 | |
|
53 |
-
| Foldables and Traversables | Abstract folding beyond lists, traversing with effects, and implementing these interfaces for custom data types | 🚧 | |
|
54 |
-
| Bifunctors | Working with two-parameter type constructors, implementing the bifunctor interface, and practical examples | 🚧 | |
|
55 |
-
| Arrows | Arrow abstractions beyond monads, implementing the Arrow interface, and creating arrow-based computations | 🚧 | |
|
56 |
-
| Comonads | Understanding dual concepts to monads, implementing Store and Stream comonads, and practical applications | 🚧 | |
|
57 |
-
| Design Patterns in Functional Python | Applying FP concepts to solve real-world problems, functional architecture, and testing strategies | 🚧 | |
|
58 |
|
59 |
# Description of notebooks
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
In [this notebook](https://github.com/marimo-team/learn/blob/main/Functional_programming/05_functors.py), you would learn:
|
64 |
-
|
65 |
-
* Why `len` is the *Functor* from the category of `list concatentation` to the category of `integer addition`
|
66 |
-
* How to *lift* an ordinary function to a specific *computation context*
|
67 |
-
* How to write an *adpter* between two categories
|
68 |
-
|
69 |
-
### References
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
- [Haskellforall. The Category Design Pattern](https://www.haskellforall.com/2012/08/the-category-design-pattern.html)
|
74 |
-
- [Haskellforall. The Functor Design Pattern](https://www.haskellforall.com/2012/09/the-functor-design-pattern.html)
|
75 |
-
- [Haskellwiki. Functor](https://wiki.haskell.org/index.php?title=Functor)
|
76 |
-
- [Haskellwiki. Typeclassopedia#Functor](https://wiki.haskell.org/index.php?title=Typeclassopedia#Functor)
|
77 |
-
- [Haskellwiki. Typeclassopedia#Category](https://wiki.haskell.org/index.php?title=Typeclassopedia#Category)
|
78 |
|
79 |
**Authors.**
|
80 |
|
81 |
Thanks to all our notebook authors!
|
82 |
|
83 |
-
|
|
|
1 |
+
# Learn Functional Programming
|
2 |
|
3 |
+
_🚧 This collection is a
|
4 |
+
[work in progress](https://github.com/marimo-team/learn/issues/51)._
|
5 |
|
6 |
+
This series of marimo notebooks introduces the powerful paradigm of functional
|
7 |
+
programming through Python. Taking inspiration from Haskell and Category Theory,
|
8 |
+
we'll build a strong foundation in FP concepts that can transform how you
|
9 |
+
approach software development.
|
10 |
|
11 |
## What You'll Learn
|
12 |
|
13 |
+
**Using only Python's standard library**, we'll construct functional programming
|
14 |
+
concepts from first principles.
|
15 |
|
16 |
Topics include:
|
17 |
+
|
18 |
+
- Recursion and higher-order functions
|
19 |
+
- Category theory fundamentals
|
20 |
+
- Functors, applicatives, and monads
|
21 |
+
- Composable abstractions for robust code
|
22 |
|
23 |
## Timeline & Collaboration
|
24 |
|
25 |
+
I'm currently studying functional programming and Haskell, estimating about 2
|
26 |
+
months or even longer to complete this series. The structure may evolve as the
|
27 |
+
project develops.
|
28 |
|
29 |
+
If you're interested in collaborating or have questions, please reach out to me
|
30 |
+
on Discord (@eugene.hs).
|
31 |
|
32 |
**Running notebooks.** To run a notebook locally, use
|
33 |
|
|
|
42 |
```
|
43 |
|
44 |
You can also open notebooks in our online playground by appending `marimo.app/`
|
45 |
+
to a notebook's URL:
|
46 |
+
[marimo.app/github.com/marimo-team/learn/blob/main/functional_programming/05_functors.py](https://marimo.app/https://github.com/marimo-team/learn/blob/main/functional_programming/05_functors.py).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
# Description of notebooks
|
49 |
|
50 |
+
Check [here](https://github.com/marimo-team/learn/issues/51) for current series
|
51 |
+
structure.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
| Notebook | Description | References |
|
54 |
+
| ----------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
55 |
+
| [05. Category and Functors](https://github.com/marimo-team/learn/blob/main/Functional_programming/05_functors.py) | Learn why `len` is a _Functor_ from `list concatenation` to `integer addition`, how to _lift_ an ordinary function into a _computation context_, and how to write an _adapter_ between two categories. | - [The Trivial Monad](http://blog.sigfpe.com/2007/04/trivial-monad.html) <br> - [Haskellwiki. Category Theory](https://en.wikibooks.org/wiki/Haskell/Category_theory) <br> - [Haskellforall. The Category Design Pattern](https://www.haskellforall.com/2012/08/the-category-design-pattern.html) <br> - [Haskellforall. The Functor Design Pattern](https://www.haskellforall.com/2012/09/the-functor-design-pattern.html) <br> - [Haskellwiki. Functor](https://wiki.haskell.org/index.php?title=Functor) <br> - [Haskellwiki. Typeclassopedia#Functor](https://wiki.haskell.org/index.php?title=Typeclassopedia#Functor) <br> - [Haskellwiki. Typeclassopedia#Category](https://wiki.haskell.org/index.php?title=Typeclassopedia#Category) |
|
|
|
|
|
|
|
|
|
56 |
|
57 |
**Authors.**
|
58 |
|
59 |
Thanks to all our notebook authors!
|
60 |
|
61 |
+
- [métaboulie](https://github.com/metaboulie)
|