From e6ecf93dc216a7e9cd40c10e9544414a040b5fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnar=20=C3=9E=C3=B3r=20Sveinsson?= Date: Wed, 14 Mar 2018 00:19:28 +0000 Subject: [PATCH 1/2] Add option module to belt --- jscomp/others/Makefile | 1 + jscomp/others/belt.ml | 4 +++ jscomp/others/belt_Option.ml | 27 ++++++++++++++ jscomp/others/belt_Option.mli | 7 ++++ lib/js/belt.js | 3 ++ lib/js/belt_Option.js | 68 +++++++++++++++++++++++++++++++++++ 6 files changed, 110 insertions(+) create mode 100644 jscomp/others/belt_Option.ml create mode 100644 jscomp/others/belt_Option.mli create mode 100644 lib/js/belt_Option.js diff --git a/jscomp/others/Makefile b/jscomp/others/Makefile index ef23b3111b..735e29c608 100644 --- a/jscomp/others/Makefile +++ b/jscomp/others/Makefile @@ -34,6 +34,7 @@ SOURCE_LIST= node_path node_fs node_process dict node_module js_array js_string belt_internalMapString\ belt_MapString \ belt_MapInt\ + belt_Option\ belt_internalSet\ belt_Set\ belt_MutableSet\ diff --git a/jscomp/others/belt.ml b/jscomp/others/belt.ml index f5ec1e63e8..a86a994300 100644 --- a/jscomp/others/belt.ml +++ b/jscomp/others/belt.ml @@ -232,7 +232,11 @@ module HashSet = Belt_HashSet *) module HashMap = Belt_HashMap +(** {!Belt.Option} + Utilities for option data type +*) +module Option = Belt_Option diff --git a/jscomp/others/belt_Option.ml b/jscomp/others/belt_Option.ml new file mode 100644 index 0000000000..d49d94c6c5 --- /dev/null +++ b/jscomp/others/belt_Option.ml @@ -0,0 +1,27 @@ +let getExn = function + | Some x -> x + | None -> assert false + +let fold opt default f = match opt with + | Some x -> f x + | None -> default + +let map opt f = match opt with + | Some x -> Some (f x) + | None -> None + +let flatMap opt f = match opt with + | Some x -> f x + | None -> None + +let getOrElse opt default = match opt with + | Some x -> x + | None -> default + +let exists = function + | Some _ -> true + | None -> false + +let empty = function + | Some _ -> false + | None -> true diff --git a/jscomp/others/belt_Option.mli b/jscomp/others/belt_Option.mli new file mode 100644 index 0000000000..232be68ac9 --- /dev/null +++ b/jscomp/others/belt_Option.mli @@ -0,0 +1,7 @@ +val getExn : 'a option -> 'a +val fold : 'a option -> 'b -> ('a -> 'b) -> 'b +val map : 'a option -> ('a -> 'b) -> 'b option +val flatMap : 'a option -> ('a -> 'b option) -> 'b option +val getOrElse : 'a option -> 'a -> 'a +val exists : 'a option -> bool +val empty : 'a option -> bool diff --git a/lib/js/belt.js b/lib/js/belt.js index 50a1edc580..d4e233eb4c 100644 --- a/lib/js/belt.js +++ b/lib/js/belt.js @@ -27,6 +27,8 @@ var HashSet = 0; var HashMap = 0; +var Option = 0; + exports.Id = Id; exports.$$Array = $$Array; exports.SortArray = SortArray; @@ -40,4 +42,5 @@ exports.MutableSet = MutableSet; exports.MutableMap = MutableMap; exports.HashSet = HashSet; exports.HashMap = HashMap; +exports.Option = Option; /* No side effect */ diff --git a/lib/js/belt_Option.js b/lib/js/belt_Option.js new file mode 100644 index 0000000000..0632e0e4f2 --- /dev/null +++ b/lib/js/belt_Option.js @@ -0,0 +1,68 @@ +'use strict'; + +var Curry = require("./curry.js"); + +function getExn(param) { + if (param) { + return param[0]; + } else { + return /* assert false */0; + } +} + +function fold(opt, $$default, f) { + if (opt) { + return Curry._1(f, opt[0]); + } else { + return $$default; + } +} + +function map(opt, f) { + if (opt) { + return /* Some */[Curry._1(f, opt[0])]; + } else { + return /* None */0; + } +} + +function flatMap(opt, f) { + if (opt) { + return Curry._1(f, opt[0]); + } else { + return /* None */0; + } +} + +function getOrElse(opt, $$default) { + if (opt) { + return opt[0]; + } else { + return $$default; + } +} + +function exists(param) { + if (param) { + return /* true */1; + } else { + return /* false */0; + } +} + +function empty(param) { + if (param) { + return /* false */0; + } else { + return /* true */1; + } +} + +exports.getExn = getExn; +exports.fold = fold; +exports.map = map; +exports.flatMap = flatMap; +exports.getOrElse = getOrElse; +exports.exists = exists; +exports.empty = empty; +/* No side effect */ From 6ed6ca26d97bf2ef2ee7440c9a4e19cc33a0e4a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnar=20=C3=9E=C3=B3r=20Sveinsson?= Date: Wed, 14 Mar 2018 00:23:59 +0000 Subject: [PATCH 2/2] Fix indent in makefile --- jscomp/others/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jscomp/others/Makefile b/jscomp/others/Makefile index 735e29c608..f346816114 100644 --- a/jscomp/others/Makefile +++ b/jscomp/others/Makefile @@ -34,7 +34,7 @@ SOURCE_LIST= node_path node_fs node_process dict node_module js_array js_string belt_internalMapString\ belt_MapString \ belt_MapInt\ - belt_Option\ + belt_Option\ belt_internalSet\ belt_Set\ belt_MutableSet\