-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
249 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
(* 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. *) | ||
|
||
|
||
let getExn = function | ||
| Some x -> x | ||
| None -> [%assert "getExn"] | ||
|
||
let foldU opt default f = match opt with | ||
| Some x -> (f x [@bs]) | ||
| None -> default | ||
|
||
let fold opt default f = foldU opt default (fun[@bs] x -> f x) | ||
|
||
let mapU opt f = match opt with | ||
| Some x -> Some (f x [@bs]) | ||
| None -> None | ||
|
||
let map opt f = mapU opt (fun[@bs] x -> f x) | ||
|
||
let flatMapU opt f = match opt with | ||
| Some x -> (f x [@bs]) | ||
| None -> None | ||
|
||
let flatMap opt f = flatMapU opt (fun[@bs] x -> f x) | ||
|
||
let getWithDefault opt default = match opt with | ||
| Some x -> x | ||
| None -> default | ||
|
||
let isSome = function | ||
| Some _ -> true | ||
| None -> false | ||
|
||
let isNone = function | ||
| Some _ -> false | ||
| None -> true | ||
|
||
let eqU a b f = match (a, b) with | ||
| (Some a, Some b) -> f a b [@bs] | ||
| (None, Some _) | ||
| (Some _, None) -> false | ||
| (None, None) -> 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 | ||
| (Some a, Some b) -> f a b [@bs] | ||
| (None, Some _) -> -1 | ||
| (Some _, None) -> 1 | ||
| (None, None) -> 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,43 @@ | ||
(* 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.Option} | ||
Utilities for option data type | ||
*) | ||
|
||
val getExn : 'a option -> 'a | ||
val foldU : 'a option -> 'b -> ('a -> 'b [@bs]) -> 'b | ||
val fold : 'a option -> 'b -> ('a -> 'b) -> 'b | ||
val mapU : 'a option -> ('a -> 'b [@bs]) -> 'b option | ||
val map : 'a option -> ('a -> 'b) -> 'b option | ||
val flatMapU : 'a option -> ('a -> 'b option [@bs]) -> 'b option | ||
val flatMap : 'a option -> ('a -> 'b option) -> 'b option | ||
val getWithDefault : 'a option -> 'a -> 'a | ||
val isSome : 'a option -> bool | ||
val isNone : 'a option -> bool | ||
val eqU : 'a option -> 'b option -> ('a -> 'b -> bool [@bs]) -> bool | ||
val eq : 'a option -> 'b option -> ('a -> 'b -> bool) -> bool | ||
val cmpU : 'a option -> 'b option -> ('a -> 'b -> int [@bs]) -> int | ||
val cmp : 'a option -> 'b option -> ('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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
'use strict'; | ||
|
||
var Curry = require("./curry.js"); | ||
|
||
function getExn(param) { | ||
if (param) { | ||
return param[0]; | ||
} else { | ||
throw new Error("getExn"); | ||
} | ||
} | ||
|
||
function foldU(opt, $$default, f) { | ||
if (opt) { | ||
return f(opt[0]); | ||
} else { | ||
return $$default; | ||
} | ||
} | ||
|
||
function fold(opt, $$default, f) { | ||
return foldU(opt, $$default, Curry.__1(f)); | ||
} | ||
|
||
function mapU(opt, f) { | ||
if (opt) { | ||
return /* Some */[f(opt[0])]; | ||
} else { | ||
return /* None */0; | ||
} | ||
} | ||
|
||
function map(opt, f) { | ||
return mapU(opt, Curry.__1(f)); | ||
} | ||
|
||
function flatMapU(opt, f) { | ||
if (opt) { | ||
return f(opt[0]); | ||
} else { | ||
return /* None */0; | ||
} | ||
} | ||
|
||
function flatMap(opt, f) { | ||
return flatMapU(opt, Curry.__1(f)); | ||
} | ||
|
||
function getWithDefault(opt, $$default) { | ||
if (opt) { | ||
return opt[0]; | ||
} else { | ||
return $$default; | ||
} | ||
} | ||
|
||
function isSome(param) { | ||
if (param) { | ||
return /* true */1; | ||
} else { | ||
return /* false */0; | ||
} | ||
} | ||
|
||
function isNone(param) { | ||
if (param) { | ||
return /* false */0; | ||
} else { | ||
return /* true */1; | ||
} | ||
} | ||
|
||
function eqU(a, b, f) { | ||
if (a) { | ||
if (b) { | ||
return f(a[0], b[0]); | ||
} else { | ||
return /* false */0; | ||
} | ||
} else if (b) { | ||
return /* false */0; | ||
} else { | ||
return /* true */1; | ||
} | ||
} | ||
|
||
function eq(a, b, f) { | ||
return eqU(a, b, Curry.__2(f)); | ||
} | ||
|
||
function cmpU(a, b, f) { | ||
if (a) { | ||
if (b) { | ||
return f(a[0], b[0]); | ||
} else { | ||
return 1; | ||
} | ||
} else if (b) { | ||
return -1; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
function cmp(a, b, f) { | ||
return cmpU(a, b, Curry.__2(f)); | ||
} | ||
|
||
exports.getExn = getExn; | ||
exports.foldU = foldU; | ||
exports.fold = fold; | ||
exports.mapU = mapU; | ||
exports.map = map; | ||
exports.flatMapU = flatMapU; | ||
exports.flatMap = flatMap; | ||
exports.getWithDefault = getWithDefault; | ||
exports.isSome = isSome; | ||
exports.isNone = isNone; | ||
exports.eqU = eqU; | ||
exports.eq = eq; | ||
exports.cmpU = cmpU; | ||
exports.cmp = cmp; | ||
/* No side effect */ |