Skip to content

Commit

Permalink
Merge pull request #2621 from mchaver/master
Browse files Browse the repository at this point in the history
Add Result module to Belt
  • Loading branch information
bobzhang committed May 17, 2018
2 parents 6619da4 + 0033bf6 commit d8f50d2
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 2 deletions.
2 changes: 2 additions & 0 deletions jscomp/others/.depend
Expand Up @@ -58,6 +58,7 @@ belt_MapString.cmj : belt_internalMapString.cmj belt_internalAVLtree.cmj \
belt_MapInt.cmj : belt_internalMapInt.cmj belt_internalAVLtree.cmj \
belt_Array.cmj belt_MapInt.cmi
belt_Option.cmj : belt_Option.cmi
belt_Result.cmj : belt_Result.cmi
belt_Set.cmj : belt_SetString.cmj belt_SetInt.cmj belt_SetDict.cmj \
belt_Id.cmj belt_Array.cmj belt_Set.cmi
belt_MutableSet.cmj : belt_internalAVLset.cmj belt_SortArray.cmj \
Expand Down Expand Up @@ -130,6 +131,7 @@ belt_Map.cmi : belt_MapString.cmi belt_MapInt.cmi belt_MapDict.cmi \
belt_MapString.cmi :
belt_MapInt.cmi :
belt_Option.cmi :
belt_Result.cmi :
belt_Set.cmi : belt_SetString.cmi belt_SetInt.cmi belt_SetDict.cmi \
belt_Id.cmi
belt_MutableSet.cmi : belt_MutableSetString.cmi belt_MutableSetInt.cmi \
Expand Down
1 change: 1 addition & 0 deletions jscomp/others/Makefile
Expand Up @@ -35,6 +35,7 @@ SOURCE_LIST= node_path node_fs node_process dict node_module js_array js_string
belt_MapString \
belt_MapInt\
belt_Option\
belt_Result\
belt_internalSet\
belt_Set\
belt_MutableSet\
Expand Down
11 changes: 9 additions & 2 deletions jscomp/others/belt.ml
Expand Up @@ -239,13 +239,20 @@ module HashMap = Belt_HashMap

(** {!Belt.Option}
Utilities for option data type
Utilities for option data type.
*)
module Option = Belt_Option


(** {!Belt.Result}
Utilities for result data type.
*)

module Result = Belt_Result

