summaryrefslogtreecommitdiff
path: root/topics/programming/maybe-monad.gmi
diff options
context:
space:
mode:
authorPjotr Prins2023-12-03 09:47:38 -0600
committerPjotr Prins2023-12-03 09:47:38 -0600
commitbaeafc5ccc4a9893d22e6629db97720e3fa6d3ae (patch)
tree3701e477b4643893fbb33495ef64a758bba03de9 /topics/programming/maybe-monad.gmi
parentaa3d310aa257f0ef0a8636272883c3c4e6855a1c (diff)
downloadgn-gemtext-baeafc5ccc4a9893d22e6629db97720e3fa6d3ae.tar.gz
Rename/move
Diffstat (limited to 'topics/programming/maybe-monad.gmi')
-rw-r--r--topics/programming/maybe-monad.gmi61
1 files changed, 61 insertions, 0 deletions
diff --git a/topics/programming/maybe-monad.gmi b/topics/programming/maybe-monad.gmi
new file mode 100644
index 0000000..d6f87f2
--- /dev/null
+++ b/topics/programming/maybe-monad.gmi
@@ -0,0 +1,61 @@
+# Maybe monad
+
+None values are values that represent the absence of a value. This leads to a proliferation of conditionals and special cases in the code, and is a terrible way to represent the absence of a value. We need something better. Enter the maybe monad.
+
+For a detailed case against None values, read
+=> https://www.lucidchart.com/techblog/2015/08/31/the-worst-mistake-of-computer-science/
+
+Consider the following code snippet where we print a value if it is not None.
+```
+def print_unless_none(x):
+ if x is not None:
+ print(x)
+
+foo = 1
+bar = None
+print_unless_none(foo)
+print_unless_none(bar)
+```
+
+Rewriting the same code using the maybe monad, we can avoid the conditional check making the code more concise and more robust against bugs.
+```
+from pymonad.maybe import Just, Nothing
+
+foo = Just(1)
+bar = Nothing
+foo.bind(print)
+bar.bind(print)
+```
+
+Monads may also be passed through a chain of function calls without any condition checking in between. If foo were Nothing, the entire sequence of operations would be skipped with no error raised. Notice how this is much cleaner than interleaving the code with if conditions checking for None intermediate values.
+```
+foo = Just(1)
+foo.map(lambda x: 1 + x) \
+ .map(lambda x: x**2) \
+ .bind(print)
+```
+
+Finally, let's put all this together in a practical example using sql_query_mdict from genenetwork. Consider the following code using the DictCursor. The column foo may contain NULL values, and we need to check for them.
+```
+with database_connection() as conn:
+ with conn.cursor(MySQLdb.cursors.DictCursor) as cursor:
+ cursor.execute("SELECT foo FROM bar")
+ for row in cursor.fetchall():
+ if row["foo"] is not None:
+ print(row["foo"])
+```
+But, with sql_query_mdict, the row object is a MonadictDict where all values are monadic. We therefore do not need any special conditional checks.
+```
+with database_connection() as conn:
+ for row in sql_query_mdict(conn, "SELECT foo FROM bar"):
+ row["foo"].bind(print)
+```
+As a bonus, sql_query_mdict also gets rid of cursors by returning a generator and letting us iterate over it pythonically.
+
+## Useful Resources
+
+=> https://www.miguelfarrajota.com/2021/06/monads-in-python-with-pymonad/
+
+=> https://jasondelaat.github.io/pymonad_docs/explanations/whats-a-monad.html
+
+=> https://simon.tournier.info/posts/2021-02-03-monad.html