aboutsummaryrefslogtreecommitdiffstats
path: root/stdlib/maybe.lisp
blob: 086b0dcecbc99658f6ba5a50998b10bef54f6a13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
; LIB

(let! Maybe/just (\[a]
    ["Maybe" "just" a]))
(let! Maybe/nothing (\[]
    ["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/map (\[f m]
    (match (Maybe/kind m)
        "just" (Maybe/just (f (Maybe/unpack m)))
        "nothing" (Maybe/nothing))))

(let! Maybe/and-then (\[f m]
    (match (Maybe/kind m)
        "just" (f (Maybe/unpack m))
        "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/unpack m)]))
;;     (Maybe/nothing)
;;         (print-line! "found nothing!"))