diff options
Diffstat (limited to 'stdlib')
| -rw-r--r-- | stdlib/maybe-with-unions.milch | 42 | ||||
| -rw-r--r-- | stdlib/maybe.milch | 41 |
2 files changed, 17 insertions, 66 deletions
diff --git a/stdlib/maybe-with-unions.milch b/stdlib/maybe-with-unions.milch deleted file mode 100644 index 8a15471..0000000 --- a/stdlib/maybe-with-unions.milch +++ /dev/null @@ -1,42 +0,0 @@ -; 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)))) -;; ]) diff --git a/stdlib/maybe.milch b/stdlib/maybe.milch index 4114e22..ef9e906 100644 --- a/stdlib/maybe.milch +++ b/stdlib/maybe.milch @@ -1,33 +1,20 @@ ; LIB -(let Maybe/just (\[a] - ["Maybe" "just" a])) -(let Maybe/nothing (\[] - ["Maybe" "nothing"])) +(record Maybe/Just value) +(record Maybe/Nothing) -(let Maybe/#at (\[n seq] - (match seq - [] - (fatal! "Maybe/#at out of bounds") - otherwise - (match n - 0 - (head seq) - otherwise - (Maybe/#at (- n 1) (tail seq)))))) - -(let Maybe/unpack (Maybe/#at 2)) -(let Maybe/kind (Maybe/#at 1)) +(let Maybe/just Maybe/Just/create) +(let Maybe/nothing Maybe/Nothing/create) (let Maybe/map (\[f m] - (match (Maybe/kind m) - "just" (Maybe/just (f (Maybe/unpack m))) - "nothing" (Maybe/nothing)))) + (match (kind m) + "Maybe/Just" (Maybe/just (f (Maybe/Just/get-value m))) + "Maybe/Nothing" (Maybe/nothing)))) (let Maybe/and-then (\[f m] - (match (Maybe/kind m) - "just" (f (Maybe/unpack m)) - "nothing" (Maybe/nothing)))) + (match (kind m) + "Maybe/Just" (f (Maybe/Just/get-value m)) + "Maybe/Nothing" (Maybe/nothing)))) ; TESTING @@ -46,6 +33,12 @@ ;; (let m (Maybe/just 10)) ;; (match m ;; (Maybe/just _) -;; (print-line! (fmt "found just {0}!" [(Maybe/unpack m)])) +;; (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)))) +;; ]) |
