aboutsummaryrefslogtreecommitdiffstats
path: root/stdlib/maybe-with-unions.lisp
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2022-12-05 11:03:32 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2022-12-05 14:21:53 +0200
commitac6b455f4a9be11d322551a927c4330532f9f184 (patch)
treec5dd421f4aeb8ca60eadabc5cc7edf03ae58e743 /stdlib/maybe-with-unions.lisp
parent9f4ca350ee81a874b0c838c2e82076efde6db8b2 (diff)
Add record, union concepts
Diffstat (limited to 'stdlib/maybe-with-unions.lisp')
-rw-r--r--stdlib/maybe-with-unions.lisp42
1 files changed, 42 insertions, 0 deletions
diff --git a/stdlib/maybe-with-unions.lisp b/stdlib/maybe-with-unions.lisp
new file mode 100644
index 0000000..53a5024
--- /dev/null
+++ b/stdlib/maybe-with-unions.lisp
@@ -0,0 +1,42 @@
+; LIB
+
+(union! Maybe
+ (just value)
+ (nothing))
+
+(let! Maybe/map (\[f m]
+ (match (kind m)
+ "Maybe/Just" (Maybe/just (f (Maybe/Just/get-value m)))
+ "Maybe/Nothing" (Maybe/nothing))))
+
+(let! Maybe/and-then (\[f m]
+ (match (kind m)
+ "Maybe/Just" (f (Maybe/Just/get-value m))
+ "Maybe/Nothing" (Maybe/nothing))))
+
+; TESTING
+
+;; (let! print-line! (\[s]
+;; (print! (concat s "\n"))))
+
+;; (let! a (Maybe/just 10))
+;; (let! b (Maybe/nothing))
+
+;; (Maybe/map (+ 5) a)
+;; (Maybe/map (+ 5) b)
+
+;; (Maybe/and-then (\[n] (Maybe/just (+ n 5))) a)
+;; (Maybe/and-then (\[n] (Maybe/just (+ n 5))) b)
+
+;; (let! m (Maybe/just 10))
+;; (match m
+;; (Maybe/just _)
+;; (print-line! (fmt "found just {0}!" [(Maybe/Just/get-value m)]))
+;; (Maybe/nothing)
+;; (print-line! "found nothing!"))
+
+;; (let! m (Maybe/just 10))
+;; (pipe m [
+;; (Maybe/map (+ 5))
+;; (Maybe/and-then (\[val] (Maybe/just (- val 3))))
+;; ])