File tree Expand file tree Collapse file tree 3 files changed +45
-2
lines changed
Expand file tree Collapse file tree 3 files changed +45
-2
lines changed Original file line number Diff line number Diff line change @@ -47,10 +47,19 @@ let (>>=) x f : [Monad m] -> m a -> (a -> m b) -> m b = flat_map f x
4747
4848let join mm : [Monad m] -> m (m a) -> m a = mm >>= (\x -> x)
4949
50+ // Kleisli composition
51+ #[infix(right, 9)]
52+ let (<=<) g f x : [Monad m] -> (b -> m c) -> (a -> m b) -> a -> m c = g =<< f x
53+
54+ #[infix(left, 9)]
55+ let (>=>) f g x : [Monad m] -> (a -> m b) -> (b -> m c) -> a -> m c = f x >>= g
56+
5057{
5158 Monad,
5259 flat_map,
5360 (>>=),
5461 (=<<),
5562 join,
63+ (<=<),
64+ (>=>),
5665}
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ let { Applicative } = import! std.applicative
1212let { Alternative } = import! std.alternative
1313let { Show } = import! std.show
1414let { Bool } = import! std.bool
15- let { Option } = import! std.types
15+ let { Option, Result } = import! std.types
1616let string @ { ? } = import! std.string
1717let { Foldable } = import! std.foldable
1818let { (<>) } = import! std.semigroup
@@ -23,6 +23,16 @@ let unwrap opt : Option a -> a =
2323 | Some x -> x
2424 | None -> error "Option was None"
2525
26+ let unwrap_or default opt : a -> Option a -> a =
27+ match opt with
28+ | Some x -> x
29+ | None -> default
30+
31+ let to_result err opt : e -> Option a -> Result e a =
32+ match opt with
33+ | Some x -> Ok x
34+ | None -> Err err
35+
2636let former =
2737 let semigroup : Semigroup (Option a) = {
2838 append = \l r ->
@@ -148,7 +158,11 @@ let traversable : Traversable Option = {
148158
149159{
150160 Option,
161+
151162 unwrap,
163+ unwrap_or,
164+ to_result,
165+
152166 semigroup,
153167 monoid,
154168 former,
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ let { Show } = import! std.show
66let { Functor } = import! std.functor
77let { Applicative } = import! std.applicative
88let { Monad } = import! std.monad
9- let { Result } = import! std.types
9+ let { Result, Option } = import! std.types
1010let { Foldable } = import! std.foldable
1111let { Traversable } = import! std.traversable
1212let { Bool } = import! std.bool
@@ -20,16 +20,31 @@ let unwrap_ok res : Result e a -> a =
2020 | Ok x -> x
2121 | Err _ -> error "Result was an Err"
2222
23+ let unwrap_ok_or default res : a -> Result e a -> a =
24+ match res with
25+ | Ok x -> x
26+ | Err _ -> default
27+
2328let unwrap_err res : Result e a -> e =
2429 match res with
2530 | Ok _ -> error "Result was an Ok"
2631 | Err x -> x
2732
33+ let unwrap_err_or default res : e -> Result e a -> e =
34+ match res with
35+ | Ok _ -> default
36+ | Err x -> x
37+
2838let map_err f res : (e -> f) -> Result e a -> Result f a =
2939 match res with
3040 | Ok a -> Ok a
3141 | Err e -> Err (f e)
3242
43+ let to_option res : Result e a -> Option a =
44+ match res with
45+ | Ok x -> Some x
46+ | Err _ -> None
47+
3348let eq : [Eq e] -> [Eq a] -> Eq (Result e a) = {
3449 (==) = \l r ->
3550 match (l, r) with
@@ -103,9 +118,14 @@ let show ?e ?t : [Show e] -> [Show t] -> Show (Result e t) =
103118
104119{
105120 Result,
121+
106122 unwrap_ok,
123+ unwrap_ok_or,
107124 unwrap_err,
125+ unwrap_err_or,
108126 map_err,
127+ to_option,
128+
109129 eq,
110130 ord,
111131 functor,
You can’t perform that action at this time.
0 commit comments