~jojo/Carth

ref: 749208029494a7c48ce04444a513424f4b998416 Carth/std/maybe.carth -rw-r--r-- 607 bytes
74920802JoJo update TODO 6 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))