forked from carp-lang/Carp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maybe.carp
121 lines (117 loc) · 3.28 KB
/
maybe.carp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
(load "Test.carp")
(use-all Maybe Test)
(deftest test
(assert-true test
(nothing? &(the (Maybe Int) (Nothing)))
"nothing? works on Nothing"
)
(assert-false test
(nothing? &(Just 1))
"nothing? works on Just"
)
(assert-true test
(just? &(Just 1))
"just? works on Just"
)
(assert-false test
(just? &(the (Maybe Int) (Nothing)))
"just? works on Nothing"
)
(assert-equal test
0
(get-tag &(Just 1))
"tag works on Just"
)
(assert-equal test
1
(get-tag &(the (Maybe Int) (Nothing)))
"tag works on Nothing"
)
(assert-equal test
1
(from (Just 1) 0)
"from works on Just"
)
(assert-equal test
0
(from (the (Maybe Int) (Nothing)) 0)
"from works on Nothing"
)
(assert-equal test
1
(unsafe-from (Just 1))
"unsafe-from works on Just"
)
(assert-equal test
2
(from (apply (Just 1) &Int.inc) 0)
"apply works on Just"
)
(assert-equal test
0
(from (apply (the (Maybe Int) (Nothing)) &Int.inc) 0)
"apply works on Nothing"
)
(assert-equal test
10
(or-zero (Just 10))
"or-zero works on Just"
)
(assert-equal test
0
(or-zero (the (Maybe Int) (Nothing)))
"or-zero works on Nothing"
)
(assert-equal test
&(Nothing)
&(the (Maybe Int) (zero))
"zero works"
)
(assert-equal test
&(Result.Success 1)
&(to-result (Just 1) @"error")
"to-result works on Just"
)
(assert-equal test
&(the (Result Int String) (Result.Error @"error"))
&(to-result (Nothing) @"error")
"to-result works on Nothing"
)
(assert-equal test
1
@(Pointer.to-ref (unsafe-ptr &(Just 1)))
"unsafe-ptr works on Just"
)
(assert-true test
(null? (unsafe-ptr &(the (Maybe Int) (Nothing))))
"unsafe-ptr works on Nothing"
)
(assert-equal test
&(Just 0)
&(from-ptr (Pointer.address &(zero)))
"from-ptr works on Ptr/Just"
)
(assert-equal test
&(Nothing)
&(the (Maybe Int) (from-ptr NULL))
"from-ptr works on NULL/Nothing"
)
(assert-equal test
123
(match (the (Maybe (Maybe Int)) (Just (Just 123)))
Nothing 0
(Just (Nothing)) 0
(Just (Just x)) x)
"Matching on nested Maybes."
)
(assert-equal test
123
(match (the (Maybe (Maybe (Maybe Int))) (Just (Just (Nothing))))
Nothing 0
(Just (Nothing)) 0
(Just (Just (Just _))) 0
(Just (Just (Nothing))) 123
)
"Matching on nested Maybes."
)
)