~jojo/Carth

ref: 55fb4f948f1f3797078b584dc60b4f7dd68b37ed Carth/std/maybe.carth -rw-r--r-- 607 bytes
55fb4f94JoJo Check `cast` in Infer instead of Gen 4 months ago
                                                                                
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
(import std)

(data (Maybe a)
  None
  (Some a))

(define (maybe b f)
  (fmatch
    (case (Some a) (f a))
    (case None     b)))

(define unwrap!
  (fmatch (case (Some x) x)
          (case None (panic "unwrap! of None"))))

(define (maybe/map f)
  (fmatch (case (Some x) (Some (f x)))
          (case None None)))

(define (maybe/map2 f) (maybe/lift2 (<oo Some f)))

(define (maybe/lift2 f ma mb)
  (maybe/bindr ma (fun (a) (maybe/bindr mb (f a)))))

(define (maybe/bind f)
  (fmatch
    (case (Some a) (f a))
    (case None None)))

(define maybe/bindl maybe/bind)
(define maybe/bindr (flip maybe/bind))