From 96a33664d0fa40d0ee5f8a8c51d97a7244647e76 Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Tue, 25 Jul 2023 12:16:12 +0400 Subject: [PATCH] Clean up Result and Option modules --- src/Core__Option.mjs | 15 +++++---------- src/Core__Option.res | 30 ++++++++++-------------------- src/Core__Result.mjs | 19 +++++-------------- src/Core__Result.res | 30 +++++++++--------------------- 4 files changed, 29 insertions(+), 65 deletions(-) diff --git a/src/Core__Option.mjs b/src/Core__Option.mjs index 6503d8e3..a98a6572 100644 --- a/src/Core__Option.mjs +++ b/src/Core__Option.mjs @@ -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)); } } @@ -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)); } } diff --git a/src/Core__Option.res b/src/Core__Option.res index 9557b726..a799a0ea 100644 --- a/src/Core__Option.res +++ b/src/Core__Option.res @@ -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 @@ -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 diff --git a/src/Core__Result.mjs b/src/Core__Result.mjs index fe5c9ba3..5f7e0c28 100644 --- a/src/Core__Result.mjs +++ b/src/Core__Result.mjs @@ -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; } } diff --git a/src/Core__Result.res b/src/Core__Result.res index 357a77ef..375ceafa 100644 --- a/src/Core__Result.res +++ b/src/Core__Result.res @@ -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 @@ -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)) }