aboutsummaryrefslogtreecommitdiffstats
path: root/examples/maybe.lisp
blob: 7cabfeaece2147e5b2f43f072880fccdd624d7a9 (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
52
53
54
55
56
57
58
59
60
; LIB

(let! just (\[a]
    ["maybe" "just" a]))
(let! nothing (\[]
    ["maybe" "nothing"]))

(let! unsafe-at (\[n seq]
    (match seq
        []
            (fatal! "unsafe-at out of bounds")
        otherwise
            (match n
                0
                    (head seq)
                otherwise
                    (unsafe-at (- n 1) (tail seq))))))

(let! unpack-just (unsafe-at 2))
(let! kind (unsafe-at 1))

(let! map (\[f m]
    (match (kind m)
        "just" (just (f (unpack-just m)))
        "nothing" (nothing))))

(let! and-then (\[f m]
    (match (kind m)
        "just" (f (unpack-just m))
        "nothing" (nothing))))

; TESTING

;; (let! print-line! (\[s]
;;     (print! (concat s "\n"))))

;; (let! a (just 10))
;; (let! b (nothing))

;; (map (+ 5) a)
;; (map (+ 5) b)

;; (and-then (\[n] (just (+ n 5))) a)
;; (and-then (\[n] (just (+ n 5))) b)

;; (let! m (just 10))
;; (match m
;;     (just _)
;;         (print-line! (fmt "found just {0}!" [(unpack-just m)]))
;;     (nothing)
;;         (print-line! "found nothing!"))

(let! exports [
    just
    nothing
    unpack-just
    kind
    map
    and-then
])