File tree Expand file tree Collapse file tree 2 files changed +60
-0
lines changed Expand file tree Collapse file tree 2 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 1+ .rebar3
2+ _build
3+ _checkouts
Original file line number Diff line number Diff line change 1+ {- Basic result type.
2+ -}
3+ module result
4+
5+ import_type option.option
6+
7+ export_type result
8+
9+ type result 'a 'b = Ok 'a | Err 'b
10+
11+ let is_ok r =
12+ match r with
13+ Ok _ -> true
14+ | Err _ -> false
15+
16+ let is_ok r =
17+ match r with
18+ Ok _ -> false
19+ | Err _ -> true
20+
21+ test "Applying `is_ok` to Ok x should return true" =
22+ assert.equal (true) (is_ok (Ok 1))
23+
24+ let map f (Ok x) = Ok (f x)
25+
26+ let map _ (Err x) = Err x
27+
28+ test "mapping the identity function to Ok x should return Ok x" =
29+ let f x = x in
30+ assert.equal (Ok 1) (map f (Ok 1))
31+
32+ let of_option opt e =
33+ match opt with
34+ Some x -> Ok x
35+ | None -> Err e
36+
37+ let andThen callback (Ok value) = callback value
38+ let andThen _ (Err msg) = Err msg
39+
40+ test "andThen test with Ok x" =
41+ let f x = (Ok x) in
42+ assert.equal (Ok 1) (andThen f (Ok 1))
43+
44+ test "andThen with Err x and identity function is Err x" =
45+ let f x = (Ok x) in
46+ assert.equal (Err "msg") (andThen f (Err "msg"))
47+
48+ let withDefault _ (Ok x) = x
49+ let withDefault default (Err _) = default
50+
51+ test "withdefault test with Ok result" =
52+ let f x = (Ok x) in
53+ assert.equal (1) (withDefault 0 (f 1))
54+
55+ test "withdefault test with Err returning default" =
56+ let f _ = (Err "error") in
57+ assert.equal (0) (withDefault 0 (f 1))
You can’t perform that action at this time.
0 commit comments