(** {!Belt.Debug}
Utilities for set up debugging
*)
module Debug = Belt_Debug
module Debug = Belt_Debug
77 changes: 77 additions & 0 deletions jscomp/others/belt_Result.ml
@@ -0,0 +1,77 @@
(* Copyright (C) 2017 Authors of BuckleScript
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

type ('a, 'b) t =
| Ok of 'a
| Error of 'b

let getExn = function
| Ok x -> x
| Error _ -> [%assert "getExn"]

let mapWithDefaultU opt default f = match opt with
| Ok x -> (f x [@bs])
| Error _ -> default

let mapWithDefault opt default f = mapWithDefaultU opt default (fun[@bs] x -> f x)

let mapU opt f = match opt with
| Ok x -> Ok (f x [@bs])
| Error y -> Error y

let map opt f = mapU opt (fun[@bs] x -> f x)

let flatMapU opt f = match opt with
| Ok x -> (f x [@bs])
| Error y -> Error y

let flatMap opt f = flatMapU opt (fun[@bs] x -> f x)

let getWithDefault opt default = match opt with
| Ok x -> x
| Error _ -> default

let isOk = function
| Ok _ -> true
| Error _ -> false

let isError = function
| Ok _ -> false
| Error _ -> true

let eqU a b f = match (a, b) with
| (Ok a, Ok b) -> f a b [@bs]
| (Error _, Ok _)
| (Ok _, Error _) -> false
| (Error _, Error _) -> true

let eq a b f = eqU a b (fun[@bs] x y -> f x y)

let cmpU a b f = match (a, b) with
| (Ok a, Ok b) -> f a b [@bs]
| (Error _, Ok _) -> -1
| (Ok _, Error _) -> 1
| (Error _, Error _) -> 0

let cmp a b f = cmpU a b (fun[@bs] x y -> f x y)
47 changes: 47 additions & 0 deletions jscomp/others/belt_Result.mli
@@ -0,0 +1,47 @@
(* Copyright (C) 2017 Authors of BuckleScript
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

(** {!Belt.Result}
Utilities for result data type.
*)

type ('a, 'b) t =
| Ok of 'a
| Error of 'b

val getExn : ('a, 'b) t -> 'a
val mapWithDefaultU : ('a, 'c) t -> 'b -> ('a -> 'b [@bs]) -> 'b
val mapWithDefault : ('a, 'c) t -> 'b -> ('a -> 'b) -> 'b
val mapU : ('a, 'c) t -> ('a -> 'b [@bs]) -> ('b, 'c) t
val map : ('a, 'c) t -> ('a -> 'b) -> ('b, 'c) t
val flatMapU : ('a, 'c) t -> ('a -> ('b, 'c) t [@bs]) -> ('b, 'c) t
val flatMap : ('a, 'c) t -> ('a -> ('b, 'c) t) -> ('b, 'c) t
val getWithDefault : ('a, 'b) t -> 'a -> 'a
val isOk : ('a, 'b) t -> bool
val isError : ('a, 'b) t -> bool
val eqU : ('a, 'c) t -> ('b, 'd) t -> ('a -> 'b -> bool [@bs]) -> bool
val eq : ('a, 'c) t -> ('b, 'd) t -> ('a -> 'b -> bool) -> bool
val cmpU : ('a, 'c) t -> ('b, 'd) t -> ('a -> 'b -> int [@bs]) -> int
val cmp : ('a, 'c) t -> ('b, 'd) t -> ('a -> 'b -> int) -> int
1 change: 1 addition & 0 deletions jscomp/others/js_result.ml
Expand Up @@ -25,3 +25,4 @@
type (+'good, +'bad) t =
| Ok of 'good
| Error of 'bad
[@@ocaml.deprecated "Please use `Belt.Result.t` instead"]
1 change: 1 addition & 0 deletions jscomp/others/js_result.mli
Expand Up @@ -25,3 +25,4 @@
type (+'good, +'bad) t =
| Ok of 'good
| Error of 'bad
[@@ocaml.deprecated "Please use `Belt.Result.t` instead"]
3 changes: 3 additions & 0 deletions lib/js/belt.js
Expand Up @@ -29,6 +29,8 @@ var HashMap = 0;

var Option = 0;

var Result = 0;

var Debug = 0;

exports.Id = Id;
Expand All @@ -45,5 +47,6 @@ exports.MutableMap = MutableMap;
exports.HashSet = HashSet;
exports.HashMap = HashMap;
exports.Option = Option;
exports.Result = Result;
exports.Debug = Debug;
/* No side effect */
124 changes: 124 additions & 0 deletions lib/js/belt_Result.js
@@ -0,0 +1,124 @@
'use strict';

var Block = require("./block.js");
var Curry = require("./curry.js");

function getExn(param) {
if (param.tag) {
throw new Error("getExn");
} else {
return param[0];
}
}

function mapWithDefaultU(opt, $$default, f) {
if (opt.tag) {
return $$default;
} else {
return f(opt[0]);
}
}

function mapWithDefault(opt, $$default, f) {
return mapWithDefaultU(opt, $$default, Curry.__1(f));
}

function mapU(opt, f) {
if (opt.tag) {
return /* Error */Block.__(1, [opt[0]]);
} else {
return /* Ok */Block.__(0, [f(opt[0])]);
}
}

function map(opt, f) {
return mapU(opt, Curry.__1(f));
}

function flatMapU(opt, f) {
if (opt.tag) {
return /* Error */Block.__(1, [opt[0]]);
} else {
return f(opt[0]);
}
}

function flatMap(opt, f) {
return flatMapU(opt, Curry.__1(f));
}

function getWithDefault(opt, $$default) {
if (opt.tag) {
return $$default;
} else {
return opt[0];
}
}

function isOk(param) {
if (param.tag) {
return /* false */0;
} else {
return /* true */1;
}
}

function isError(param) {
if (param.tag) {
return /* true */1;
} else {
return /* false */0;
}
}

function eqU(a, b, f) {
if (a.tag) {
if (b.tag) {
return /* true */1;
} else {
return /* false */0;
}
} else if (b.tag) {
return /* false */0;
} else {
return f(a[0], b[0]);
}
}

function eq(a, b, f) {
return eqU(a, b, Curry.__2(f));
}

function cmpU(a, b, f) {
if (a.tag) {
if (b.tag) {
return 0;
} else {
return -1;
}
} else if (b.tag) {
return 1;
} else {
return f(a[0], b[0]);
}
}

function cmp(a, b, f) {
return cmpU(a, b, Curry.__2(f));
}

exports.getExn = getExn;
exports.mapWithDefaultU = mapWithDefaultU;
exports.mapWithDefault = mapWithDefault;
exports.mapU = mapU;
exports.map = map;
exports.flatMapU = flatMapU;
exports.flatMap = flatMap;
exports.getWithDefault = getWithDefault;
exports.isOk = isOk;
exports.isError = isError;
exports.eqU = eqU;
exports.eq = eq;
exports.cmpU = cmpU;
exports.cmp = cmp;
/* No side effect */

0 comments on commit d8f50d2

Please sign in to comment.