From ac6b455f4a9be11d322551a927c4330532f9f184 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Mon, 5 Dec 2022 11:03:32 +0200 Subject: Add record, union concepts --- stdlib/maybe-with-unions.lisp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 stdlib/maybe-with-unions.lisp (limited to 'stdlib/maybe-with-unions.lisp') 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)))) +;; ]) -- cgit v1.3