Skip to content

Commit 775ebaf

Browse files
bors[bot]Etherian
andauthored
Merge #862
862: Add a few QoL functions to the standard library r=Marwes a=Etherian Adds - `unwrap_or` and `to_result` to `Option` - `unwrap_ok_or`, `unwrap_err_or`, and `to_option` to `Result` - Kleisli composition operators (`<=<` and `>=>`) Co-authored-by: Etherian <[email protected]>
2 parents 7eae498 + df87ac3 commit 775ebaf

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

std/monad.glu

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,19 @@ let (>>=) x f : [Monad m] -> m a -> (a -> m b) -> m b = flat_map f x
4747

4848
let 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
}

std/option.glu

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let { Applicative } = import! std.applicative
1212
let { Alternative } = import! std.alternative
1313
let { Show } = import! std.show
1414
let { Bool } = import! std.bool
15-
let { Option } = import! std.types
15+
let { Option, Result } = import! std.types
1616
let string @ { ? } = import! std.string
1717
let { Foldable } = import! std.foldable
1818
let { (<>) } = 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+
2636
let 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,

std/result.glu

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let { Show } = import! std.show
66
let { Functor } = import! std.functor
77
let { Applicative } = import! std.applicative
88
let { Monad } = import! std.monad
9-
let { Result } = import! std.types
9+
let { Result, Option } = import! std.types
1010
let { Foldable } = import! std.foldable
1111
let { Traversable } = import! std.traversable
1212
let { 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+
2328
let 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+
2838
let 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+
3348
let 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,

0 commit comments

Comments
 (0)