Skip to content

Commit 96a3366

Browse files
DZakhzth
authored andcommitted
Clean up Result and Option modules
1 parent e776049 commit 96a3366

File tree

4 files changed

+29
-65
lines changed

4 files changed

+29
-65
lines changed

src/Core__Option.mjs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ import * as Curry from "rescript/lib/es6/curry.js";
44
import * as Caml_option from "rescript/lib/es6/caml_option.js";
55

66
function filter(opt, p) {
7-
var p$1 = Curry.__1(p);
8-
if (opt !== undefined && p$1(Caml_option.valFromOption(opt))) {
7+
if (opt !== undefined && Curry._1(p, Caml_option.valFromOption(opt))) {
98
return opt;
109
}
1110

1211
}
1312

1413
function forEach(opt, f) {
15-
var f$1 = Curry.__1(f);
1614
if (opt !== undefined) {
17-
return f$1(Caml_option.valFromOption(opt));
15+
return Curry._1(f, Caml_option.valFromOption(opt));
1816
}
1917

2018
}
@@ -30,26 +28,23 @@ function getExn(x) {
3028
}
3129

3230
function mapOr(opt, $$default, f) {
33-
var f$1 = Curry.__1(f);
3431
if (opt !== undefined) {
35-
return f$1(Caml_option.valFromOption(opt));
32+
return Curry._1(f, Caml_option.valFromOption(opt));
3633
} else {
3734
return $$default;
3835
}
3936
}
4037

4138
function map(opt, f) {
42-
var f$1 = Curry.__1(f);
4339
if (opt !== undefined) {
44-
return Caml_option.some(f$1(Caml_option.valFromOption(opt)));
40+
return Caml_option.some(Curry._1(f, Caml_option.valFromOption(opt)));
4541
}
4642

4743
}
4844

4945
function flatMap(opt, f) {
50-
var f$1 = Curry.__1(f);
5146
if (opt !== undefined) {
52-
return f$1(Caml_option.valFromOption(opt));
47+
return Curry._1(f, Caml_option.valFromOption(opt));
5348
}
5449

5550
}

src/Core__Option.res

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,18 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
2424

25-
let filterU = (opt, p) =>
25+
let filter = (opt, p) =>
2626
switch opt {
27-
| Some(x) as some if p(. x) => some
27+
| Some(x) as option if p(x) => option
2828
| _ => None
2929
}
3030

31-
let filter = (opt, p) => filterU(opt, (. x) => p(x))
32-
33-
let forEachU = (opt, f) =>
31+
let forEach = (opt, f) =>
3432
switch opt {
35-
| Some(x) => f(. x)
33+
| Some(x) => f(x)
3634
| None => ()
3735
}
3836

39-
let forEach = (opt, f) => forEachU(opt, (. x) => f(x))
40-
4137
let getExn = x =>
4238
switch x {
4339
| Some(x) => x
@@ -46,32 +42,26 @@ let getExn = x =>
4642

4743
external getUnsafe: option<'a> => 'a = "%identity"
4844

49-
let mapOrU = (opt, default, f) =>
45+
let mapOr = (opt, default, f) =>
5046
switch opt {
51-
| Some(x) => f(. x)
47+
| Some(x) => f(x)
5248
| None => default
5349
}
5450

55-
let mapOr = (opt, default, f) => mapOrU(opt, default, (. x) => f(x))
56-
5751
let mapWithDefault = mapOr
5852

59-
let mapU = (opt, f) =>
53+
let map = (opt, f) =>
6054
switch opt {
61-
| Some(x) => Some(f(. x))
55+
| Some(x) => Some(f(x))
6256
| None => None
6357
}
6458

65-
let map = (opt, f) => mapU(opt, (. x) => f(x))
66-
67-
let flatMapU = (opt, f) =>
59+
let flatMap = (opt, f) =>
6860
switch opt {
69-
| Some(x) => f(. x)
61+
| Some(x) => f(x)
7062
| None => None
7163
}
7264

73-
let flatMap = (opt, f) => flatMapU(opt, (. x) => f(x))
74-
7565
let getOr = (opt, default) =>
7666
switch opt {
7767
| Some(x) => x

src/Core__Result.mjs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,29 @@ function getExn(x) {
1313
}
1414

1515
function mapOr(opt, $$default, f) {
16-
var f$1 = Curry.__1(f);
1716
if (opt.TAG === /* Ok */0) {
18-
return f$1(opt._0);
17+
return Curry._1(f, opt._0);
1918
} else {
2019
return $$default;
2120
}
2221
}
2322

2423
function map(opt, f) {
25-
var f$1 = Curry.__1(f);
2624
if (opt.TAG === /* Ok */0) {
2725
return {
2826
TAG: /* Ok */0,
29-
_0: f$1(opt._0)
27+
_0: Curry._1(f, opt._0)
3028
};
3129
} else {
32-
return {
33-
TAG: /* Error */1,
34-
_0: opt._0
35-
};
30+
return opt;
3631
}
3732
}
3833

3934
function flatMap(opt, f) {
40-
var f$1 = Curry.__1(f);
4135
if (opt.TAG === /* Ok */0) {
42-
return f$1(opt._0);
36+
return Curry._1(f, opt._0);
4337
} else {
44-
return {
45-
TAG: /* Error */1,
46-
_0: opt._0
47-
};
38+
return opt;
4839
}
4940
}
5041

src/Core__Result.res

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,26 @@ let getExn = x =>
3030
| Error(_) => raise(Not_found)
3131
}
3232

33-
let mapOrU = (opt, default, f) =>
33+
let mapOr = (opt, default, f) =>
3434
switch opt {
35-
| Ok(x) => f(. x)
35+
| Ok(x) => f(x)
3636
| Error(_) => default
3737
}
3838

39-
let mapOr = (opt, default, f) => mapOrU(opt, default, (. x) => f(x))
40-
4139
let mapWithDefault = mapOr
4240

43-
let mapU = (opt, f) =>
41+
let map = (opt, f) =>
4442
switch opt {
45-
| Ok(x) => Ok(f(. x))
46-
| Error(y) => Error(y)
43+
| Ok(x) => Ok(f(x))
44+
| Error(_) as result => result
4745
}
4846

49-
let map = (opt, f) => mapU(opt, (. x) => f(x))
50-
51-
let flatMapU = (opt, f) =>
47+
let flatMap = (opt, f) =>
5248
switch opt {
53-
| Ok(x) => f(. x)
54-
| Error(y) => Error(y)
49+
| Ok(x) => f(x)
50+
| Error(_) as result => result
5551
}
5652

57-
let flatMap = (opt, f) => flatMapU(opt, (. x) => f(x))
58-
5953
let getOr = (opt, default) =>
6054
switch opt {
6155
| Ok(x) => x
@@ -98,14 +92,8 @@ let forEach = (r, f) =>
9892
| Error(_) => ()
9993
}
10094

101-
// If the source result is Ok, should we return that instance, or
102-
// create it again? In this implementation I'm returning that specific
103-
// instance. However this is not consistent with the implementation for
104-
// other functions like mapU and flatMapU, which recreate the result.
105-
// This is more efficient. I'm not sure why the other implementations
106-
// return a new instance.
10795
let mapError = (r, f) =>
10896
switch r {
109-
| Ok(_) as ok => ok
97+
| Ok(_) as result => result
11098
| Error(e) => Error(f(e))
11199
}

0 commit comments

Comments
 (0)