Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2621 from mchaver/master
Add Result module to Belt
- Loading branch information
Showing
9 changed files
with
265 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 */ |