Skip to content

Commit

Permalink
Merge b03196a into a428630
Browse files Browse the repository at this point in the history
  • Loading branch information
arnarthor committed Mar 14, 2018
2 parents a428630 + b03196a commit 99fee3b
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 1 deletion.
1 change: 1 addition & 0 deletions jscomp/others/Makefile
Expand Up @@ -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\
Expand Down
6 changes: 5 additions & 1 deletion jscomp/others/belt.ml
Expand Up @@ -236,7 +236,11 @@ module HashSet = Belt_HashSet
*)
module HashMap = Belt_HashMap


(** {!Belt.Option}

Utilities for option data type
*)
module Option = Belt_Option


74 changes: 74 additions & 0 deletions jscomp/others/belt_Option.ml
@@ -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)
43 changes: 43 additions & 0 deletions jscomp/others/belt_Option.mli
@@ -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
3 changes: 3 additions & 0 deletions lib/js/belt.js
Expand Up @@ -27,6 +27,8 @@ var HashSet = 0;

var HashMap = 0;

var Option = 0;

exports.Id = Id;
exports.$$Array = $$Array;
exports.SortArray = SortArray;
Expand All @@ -40,4 +42,5 @@ exports.MutableSet = MutableSet;
exports.MutableMap = MutableMap;
exports.HashSet = HashSet;
exports.HashMap = HashMap;
exports.Option = Option;
/* No side effect */
123 changes: 123 additions & 0 deletions lib/js/belt_Option.js
@@ -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 */

0 comments on commit 99fee3b

Please sign in to comment.