Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Result module to Belt #2621

Merged
merged 17 commits into from May 17, 2018
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 @@ -129,6 +130,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
8 changes: 7 additions & 1 deletion jscomp/others/belt.ml
Expand Up @@ -239,8 +239,14 @@ 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
76 changes: 76 additions & 0 deletions jscomp/others/belt_Result.ml
@@ -0,0 +1,76 @@
(* 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 = ('a, 'b) Js_result.t = Ok of 'a | Error of 'b

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

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

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

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

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

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

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

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

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

let isError = function
| Js_result.Ok _ -> false
| Js_result.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)
45 changes: 45 additions & 0 deletions jscomp/others/belt_Result.mli
@@ -0,0 +1,45 @@
(* 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 = ('a, 'b) Js_result.t = Ok of 'a | Error of 'b

val getExn : ('a, 'b) t -> 'a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should getExn contain some information about Error ?

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
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;

exports.Id = Id;
exports.$$Array = $$Array;
exports.SortArray = SortArray;
Expand All @@ -43,4 +45,5 @@ exports.MutableMap = MutableMap;
exports.HashSet = HashSet;
exports.HashMap = HashMap;
exports.Option = Option;
exports.Result = Result;
/* No side effect */