Skip to content

Commit

Permalink
Clean up Result and Option modules
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh authored and zth committed Jul 27, 2023
1 parent e776049 commit 96a3366
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 65 deletions.
15 changes: 5 additions & 10 deletions src/Core__Option.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import * as Curry from "rescript/lib/es6/curry.js";
import * as Caml_option from "rescript/lib/es6/caml_option.js";

function filter(opt, p) {
var p$1 = Curry.__1(p);
if (opt !== undefined && p$1(Caml_option.valFromOption(opt))) {
if (opt !== undefined && Curry._1(p, Caml_option.valFromOption(opt))) {
return opt;
}

}

function forEach(opt, f) {
var f$1 = Curry.__1(f);
if (opt !== undefined) {
return f$1(Caml_option.valFromOption(opt));
return Curry._1(f, Caml_option.valFromOption(opt));
}

}
Expand All @@ -30,26 +28,23 @@ function getExn(x) {
}

function mapOr(opt, $$default, f) {
var f$1 = Curry.__1(f);
if (opt !== undefined) {
return f$1(Caml_option.valFromOption(opt));
return Curry._1(f, Caml_option.valFromOption(opt));
} else {
return $$default;
}
}

function map(opt, f) {
var f$1 = Curry.__1(f);
if (opt !== undefined) {
return Caml_option.some(f$1(Caml_option.valFromOption(opt)));
return Caml_option.some(Curry._1(f, Caml_option.valFromOption(opt)));
}

}

function flatMap(opt, f) {
var f$1 = Curry.__1(f);
if (opt !== undefined) {
return f$1(Caml_option.valFromOption(opt));
return Curry._1(f, Caml_option.valFromOption(opt));
}

}
Expand Down
30 changes: 10 additions & 20 deletions src/Core__Option.res
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,18 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

let filterU = (opt, p) =>
let filter = (opt, p) =>
switch opt {
| Some(x) as some if p(. x) => some
| Some(x) as option if p(x) => option
| _ => None
}

let filter = (opt, p) => filterU(opt, (. x) => p(x))

let forEachU = (opt, f) =>
let forEach = (opt, f) =>
switch opt {
| Some(x) => f(. x)
| Some(x) => f(x)
| None => ()
}

let forEach = (opt, f) => forEachU(opt, (. x) => f(x))

let getExn = x =>
switch x {
| Some(x) => x
Expand All @@ -46,32 +42,26 @@ let getExn = x =>

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

let mapOrU = (opt, default, f) =>
let mapOr = (opt, default, f) =>
switch opt {
| Some(x) => f(. x)
| Some(x) => f(x)
| None => default
}

let mapOr = (opt, default, f) => mapOrU(opt, default, (. x) => f(x))

let mapWithDefault = mapOr

let mapU = (opt, f) =>
let map = (opt, f) =>
switch opt {
| Some(x) => Some(f(. x))
| Some(x) => Some(f(x))
| None => None
}

let map = (opt, f) => mapU(opt, (. x) => f(x))

let flatMapU = (opt, f) =>
let flatMap = (opt, f) =>
switch opt {
| Some(x) => f(. x)
| Some(x) => f(x)
| None => None
}

let flatMap = (opt, f) => flatMapU(opt, (. x) => f(x))

let getOr = (opt, default) =>
switch opt {
| Some(x) => x
Expand Down
19 changes: 5 additions & 14 deletions src/Core__Result.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,29 @@ function getExn(x) {
}

function mapOr(opt, $$default, f) {
var f$1 = Curry.__1(f);
if (opt.TAG === /* Ok */0) {
return f$1(opt._0);
return Curry._1(f, opt._0);
} else {
return $$default;
}
}

function map(opt, f) {
var f$1 = Curry.__1(f);
if (opt.TAG === /* Ok */0) {
return {
TAG: /* Ok */0,
_0: f$1(opt._0)
_0: Curry._1(f, opt._0)
};
} else {
return {
TAG: /* Error */1,
_0: opt._0
};
return opt;
}
}

function flatMap(opt, f) {
var f$1 = Curry.__1(f);
if (opt.TAG === /* Ok */0) {
return f$1(opt._0);
return Curry._1(f, opt._0);
} else {
return {
TAG: /* Error */1,
_0: opt._0
};
return opt;
}
}

Expand Down
30 changes: 9 additions & 21 deletions src/Core__Result.res
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,26 @@ let getExn = x =>
| Error(_) => raise(Not_found)
}

let mapOrU = (opt, default, f) =>
let mapOr = (opt, default, f) =>
switch opt {
| Ok(x) => f(. x)
| Ok(x) => f(x)
| Error(_) => default
}

let mapOr = (opt, default, f) => mapOrU(opt, default, (. x) => f(x))

let mapWithDefault = mapOr

let mapU = (opt, f) =>
let map = (opt, f) =>
switch opt {
| Ok(x) => Ok(f(. x))
| Error(y) => Error(y)
| Ok(x) => Ok(f(x))
| Error(_) as result => result
}

let map = (opt, f) => mapU(opt, (. x) => f(x))

let flatMapU = (opt, f) =>
let flatMap = (opt, f) =>
switch opt {
| Ok(x) => f(. x)
| Error(y) => Error(y)
| Ok(x) => f(x)
| Error(_) as result => result
}

let flatMap = (opt, f) => flatMapU(opt, (. x) => f(x))

let getOr = (opt, default) =>
switch opt {
| Ok(x) => x
Expand Down Expand Up @@ -98,14 +92,8 @@ let forEach = (r, f) =>
| Error(_) => ()
}

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

0 comments on commit 96a3366

Please sign in to comment.