├── .gitignore ├── README.md ├── bindings ├── .gitignore ├── bs-mocha │ ├── bsconfig.json │ ├── lib │ │ ├── amdjs │ │ │ └── bs_mocha.js │ │ ├── goog │ │ │ └── bs_mocha.js │ │ ├── js │ │ │ ├── bs_mocha.js │ │ │ └── src │ │ │ │ └── bs_mocha.js │ │ └── ocaml │ │ │ └── .gitignore │ ├── package.json │ ├── scripts │ │ └── install.sh │ └── src │ │ ├── bs_mocha.ml │ │ └── bs_mocha.mli ├── bs-promise │ └── README.md ├── bs-re │ ├── README.md │ └── src │ │ ├── bs_re.ml │ │ └── fox_test.ml └── bs-string │ ├── README.md │ ├── bsconfig.json │ ├── lib │ ├── amdjs │ │ ├── bs_array.js │ │ ├── bs_math.js │ │ ├── bs_nativeint.js │ │ └── bs_string.js │ ├── goog │ │ ├── bs_array.js │ │ ├── bs_math.js │ │ ├── bs_nativeint.js │ │ └── bs_string.js │ ├── js │ │ ├── bs_array.js │ │ ├── bs_math.js │ │ ├── bs_nativeint.js │ │ ├── bs_string.js │ │ ├── src │ │ │ ├── bs_array.js │ │ │ ├── bs_math.js │ │ │ ├── bs_nativeint.js │ │ │ └── bs_string.js │ │ └── test │ │ │ └── bs_string_test.js │ └── ocaml │ │ └── .gitignore │ ├── package.json │ ├── scripts │ └── install.js │ ├── src │ ├── bs_array.ml │ ├── bs_math.ml │ ├── bs_nativeint.ml │ ├── bs_string.js │ └── bs_string.ml │ └── test │ ├── bs_string_test.js │ └── bs_string_test.ml └── examples ├── bs-readline ├── bsconfig.json ├── package.json └── src │ ├── bs_process.js │ ├── bs_process.ml │ ├── bs_readline.js │ ├── bs_readline.ml │ ├── bs_readline_test.js │ ├── bs_readline_test.ml │ ├── bs_stream.js │ └── bs_stream.ml ├── bs-string-map ├── README.md ├── lib │ ├── amdjs │ │ ├── .gitignore │ │ └── string_map.js │ ├── goog │ │ ├── .gitignore │ │ └── string_map.js │ ├── js │ │ ├── string_map.js │ │ └── test │ │ │ ├── .gitignore │ │ │ └── test_map.js │ └── ocaml │ │ └── .gitignore ├── package.json ├── scripts │ └── install.sh ├── src │ ├── string_map.ml │ └── string_map.mli └── test │ ├── test_map.js │ ├── test_map.ml │ └── test_map.mli ├── hello ├── main_entry.js ├── main_entry.ml └── package.json ├── node-http-server ├── README.md ├── package.json └── src │ ├── http_types.js │ ├── http_types.ml │ ├── test_http_server.js │ ├── test_http_server.ml │ └── test_http_server.mli └── reason-demo ├── README.md ├── hello.js ├── hello.re └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | node_modules 3 | *.annot 4 | *.cmi 5 | *.cmj 6 | *.log 7 | *.d 8 | .bsbuild 9 | .bsdeps 10 | .ninja_deps 11 | .ninja_log 12 | *.mlast 13 | *.mliast 14 | .merlin 15 | bs/ 16 | ocaml/ 17 | 18 | build.ninja # we should not check in since it is OS dependent -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bucklescript-addons 2 | Addons modules for the [BuckleScript](https://github.com/bloomberg/bucklescript) OCaml->Javascript translator 3 | 4 | 5 | This repo provide users some [examples](./examples) showing how to use 6 | BuckleScript and some basic [bindings](./bindings) 7 | 8 | For documentation of BuckleScript, see 9 | http://bloomberg.github.io/bucklescript/ 10 | 11 | -------------------------------------------------------------------------------- /bindings/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rescript-lang/bucklescript-addons/c544020712b9507b8b4900444320c2448f9c7ae5/bindings/.gitignore -------------------------------------------------------------------------------- /bindings/bs-mocha/bsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bs-mocha", 3 | "version": "0.1.6", 4 | "sources": { 5 | "dir": "src", 6 | "public": "all", 7 | "files": [ 8 | "bs_mocha.mli", 9 | "bs_mocha.ml" 10 | ] 11 | }, 12 | 13 | "generate-merlin" : true 14 | } -------------------------------------------------------------------------------- /bindings/bs-mocha/lib/amdjs/bs_mocha.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | define(["exports", "bs-platform/lib/amdjs/caml_builtin_exceptions", "bs-platform/lib/amdjs/list", "assert"], 4 | function(exports, Caml_builtin_exceptions, List, Assert){ 5 | 'use strict'; 6 | function from_suites(name, suite) { 7 | describe(name, function () { 8 | return List.iter(function (param) { 9 | it(param[0], param[1]); 10 | return /* () */0; 11 | }, suite); 12 | }); 13 | return /* () */0; 14 | } 15 | 16 | function close_enough(x, y) { 17 | return +(Math.abs(x - y) < 0.0000001); 18 | } 19 | 20 | function from_pair_suites(name, suites) { 21 | describe(name, function () { 22 | return List.iter(function (param) { 23 | var code = param[1]; 24 | it(param[0], function () { 25 | var match = code(); 26 | switch (match.tag | 0) { 27 | case 0 : 28 | Assert.deepEqual(match[0], match[1]); 29 | return /* () */0; 30 | case 1 : 31 | Assert.notDeepEqual(match[0], match[1]); 32 | return /* () */0; 33 | case 2 : 34 | if (close_enough(match[0], match[1])) { 35 | return 0; 36 | } 37 | else { 38 | throw [ 39 | Caml_builtin_exceptions.assert_failure, 40 | [ 41 | "src/bs_mocha.ml", 42 | 78, 43 | 16 44 | ] 45 | ]; 46 | } 47 | break; 48 | case 3 : 49 | Assert.throws(match[0]); 50 | return /* () */0; 51 | 52 | } 53 | }); 54 | return /* () */0; 55 | }, suites); 56 | }); 57 | return /* () */0; 58 | } 59 | 60 | exports.from_suites = from_suites; 61 | exports.from_pair_suites = from_pair_suites; 62 | 63 | }) 64 | /* assert Not a pure module */ 65 | -------------------------------------------------------------------------------- /bindings/bs-mocha/lib/goog/bs_mocha.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | goog.module("bs-mocha.bs_mocha"); 4 | 5 | var Caml_builtin_exceptions = goog.require("bs-platform.caml_builtin_exceptions"); 6 | var List = goog.require("bs-platform.list"); 7 | var Assert = goog.require("assert"); 8 | 9 | function from_suites(name, suite) { 10 | describe(name, function () { 11 | return List.iter(function (param) { 12 | it(param[0], param[1]); 13 | return /* () */0; 14 | }, suite); 15 | }); 16 | return /* () */0; 17 | } 18 | 19 | function close_enough(x, y) { 20 | return +(Math.abs(x - y) < 0.0000001); 21 | } 22 | 23 | function from_pair_suites(name, suites) { 24 | describe(name, function () { 25 | return List.iter(function (param) { 26 | var code = param[1]; 27 | it(param[0], function () { 28 | var match = code(); 29 | switch (match.tag | 0) { 30 | case 0 : 31 | Assert.deepEqual(match[0], match[1]); 32 | return /* () */0; 33 | case 1 : 34 | Assert.notDeepEqual(match[0], match[1]); 35 | return /* () */0; 36 | case 2 : 37 | if (close_enough(match[0], match[1])) { 38 | return 0; 39 | } 40 | else { 41 | throw [ 42 | Caml_builtin_exceptions.assert_failure, 43 | [ 44 | "src/bs_mocha.ml", 45 | 78, 46 | 16 47 | ] 48 | ]; 49 | } 50 | break; 51 | case 3 : 52 | Assert.throws(match[0]); 53 | return /* () */0; 54 | 55 | } 56 | }); 57 | return /* () */0; 58 | }, suites); 59 | }); 60 | return /* () */0; 61 | } 62 | 63 | exports.from_suites = from_suites; 64 | exports.from_pair_suites = from_pair_suites; 65 | /* assert Not a pure module */ 66 | -------------------------------------------------------------------------------- /bindings/bs-mocha/lib/js/bs_mocha.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | var Caml_builtin_exceptions = require("bs-platform/lib/js/caml_builtin_exceptions"); 5 | var List = require("bs-platform/lib/js/list"); 6 | var Assert = require("assert"); 7 | 8 | function from_suites(name, suite) { 9 | describe(name, function () { 10 | return List.iter(function (param) { 11 | it(param[0], param[1]); 12 | return /* () */0; 13 | }, suite); 14 | }); 15 | return /* () */0; 16 | } 17 | 18 | function close_enough(x, y) { 19 | return +(Math.abs(x - y) < 0.0000001); 20 | } 21 | 22 | function from_pair_suites(name, suites) { 23 | describe(name, function () { 24 | return List.iter(function (param) { 25 | var code = param[1]; 26 | it(param[0], function () { 27 | var match = code(); 28 | switch (match.tag | 0) { 29 | case 0 : 30 | Assert.deepEqual(match[0], match[1]); 31 | return /* () */0; 32 | case 1 : 33 | Assert.notDeepEqual(match[0], match[1]); 34 | return /* () */0; 35 | case 2 : 36 | if (close_enough(match[0], match[1])) { 37 | return 0; 38 | } 39 | else { 40 | throw [ 41 | Caml_builtin_exceptions.assert_failure, 42 | [ 43 | "src/bs_mocha.ml", 44 | 78, 45 | 16 46 | ] 47 | ]; 48 | } 49 | break; 50 | case 3 : 51 | Assert.throws(match[0]); 52 | return /* () */0; 53 | 54 | } 55 | }); 56 | return /* () */0; 57 | }, suites); 58 | }); 59 | return /* () */0; 60 | } 61 | 62 | exports.from_suites = from_suites; 63 | exports.from_pair_suites = from_pair_suites; 64 | /* assert Not a pure module */ 65 | -------------------------------------------------------------------------------- /bindings/bs-mocha/lib/js/src/bs_mocha.js: -------------------------------------------------------------------------------- 1 | // Generated by BUCKLESCRIPT VERSION 1.4.3, PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | var List = require("bs-platform/lib/js/list"); 5 | var Curry = require("bs-platform/lib/js/curry"); 6 | var Assert = require("assert"); 7 | var Js_boolean = require("bs-platform/lib/js/js_boolean"); 8 | 9 | function assert_fail(msg) { 10 | Assert.fail(/* () */0, /* () */0, msg, ""); 11 | return /* () */0; 12 | } 13 | 14 | function from_suites(name, suite) { 15 | describe(name, function () { 16 | return List.iter(function (param) { 17 | it(param[0], param[1]); 18 | return /* () */0; 19 | }, suite); 20 | }); 21 | return /* () */0; 22 | } 23 | 24 | function close_enough($staropt$star, a, b) { 25 | var threshold = $staropt$star ? $staropt$star[0] : 0.0000001; 26 | return +(Math.abs(a - b) < threshold); 27 | } 28 | 29 | function from_pair_suites(name, suites) { 30 | describe(name, function () { 31 | return List.iter(function (param) { 32 | var code = param[1]; 33 | it(param[0], function () { 34 | var match = Curry._1(code, /* () */0); 35 | switch (match.tag | 0) { 36 | case 0 : 37 | Assert.deepEqual(match[0], match[1]); 38 | return /* () */0; 39 | case 1 : 40 | Assert.notDeepEqual(match[0], match[1]); 41 | return /* () */0; 42 | case 2 : 43 | Assert.ok(Js_boolean.to_js_boolean(match[0])); 44 | return /* () */0; 45 | case 3 : 46 | var b = match[1]; 47 | var a = match[0]; 48 | if (close_enough(/* None */0, a, b)) { 49 | return 0; 50 | } 51 | else { 52 | Assert.deepEqual(a, b); 53 | return /* () */0; 54 | } 55 | case 4 : 56 | var b$1 = match[2]; 57 | var a$1 = match[1]; 58 | if (close_enough(/* Some */[match[0]], a$1, b$1)) { 59 | return 0; 60 | } 61 | else { 62 | Assert.deepEqual(a$1, b$1); 63 | return /* () */0; 64 | } 65 | case 5 : 66 | Assert.throws(match[0]); 67 | return /* () */0; 68 | case 6 : 69 | return assert_fail("failed"); 70 | case 7 : 71 | return assert_fail(match[0]); 72 | 73 | } 74 | }); 75 | return /* () */0; 76 | }, suites); 77 | }); 78 | return /* () */0; 79 | } 80 | 81 | exports.from_suites = from_suites; 82 | exports.from_pair_suites = from_pair_suites; 83 | /* assert Not a pure module */ 84 | -------------------------------------------------------------------------------- /bindings/bs-mocha/lib/ocaml/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rescript-lang/bucklescript-addons/c544020712b9507b8b4900444320c2448f9c7ae5/bindings/bs-mocha/lib/ocaml/.gitignore -------------------------------------------------------------------------------- /bindings/bs-mocha/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bs-mocha", 3 | "version": "0.1.6", 4 | "description": "A minimal binding to mochajs for bucklescript", 5 | "scripts": { 6 | "postinstall": "bsb", 7 | "start": "bsb -make-world -w" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/bloomberg/bucklescript-addons.git" 12 | }, 13 | "keywords": [ 14 | "ocaml", 15 | "bucklescript", 16 | "map" 17 | ], 18 | "author": "Hongbo Zhang", 19 | "license": "ISC", 20 | "bugs": { 21 | "url": "https://github.com/bloomberg/bucklescript-addons/issues" 22 | }, 23 | "homepage": "https://github.com/bloomberg/bucklescript-addons#readme", 24 | "dependencies": { 25 | "mocha": "^2.5.3", 26 | "bs-platform" : "^1.3.3" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /bindings/bs-mocha/scripts/install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -e 4 | # echo $PATH 5 | echo $(bsc -where) 6 | bsc -bs-package-name bs-mocha -bs-package-output lib/js -bs-package-output amdjs:lib/amdjs -bs-package-output goog:lib/goog -I src -c -bs-files src/*.mli src/*.ml 7 | 8 | # install 9 | cp ./src/*.cm* ./lib/ocaml 10 | -------------------------------------------------------------------------------- /bindings/bs-mocha/src/bs_mocha.ml: -------------------------------------------------------------------------------- 1 | 2 | (* Copyright (C) 2015-2016 Bloomberg Finance L.P. 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * In addition to the permissions granted to you by the LGPL, you may combine 10 | * or link a "work that uses the Library" with a publicly distributed version 11 | * of this file to produce a combined library or application, then distribute 12 | * that combined work under the terms of your choosing, with no requirement 13 | * to comply with the obligations normally placed on you by section 4 of the 14 | * LGPL version 3 (or the corresponding section of a later version of the LGPL 15 | * should you choose to use a later version). 16 | * 17 | * This program is distributed in the hope that it will be useful, 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 | * GNU Lesser General Public License for more details. 21 | * 22 | * You should have received a copy of the GNU Lesser General Public License 23 | * along with this program; if not, write to the Free Software 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) 25 | 26 | external describe : string -> (unit -> unit[@bs]) -> unit = "describe" 27 | [@@bs.val] 28 | 29 | external it : string -> (unit -> unit) -> unit = "it" 30 | [@@bs.val] 31 | 32 | external eq : 'a -> 'a -> unit = "deepEqual" 33 | [@@bs.val] 34 | [@@bs.module "assert"] 35 | 36 | external neq : 'a -> 'a -> unit = "notDeepEqual" 37 | [@@bs.val] 38 | [@@bs.module "assert"] 39 | 40 | external ok : Js.boolean -> unit = "ok" 41 | [@@bs.val] 42 | [@@bs.module "assert"] 43 | 44 | external fail : 'a -> 'a -> string Js.undefined -> string -> unit = "fail" 45 | [@@bs.val] 46 | [@@bs.module "assert"] 47 | 48 | 49 | external dump : 'a array -> unit = "console.log" [@@bs.val ] [@@bs.splice] 50 | external throws : (unit -> unit) -> unit = "throws" [@@bs.val] [@@bs.module "assert"] 51 | (** There is a problem -- 52 | it does not return [unit ] 53 | *) 54 | 55 | let assert_equal = eq 56 | let assert_notequal = neq 57 | let assert_ok = fun a -> ok (Js.Boolean.to_js_boolean a) 58 | let assert_fail = fun msg -> fail () () (Js.Undefined.return msg) "" 59 | (* assert -- raises an AssertionError which mocha handls better 60 | *) 61 | let from_suites name (suite : (string * ('a -> unit)) list) = 62 | describe name (fun [@bs] () -> 63 | List.iter (fun (name, code) -> it name code) suite) 64 | 65 | type eq = 66 | | Eq : 'a *'a -> eq 67 | | Neq : 'a * 'a -> eq 68 | | Ok : bool -> eq 69 | | Approx : float * float -> eq 70 | | ApproxThreshold : float * float * float -> eq 71 | | ThrowAny : (unit -> unit) -> eq 72 | | Fail : unit -> eq 73 | | FailWith : string -> eq 74 | (* TODO: | Exception : exn -> (unit -> unit) -> _ eq *) 75 | 76 | type pair_suites = (string * (unit -> eq)) list 77 | 78 | let close_enough ?(threshold=0.0000001 (* epsilon_float *)) a b = 79 | abs_float (a -. b) < threshold 80 | 81 | let from_pair_suites name (suites : pair_suites) = 82 | describe name (fun [@bs] () -> 83 | suites |> 84 | List.iter (fun (name, code) -> 85 | it name (fun _ -> 86 | match code () with 87 | | Eq(a,b) -> assert_equal a b 88 | | Neq(a,b) -> assert_notequal a b 89 | | Ok(a) -> assert_ok a 90 | | Approx(a, b) -> 91 | if not (close_enough a b) then assert_equal a b (* assert_equal gives better ouput *) 92 | | ApproxThreshold(t, a, b) -> 93 | if not (close_enough ~threshold:t a b) then assert_equal a b (* assert_equal gives better ouput *) 94 | | ThrowAny fn -> throws fn 95 | | Fail _ -> assert_fail "failed" 96 | | FailWith msg -> assert_fail msg 97 | ) 98 | ) 99 | ) 100 | -------------------------------------------------------------------------------- /bindings/bs-mocha/src/bs_mocha.mli: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2015-2016 Bloomberg Finance L.P. 2 | * 3 | * This program is free software: you can redistribute it and/or modify 4 | * it under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or 6 | * (at your option) any later version. 7 | * 8 | * In addition to the permissions granted to you by the LGPL, you may combine 9 | * or link a "work that uses the Library" with a publicly distributed version 10 | * of this file to produce a combined library or application, then distribute 11 | * that combined work under the terms of your choosing, with no requirement 12 | * to comply with the obligations normally placed on you by section 4 of the 13 | * LGPL version 3 (or the corresponding section of a later version of the LGPL 14 | * should you choose to use a later version). 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with this program; if not, write to the Free Software 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) 24 | 25 | 26 | type eq = 27 | | Eq : 'a *'a -> eq 28 | | Neq : 'a * 'a -> eq 29 | | Ok : bool -> eq 30 | | Approx : float * float -> eq 31 | | ApproxThreshold : float * float * float -> eq 32 | | ThrowAny : (unit -> unit) -> eq 33 | | Fail : unit -> eq 34 | | FailWith : string -> eq 35 | type pair_suites = (string * (unit -> eq)) list 36 | 37 | val from_suites : string -> (string * (unit -> unit)) list -> unit 38 | val from_pair_suites : string -> pair_suites -> unit 39 | -------------------------------------------------------------------------------- /bindings/bs-promise/README.md: -------------------------------------------------------------------------------- 1 | `bs-promise` has moved to https://github.com/BuckleTypes/bs-promise 2 | -------------------------------------------------------------------------------- /bindings/bs-re/README.md: -------------------------------------------------------------------------------- 1 | These bindings are obsolete, replaced by the `Js.Re` module that is shipped with BuckleScript. 2 | -------------------------------------------------------------------------------- /bindings/bs-re/src/bs_re.ml: -------------------------------------------------------------------------------- 1 | 2 | (* Copyright (C) 2015-2016 Bloomberg Finance L.P. 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * In addition to the permissions granted to you by the LGPL, you may combine 10 | * or link a "work that uses the Library" with a publicly distributed version 11 | * of this file to produce a combined library or application, then distribute 12 | * that combined work under the terms of your choosing, with no requirement 13 | * to comply with the obligations normally placed on you by section 4 of the 14 | * LGPL version 3 (or the corresponding section of a later version of the LGPL 15 | * should you choose to use a later version). 16 | * 17 | * This program is distributed in the hope that it will be useful, 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 | * GNU Lesser General Public License for more details. 21 | * 22 | * You should have received a copy of the GNU Lesser General Public License 23 | * along with this program; if not, write to the Free Software 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) 25 | 26 | (** Bindings to JavaScript regex expressions: 27 | https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp 28 | *) 29 | 30 | 31 | external create : string -> flag:string -> Js.re = 32 | "RegExp" [@@bs.new] 33 | 34 | type exec_output 35 | 36 | external exec : Js.re -> string -> exec_output Js.null = "exec" [@@bs.send] 37 | 38 | external out_values : exec_output -> string array = "%identity" 39 | 40 | external out_index : exec_output -> int = "index" [@@bs.send] 41 | external out_input : exec_output -> string = "input" [@@bs.send] 42 | 43 | external test : exec_output -> string -> Js.boolean = "test" [@@bs.send] 44 | 45 | 46 | -------------------------------------------------------------------------------- /bindings/bs-re/src/fox_test.ml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | let re = [%bs.re {|/quick\s(brown).+?(jumps)/ig|} ] 5 | 6 | let result = Bs_re.exec "The Quick Brown Fox Jumps Over The Lazy Dog" 7 | 8 | let () = 9 | match Js.Null.to_opt result with 10 | | None -> assert false 11 | | Some output -> 12 | (Bs_re.out_values output = 13 | [|"Quick Brown Fox Jumps"; 14 | "Brown"; 15 | "Jumps" 16 | |], 17 | Bs_re.out_index output = 4) 18 | -------------------------------------------------------------------------------- /bindings/bs-string/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Bindings to JS String for BuckleScript 4 | 5 | These bindings are obsolete. Replaced by the `Js.String`, `Js.Array` and `JS.Math` modules shipped with BuckleScript. 6 | -------------------------------------------------------------------------------- /bindings/bs-string/bsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bs-string", 3 | "version": "0.1.3", 4 | "bs-dependencies": [ 5 | "bs-mocha" 6 | ], 7 | "sources": [ 8 | { 9 | "dir": "src", 10 | "public": "all", 11 | "files": [ 12 | "bs_string.ml", 13 | "bs_nativeint.ml", 14 | "bs_math.ml", 15 | "bs_array.ml" 16 | ] 17 | }, 18 | { 19 | "dir": "test", 20 | "files": [ 21 | "bs_string_test.ml" 22 | ] 23 | } 24 | ], 25 | "generate-merlin" : true 26 | } -------------------------------------------------------------------------------- /bindings/bs-string/lib/amdjs/bs_array.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | define(["exports"], 4 | function(exports){ 5 | 'use strict'; 6 | 7 | 8 | 9 | }) 10 | /* No side effect */ 11 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/amdjs/bs_math.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | define(["exports"], 4 | function(exports){ 5 | 'use strict'; 6 | 7 | 8 | 9 | }) 10 | /* No side effect */ 11 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/amdjs/bs_nativeint.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | define(["exports"], 4 | function(exports){ 5 | 'use strict'; 6 | 7 | 8 | 9 | }) 10 | /* No side effect */ 11 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/amdjs/bs_string.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | define(["exports"], 4 | function(exports){ 5 | 'use strict'; 6 | function ascii_explode(s) { 7 | return s.split(""); 8 | } 9 | 10 | exports.ascii_explode = ascii_explode; 11 | 12 | }) 13 | /* No side effect */ 14 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/goog/bs_array.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | goog.module("bs-string.bs_array"); 4 | 5 | 6 | 7 | /* No side effect */ 8 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/goog/bs_math.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | goog.module("bs-string.bs_math"); 4 | 5 | 6 | 7 | /* No side effect */ 8 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/goog/bs_nativeint.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | goog.module("bs-string.bs_nativeint"); 4 | 5 | 6 | 7 | /* No side effect */ 8 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/goog/bs_string.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | goog.module("bs-string.bs_string"); 4 | 5 | 6 | function ascii_explode(s) { 7 | return s.split(""); 8 | } 9 | 10 | exports.ascii_explode = ascii_explode; 11 | /* No side effect */ 12 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/js/bs_array.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | 6 | /* No side effect */ 7 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/js/bs_math.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | 6 | /* No side effect */ 7 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/js/bs_nativeint.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | 6 | /* No side effect */ 7 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/js/bs_string.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | function ascii_explode(s) { 6 | return s.split(""); 7 | } 8 | 9 | exports.ascii_explode = ascii_explode; 10 | /* No side effect */ 11 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/js/src/bs_array.js: -------------------------------------------------------------------------------- 1 | // Generated by BUCKLESCRIPT VERSION 1.4.1 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | 6 | /* No side effect */ 7 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/js/src/bs_math.js: -------------------------------------------------------------------------------- 1 | // Generated by BUCKLESCRIPT VERSION 1.4.1 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | 6 | /* No side effect */ 7 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/js/src/bs_nativeint.js: -------------------------------------------------------------------------------- 1 | // Generated by BUCKLESCRIPT VERSION 1.4.1 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | 6 | /* No side effect */ 7 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/js/src/bs_string.js: -------------------------------------------------------------------------------- 1 | // Generated by BUCKLESCRIPT VERSION 1.4.1 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | function ascii_explode(s) { 6 | return s.split(""); 7 | } 8 | 9 | exports.ascii_explode = ascii_explode; 10 | /* No side effect */ 11 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/js/test/bs_string_test.js: -------------------------------------------------------------------------------- 1 | // Generated by BUCKLESCRIPT VERSION 1.4.1 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | var Pervasives = require("bs-platform/lib/js/pervasives"); 5 | var Bs_mocha = require("bs-mocha/lib/js/src/bs_mocha"); 6 | var Block = require("bs-platform/lib/js/block"); 7 | var Bs_string = require("../src/bs_string"); 8 | 9 | var suites = [/* [] */0]; 10 | 11 | var test_id = [0]; 12 | 13 | function eq(loc, param) { 14 | var y = param[1]; 15 | var x = param[0]; 16 | test_id[0] = test_id[0] + 1 | 0; 17 | suites[0] = /* :: */[ 18 | /* tuple */[ 19 | loc + (" id " + Pervasives.string_of_int(test_id[0])), 20 | function () { 21 | return /* Eq */Block.__(0, [ 22 | x, 23 | y 24 | ]); 25 | } 26 | ], 27 | suites[0] 28 | ]; 29 | return /* () */0; 30 | } 31 | 32 | eq('File "/Users/hzhang295/git/bucklescript-addons/bindings/bs-string/test/bs_string_test.ml", line 14, characters 7-14', /* tuple */[ 33 | "hihi".slice(2), 34 | "hi" 35 | ]); 36 | 37 | eq('File "/Users/hzhang295/git/bucklescript-addons/bindings/bs-string/test/bs_string_test.ml", line 15, characters 7-14', /* tuple */[ 38 | " The quick brown fox jumps over the lazy dog. ".split((/\s+/)), 39 | /* array */[ 40 | "", 41 | "The", 42 | "quick", 43 | "brown", 44 | "fox", 45 | "jumps", 46 | "over", 47 | "the", 48 | "lazy", 49 | "dog.", 50 | "" 51 | ] 52 | ]); 53 | 54 | eq('File "/Users/hzhang295/git/bucklescript-addons/bindings/bs-string/test/bs_string_test.ml", line 30, characters 7-14', /* tuple */[ 55 | /* array */[ 56 | "a", 57 | "b", 58 | "c", 59 | ";", 60 | "d" 61 | ], 62 | Bs_string.ascii_explode("abc;d") 63 | ]); 64 | 65 | Bs_mocha.from_pair_suites("/Users/hzhang295/git/bucklescript-addons/bindings/bs-string/test/bs_string_test.ml", suites[0]); 66 | 67 | exports.suites = suites; 68 | exports.test_id = test_id; 69 | exports.eq = eq; 70 | /* Not a pure module */ 71 | -------------------------------------------------------------------------------- /bindings/bs-string/lib/ocaml/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rescript-lang/bucklescript-addons/c544020712b9507b8b4900444320c2448f9c7ae5/bindings/bs-string/lib/ocaml/.gitignore -------------------------------------------------------------------------------- /bindings/bs-string/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bs-string", 3 | "version": "0.1.3", 4 | "description": "A minimal binding to mochajs for bucklescript", 5 | "scripts": { 6 | "postinstall": "bsb", 7 | "test" : "bsc -bs-package-name $npm_package_name -I src -bs-package-include bs-mocha -I test -bs-files test/*.ml && mocha test/*.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/bloomberg/bucklescript-addons.git" 12 | }, 13 | "keywords": [ 14 | "ocaml", 15 | "bucklescript", 16 | "map" 17 | ], 18 | "author": "Hongbo Zhang", 19 | "license": "ISC", 20 | "bugs": { 21 | "url": "https://github.com/bloomberg/bucklescript-addons/issues" 22 | }, 23 | "homepage": "https://github.com/bloomberg/bucklescript-addons#readme", 24 | "dependencies": { 25 | "bs-mocha": "0.1.5", 26 | "bs-platform": "0.9.3" 27 | }, 28 | "devDependencies": { 29 | "mocha": "^2.5.3" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /bindings/bs-string/scripts/install.js: -------------------------------------------------------------------------------- 1 | 2 | // TODO bsc compiler should default 3 | // to such variables when it is set 4 | 5 | var fs = require('fs') 6 | var child_process = require('child_process') 7 | var process = global.process 8 | var path = require('path') 9 | var src_dir = "src" 10 | 11 | 12 | if (!String.prototype.endsWith) { 13 | String.prototype.endsWith = function(searchString, position) { 14 | var subjectString = this.toString(); 15 | if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) { 16 | position = subjectString.length; 17 | } 18 | position -= searchString.length; 19 | var lastIndex = subjectString.indexOf(searchString, position); 20 | return lastIndex !== -1 && lastIndex === position; 21 | }; 22 | } 23 | // TODO: to work with 0.12 24 | // polyfil endswith 25 | var files = 26 | fs 27 | .readdirSync('./src') 28 | .filter(function(x){return x.endsWith('.ml') || x.endsWith('.mli')}) 29 | .map (function(x){return path.join(src_dir,x)}) 30 | 31 | console.log("files", files ); 32 | 33 | var npm_package_name = process.env['npm_package_name'] 34 | 35 | var command = 36 | "bsc -bs-package-name " + npm_package_name + 37 | " -bs-package-output lib/js -bs-package-output amdjs:lib/amdjs -bs-package-output goog:lib/goog " + 38 | "-I " + src_dir + 39 | " -c -bs-files " + 40 | files.join(" ") 41 | console.log("command", command); 42 | child_process.execSync(command); 43 | 44 | 45 | child_process.execSync("cp ./src/*.cm* ./lib/ocaml") -------------------------------------------------------------------------------- /bindings/bs-string/src/bs_array.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2015-2016 Bloomberg Finance L.P. 2 | * 3 | * This program is free software: you can redistribute it and/or modify 4 | * it under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or 6 | * (at your option) any later version. 7 | * 8 | * In addition to the permissions granted to you by the LGPL, you may combine 9 | * or link a "work that uses the Library" with a publicly distributed version 10 | * of this file to produce a combined library or application, then distribute 11 | * that combined work under the terms of your choosing, with no requirement 12 | * to comply with the obligations normally placed on you by section 4 of the 13 | * LGPL version 3 (or the corresponding section of a later version of the LGPL 14 | * should you choose to use a later version). 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with this program; if not, write to the Free Software 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) 24 | 25 | type 'a t = 'a Js_array.t 26 | 27 | external map : 'a t -> ('a -> 'b [@bs]) -> 'b t = 28 | "map" [@@bs.send] 29 | 30 | external reduce : 'a t -> ('acc -> 'a -> 'acc [@bs]) -> 'acc -> 'acc = 31 | "reduce" [@@bs.send] 32 | 33 | external push : 'a t -> 'a -> int = "push" [@@bs.send] 34 | -------------------------------------------------------------------------------- /bindings/bs-string/src/bs_math.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2015-2016 Bloomberg Finance L.P. 2 | * 3 | * This program is free software: you can redistribute it and/or modify 4 | * it under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or 6 | * (at your option) any later version. 7 | * 8 | * In addition to the permissions granted to you by the LGPL, you may combine 9 | * or link a "work that uses the Library" with a publicly distributed version 10 | * of this file to produce a combined library or application, then distribute 11 | * that combined work under the terms of your choosing, with no requirement 12 | * to comply with the obligations normally placed on you by section 4 of the 13 | * LGPL version 3 (or the corresponding section of a later version of the LGPL 14 | * should you choose to use a later version). 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with this program; if not, write to the Free Software 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) 24 | 25 | 26 | external max_int : int -> int -> int = "Math.max" [@@bs.val] 27 | external max_float : float -> float -> float = "Math.max" [@@bs.val] 28 | -------------------------------------------------------------------------------- /bindings/bs-string/src/bs_nativeint.ml: -------------------------------------------------------------------------------- 1 | 2 | 3 | type t = nativeint 4 | 5 | 6 | external parseInt : string -> int -> t = ""[@@bs.val] 7 | 8 | (* local variables: *) 9 | (* compile-command: "npm run postinstall" *) 10 | (* end: *) 11 | -------------------------------------------------------------------------------- /bindings/bs-string/src/bs_string.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.8.2 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | 6 | /* No side effect */ 7 | -------------------------------------------------------------------------------- /bindings/bs-string/src/bs_string.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2015-2016 Bloomberg Finance L.P. 2 | * 3 | * This program is free software: you can redistribute it and/or modify 4 | * it under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or 6 | * (at your option) any later version. 7 | * 8 | * In addition to the permissions granted to you by the LGPL, you may combine 9 | * or link a "work that uses the Library" with a publicly distributed version 10 | * of this file to produce a combined library or application, then distribute 11 | * that combined work under the terms of your choosing, with no requirement 12 | * to comply with the obligations normally placed on you by section 4 of the 13 | * LGPL version 3 (or the corresponding section of a later version of the LGPL 14 | * should you choose to use a later version). 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with this program; if not, write to the Free Software 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) 24 | 25 | type t = Js_string.t 26 | 27 | external slice : 28 | t -> int -> t = 29 | "slice" [@@bs.send] 30 | 31 | external slice_until : 32 | t -> int -> int -> t = 33 | "slice" [@@bs.send] 34 | 35 | external split : 36 | t -> Js_re.t -> t Js_array.t = 37 | "split" [@@bs.send] 38 | 39 | external split_limit : 40 | t -> Js_re.t -> int -> t Js_array.t = 41 | "split" [@@bs.send] 42 | 43 | external split_by_string : 44 | t -> t -> t Js_array.t = 45 | "split" [@@bs.send] 46 | 47 | 48 | external of_any : 'a -> t = "js_anything_to_string" 49 | 50 | let ascii_explode s = split_by_string s "" 51 | 52 | external charCodeAt : t -> int -> int = 53 | "charCodeAt" [@@bs.send] 54 | -------------------------------------------------------------------------------- /bindings/bs-string/test/bs_string_test.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | var Pervasives = require("bs-platform/lib/js/pervasives"); 5 | var Bs_mocha = require("bs-mocha/lib/js/bs_mocha"); 6 | var Block = require("bs-platform/lib/js/block"); 7 | var Bs_string = require("../lib/js/bs_string"); 8 | 9 | var suites = [/* [] */0]; 10 | 11 | var test_id = [0]; 12 | 13 | function eq(loc, param) { 14 | var y = param[1]; 15 | var x = param[0]; 16 | test_id[0] = test_id[0] + 1 | 0; 17 | suites[0] = /* :: */[ 18 | /* tuple */[ 19 | loc + (" id " + Pervasives.string_of_int(test_id[0])), 20 | function () { 21 | return /* Eq */Block.__(0, [ 22 | x, 23 | y 24 | ]); 25 | } 26 | ], 27 | suites[0] 28 | ]; 29 | return /* () */0; 30 | } 31 | 32 | eq('File "test/bs_string_test.ml", line 14, characters 7-14', /* tuple */[ 33 | "hihi".slice(2), 34 | "hi" 35 | ]); 36 | 37 | eq('File "test/bs_string_test.ml", line 15, characters 7-14', /* tuple */[ 38 | " The quick brown fox jumps over the lazy dog. ".split((/\s+/)), 39 | /* array */[ 40 | "", 41 | "The", 42 | "quick", 43 | "brown", 44 | "fox", 45 | "jumps", 46 | "over", 47 | "the", 48 | "lazy", 49 | "dog.", 50 | "" 51 | ] 52 | ]); 53 | 54 | eq('File "test/bs_string_test.ml", line 30, characters 7-14', /* tuple */[ 55 | /* array */[ 56 | "a", 57 | "b", 58 | "c", 59 | ";", 60 | "d" 61 | ], 62 | Bs_string.ascii_explode("abc;d") 63 | ]); 64 | 65 | Bs_mocha.from_pair_suites("test/bs_string_test.ml", suites[0]); 66 | 67 | exports.suites = suites; 68 | exports.test_id = test_id; 69 | exports.eq = eq; 70 | /* Not a pure module */ 71 | -------------------------------------------------------------------------------- /bindings/bs-string/test/bs_string_test.ml: -------------------------------------------------------------------------------- 1 | 2 | 3 | let suites : Bs_mocha.pair_suites ref = ref [] 4 | let test_id = ref 0 5 | let eq loc (x, y) = 6 | incr test_id ; 7 | suites := 8 | (loc ^" id " ^ (string_of_int !test_id), (fun [@bs] () -> Bs_mocha.Eq(x,y))) :: !suites 9 | 10 | 11 | 12 | let () = 13 | begin 14 | eq __LOC__ (Bs_string.slice "hihi" 2, "hi"); 15 | eq __LOC__ ( 16 | Bs_string.split " The quick brown fox jumps over the lazy dog. " 17 | [%bs.re {|/\s+/|}], 18 | [| ""; 19 | "The"; 20 | "quick"; 21 | "brown"; 22 | "fox"; 23 | "jumps"; 24 | "over"; 25 | "the"; 26 | "lazy"; 27 | "dog."; 28 | "" |] 29 | ); 30 | eq __LOC__ ([|"a";"b"; "c"; ";"; "d"|], Bs_string.ascii_explode "abc;d") 31 | end 32 | 33 | 34 | let () = 35 | Bs_mocha.from_pair_suites __FILE__ !suites 36 | -------------------------------------------------------------------------------- /examples/bs-readline/bsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "bs-readline", 3 | "version": "0.1.2", 4 | "ocaml-config": { 5 | "sources": [ 6 | {"dir": "src", 7 | "public":"all", 8 | "files": [ 9 | "bs_process.ml", 10 | // TODO: sanity check, check if the file exist 11 | "bs_readline_test.ml", 12 | "bs_stream.ml" 13 | ] 14 | } 15 | ] 16 | } 17 | } -------------------------------------------------------------------------------- /examples/bs-readline/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bs-readline-example", 3 | "version": "0.1.1", 4 | "description": "Example for using bucklescript do nodejs io", 5 | "scripts": { 6 | "build": "bsc -annot -bs-no-any-assert -bs-package-include bs-string -c -I src -bs-files src/*.ml" 7 | }, 8 | "repository": { 9 | "type": "git", 10 | "url": "git+https://github.com/bloomberg/bucklescript-addons.git" 11 | }, 12 | "keywords": [ 13 | "ocaml", 14 | "bucklescript", 15 | "map" 16 | ], 17 | "author": "Hongbo Zhang", 18 | "license": "ISC", 19 | "bugs": { 20 | "url": "https://github.com/bloomberg/bucklescript-addons/issues" 21 | }, 22 | "homepage": "https://github.com/bloomberg/bucklescript-addons#readme", 23 | "dependencies": { 24 | "bs-platform" : "0.9.3", 25 | "bs-string" : "0.1.1" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /examples/bs-readline/src/bs_process.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | 6 | /* No side effect */ 7 | -------------------------------------------------------------------------------- /examples/bs-readline/src/bs_process.ml: -------------------------------------------------------------------------------- 1 | 2 | 3 | (* external stdin : Bs_stream.readable = *) 4 | (* "stdin" [@@bs.val] [@@bs.module "process"] *) 5 | (* external stdout : Bs_stream.writable = *) 6 | (* "stdout" [@@bs.val] [@@bs.module "process"] *) 7 | 8 | (* external exit : int -> unit = "exit" [@@bs.call] [@@bs.module "process"] *) 9 | 10 | 11 | external process : 12 | [%bs.obj: < 13 | stdin : Bs_stream.readable; 14 | stdout : Bs_stream.writable; 15 | exit : int -> unit [@bs.meth]; 16 | 17 | > ] = "global.process" [@@bs.val] 18 | (** ideas 19 | {[ 20 | module%bs Process : sig 21 | val stdin : Bs_stream.readable 22 | val stdout : Bs_stream.writable 23 | val exit : int -> unit 24 | end 25 | ]} 26 | *) 27 | -------------------------------------------------------------------------------- /examples/bs-readline/src/bs_readline.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | 6 | /* No side effect */ 7 | -------------------------------------------------------------------------------- /examples/bs-readline/src/bs_readline.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2015-2016 Bloomberg Finance L.P. 2 | * 3 | * This program is free software: you can redistribute it and/or modify 4 | * it under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or 6 | * (at your option) any later version. 7 | * 8 | * In addition to the permissions granted to you by the LGPL, you may combine 9 | * or link a "work that uses the Library" with a publicly distributed version 10 | * of this file to produce a combined library or application, then distribute 11 | * that combined work under the terms of your choosing, with no requirement 12 | * to comply with the obligations normally placed on you by section 4 of the 13 | * LGPL version 3 (or the corresponding section of a later version of the LGPL 14 | * should you choose to use a later version). 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with this program; if not, write to the Free Software 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) 24 | 25 | 26 | (* https://github.com/typed-typings/env-node/blob/master/4/node.d.ts#L1018 *) 27 | type t 28 | type readline_options 29 | type readable_stream 30 | type writable_stream 31 | 32 | external from_options : 33 | input:Bs_stream.readable -> 34 | output:Bs_stream.writable -> 35 | terminal:Js.boolean -> unit -> readline_options = "" [@@bs.obj] 36 | 37 | external createInterface : readline_options -> t = 38 | "createInterface"[@@bs.call] [@@bs.module "readline"] 39 | 40 | (** 41 | see typescript 42 | {[ 43 | interface EventEmitter { 44 | on (event : string, listen : Function) 45 | } 46 | ]} 47 | This is weakly typed, the sub 48 | https://github.com/typed-typings/env-node/blob/master/4/node.d.ts#L296 49 | *) 50 | (** ideas : 51 | {[ < on_line : [`line] -> .. 52 | ]} 53 | since it's only one option, the inferred type would be 54 | 55 | {[ 56 | external on_line : callback -> 57 | ]} 58 | 59 | *) 60 | -------------------------------------------------------------------------------- /examples/bs-readline/src/bs_readline_test.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | var Readline = require("readline"); 5 | 6 | var rl = Readline.createInterface({ 7 | input: global.process.stdin, 8 | output: global.process.stdout, 9 | terminal: false 10 | }); 11 | 12 | function process(str) { 13 | var digits = str.split("").map(function (x) { 14 | var v = x.charCodeAt(0); 15 | if (v >= /* "A" */65) { 16 | return ((v - /* "A" */65 | 0) + 10 | 0) + 1 | 0; 17 | } 18 | else { 19 | return (v - /* "0" */48 | 0) + 1 | 0; 20 | } 21 | }); 22 | return digits.reduce(function (acc, a) { 23 | return Math.max(acc, a); 24 | }, 1); 25 | } 26 | 27 | var lines = /* array */[]; 28 | 29 | rl.on("line", function (line) { 30 | lines.push(line); 31 | return /* () */0; 32 | }); 33 | 34 | rl.on("close", function () { 35 | var lines$1 = lines; 36 | if (lines$1.length !== 2) { 37 | return /* impossible branch */0; 38 | } 39 | else { 40 | var firstline = lines$1[0]; 41 | var secondline = lines$1[1]; 42 | var match_000 = process(firstline); 43 | var match_001 = process(secondline); 44 | var first = parseInt(firstline, match_000); 45 | var second = parseInt(secondline, match_001); 46 | global.process.stdout.write("" + (first + second)); 47 | return /* () */0; 48 | } 49 | }); 50 | 51 | /* rl Not a pure module */ 52 | -------------------------------------------------------------------------------- /examples/bs-readline/src/bs_readline_test.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2015-2016 Bloomberg Finance L.P. 2 | * 3 | * This program is free software: you can redistribute it and/or modify 4 | * it under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or 6 | * (at your option) any later version. 7 | * 8 | * In addition to the permissions granted to you by the LGPL, you may combine 9 | * or link a "work that uses the Library" with a publicly distributed version 10 | * of this file to produce a combined library or application, then distribute 11 | * that combined work under the terms of your choosing, with no requirement 12 | * to comply with the obligations normally placed on you by section 4 of the 13 | * LGPL version 3 (or the corresponding section of a later version of the LGPL 14 | * should you choose to use a later version). 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with this program; if not, write to the Free Software 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) 24 | 25 | [@@@bs.config{no_export}] 26 | 27 | let first_line = ref true 28 | 29 | let rl = Bs_readline.createInterface 30 | (Bs_readline.from_options 31 | ~input:Bs_process.process##stdin 32 | ~output:Bs_process.process##stdout 33 | ~terminal:Js.false_ ()) 34 | 35 | 36 | external on : Bs_readline.t -> 37 | ([ 38 | | `line of (string->unit [@bs]) 39 | | `close of (unit -> unit [@bs]) ] [@bs.string]) -> unit = "" [@@bs.send] 40 | 41 | 42 | 43 | let a_char_code = Char.code 'A' 44 | let zero_char_code = Char.code '0' 45 | let process str = 46 | let digits = 47 | Bs_array.map (Bs_string.split_by_string str "") 48 | (fun[@bs] x -> 49 | let v = Bs_string.charCodeAt x 0 in 50 | if v >= a_char_code then v - a_char_code + 10 + 1 51 | else 52 | v - zero_char_code + 1 ) 53 | in 54 | Bs_array.reduce digits (fun[@bs] acc a -> Bs_math.max_int acc a ) 1 55 | 56 | let lines : string Bs_array.t = [||] 57 | 58 | let process_lines lines = 59 | match lines with 60 | | [| firstline; secondline|] 61 | -> let a , b = process firstline , process secondline in 62 | let first = (Bs_nativeint.parseInt firstline a) in 63 | let second = ( Bs_nativeint.parseInt secondline b) in 64 | (* Js.log (first, second) *) 65 | (* Js.log ( Nativeint.add first second ) *) 66 | ignore (Bs_process.process##stdout##write (Bs_string.of_any ( Nativeint.add first second ))) 67 | | _ -> assert false 68 | 69 | 70 | let () = 71 | on rl (`line (fun [@bs] line -> 72 | ignore (Bs_array.push lines line) 73 | )); 74 | on rl (`close (fun [@bs] () -> 75 | process_lines lines 76 | )) 77 | 78 | (* 79 | let () = 80 | Bs_readline.on_line rl begin fun [@bs] line -> 81 | if !first_line then 82 | first_line:= false 83 | else 84 | begin 85 | Js.log line; 86 | Js.log 42 87 | end 88 | end; 89 | Bs_readline.on_close rl begin fun[@bs] () -> 90 | Bs_process.exit 0 91 | end 92 | *) 93 | 94 | (* local variables: *) 95 | (* compile-command: "npm --no-color run build" *) 96 | (* end: *) 97 | -------------------------------------------------------------------------------- /examples/bs-readline/src/bs_stream.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | 6 | /* No side effect */ 7 | -------------------------------------------------------------------------------- /examples/bs-readline/src/bs_stream.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2015-2016 Bloomberg Finance L.P. 2 | * 3 | * This program is free software: you can redistribute it and/or modify 4 | * it under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or 6 | * (at your option) any later version. 7 | * 8 | * In addition to the permissions granted to you by the LGPL, you may combine 9 | * or link a "work that uses the Library" with a publicly distributed version 10 | * of this file to produce a combined library or application, then distribute 11 | * that combined work under the terms of your choosing, with no requirement 12 | * to comply with the obligations normally placed on you by section 4 of the 13 | * LGPL version 3 (or the corresponding section of a later version of the LGPL 14 | * should you choose to use a later version). 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with this program; if not, write to the Free Software 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) 24 | 25 | type readable 26 | 27 | type writable = [%bs.obj: < 28 | write : Bs_string.t -> Js.boolean [@bs.meth] 29 | > ] 30 | -------------------------------------------------------------------------------- /examples/bs-string-map/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | String Map implementation using Bucklescript 4 | -------------------------------------------------------------------------------- /examples/bs-string-map/lib/amdjs/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rescript-lang/bucklescript-addons/c544020712b9507b8b4900444320c2448f9c7ae5/examples/bs-string-map/lib/amdjs/.gitignore -------------------------------------------------------------------------------- /examples/bs-string-map/lib/amdjs/string_map.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.7.1 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | define(["exports", "bs-platform/lib/amdjs/caml_builtin_exceptions", "bs-platform/lib/amdjs/pervasives", "bs-platform/lib/amdjs/string"], 4 | function(exports, Caml_builtin_exceptions, Pervasives, $$String){ 5 | 'use strict'; 6 | function height(param) { 7 | if (param) { 8 | return param[4]; 9 | } 10 | else { 11 | return 0; 12 | } 13 | } 14 | 15 | function create(l, x, d, r) { 16 | var hl = height(l); 17 | var hr = height(r); 18 | return /* Node */[ 19 | l, 20 | x, 21 | d, 22 | r, 23 | hl >= hr ? hl + 1 | 0 : hr + 1 | 0 24 | ]; 25 | } 26 | 27 | function singleton(x, d) { 28 | return /* Node */[ 29 | /* Empty */0, 30 | x, 31 | d, 32 | /* Empty */0, 33 | 1 34 | ]; 35 | } 36 | 37 | function bal(l, x, d, r) { 38 | var hl = l ? l[4] : 0; 39 | var hr = r ? r[4] : 0; 40 | if (hl > (hr + 2 | 0)) { 41 | if (l) { 42 | var lr = l[3]; 43 | var ld = l[2]; 44 | var lv = l[1]; 45 | var ll = l[0]; 46 | if (height(ll) >= height(lr)) { 47 | return create(ll, lv, ld, create(lr, x, d, r)); 48 | } 49 | else if (lr) { 50 | return create(create(ll, lv, ld, lr[0]), lr[1], lr[2], create(lr[3], x, d, r)); 51 | } 52 | else { 53 | return Pervasives.invalid_arg("Map.bal"); 54 | } 55 | } 56 | else { 57 | return Pervasives.invalid_arg("Map.bal"); 58 | } 59 | } 60 | else if (hr > (hl + 2 | 0)) { 61 | if (r) { 62 | var rr = r[3]; 63 | var rd = r[2]; 64 | var rv = r[1]; 65 | var rl = r[0]; 66 | if (height(rr) >= height(rl)) { 67 | return create(create(l, x, d, rl), rv, rd, rr); 68 | } 69 | else if (rl) { 70 | return create(create(l, x, d, rl[0]), rl[1], rl[2], create(rl[3], rv, rd, rr)); 71 | } 72 | else { 73 | return Pervasives.invalid_arg("Map.bal"); 74 | } 75 | } 76 | else { 77 | return Pervasives.invalid_arg("Map.bal"); 78 | } 79 | } 80 | else { 81 | return /* Node */[ 82 | l, 83 | x, 84 | d, 85 | r, 86 | hl >= hr ? hl + 1 | 0 : hr + 1 | 0 87 | ]; 88 | } 89 | } 90 | 91 | function is_empty(param) { 92 | if (param) { 93 | return /* false */0; 94 | } 95 | else { 96 | return /* true */1; 97 | } 98 | } 99 | 100 | function add(x, data, param) { 101 | if (param) { 102 | var r = param[3]; 103 | var d = param[2]; 104 | var v = param[1]; 105 | var l = param[0]; 106 | var c = $$String.compare(x, v); 107 | if (c) { 108 | if (c < 0) { 109 | return bal(add(x, data, l), v, d, r); 110 | } 111 | else { 112 | return bal(l, v, d, add(x, data, r)); 113 | } 114 | } 115 | else { 116 | return /* Node */[ 117 | l, 118 | x, 119 | data, 120 | r, 121 | param[4] 122 | ]; 123 | } 124 | } 125 | else { 126 | return /* Node */[ 127 | /* Empty */0, 128 | x, 129 | data, 130 | /* Empty */0, 131 | 1 132 | ]; 133 | } 134 | } 135 | 136 | function find(x, _param) { 137 | while(true) { 138 | var param = _param; 139 | if (param) { 140 | var c = $$String.compare(x, param[1]); 141 | if (c) { 142 | _param = c < 0 ? param[0] : param[3]; 143 | continue ; 144 | 145 | } 146 | else { 147 | return param[2]; 148 | } 149 | } 150 | else { 151 | throw Caml_builtin_exceptions.not_found; 152 | } 153 | }; 154 | } 155 | 156 | function mem(x, _param) { 157 | while(true) { 158 | var param = _param; 159 | if (param) { 160 | var c = $$String.compare(x, param[1]); 161 | if (c) { 162 | _param = c < 0 ? param[0] : param[3]; 163 | continue ; 164 | 165 | } 166 | else { 167 | return /* true */1; 168 | } 169 | } 170 | else { 171 | return /* false */0; 172 | } 173 | }; 174 | } 175 | 176 | function min_binding(_param) { 177 | while(true) { 178 | var param = _param; 179 | if (param) { 180 | var l = param[0]; 181 | if (l) { 182 | _param = l; 183 | continue ; 184 | 185 | } 186 | else { 187 | return /* tuple */[ 188 | param[1], 189 | param[2] 190 | ]; 191 | } 192 | } 193 | else { 194 | throw Caml_builtin_exceptions.not_found; 195 | } 196 | }; 197 | } 198 | 199 | function max_binding(_param) { 200 | while(true) { 201 | var param = _param; 202 | if (param) { 203 | var r = param[3]; 204 | if (r) { 205 | _param = r; 206 | continue ; 207 | 208 | } 209 | else { 210 | return /* tuple */[ 211 | param[1], 212 | param[2] 213 | ]; 214 | } 215 | } 216 | else { 217 | throw Caml_builtin_exceptions.not_found; 218 | } 219 | }; 220 | } 221 | 222 | function remove_min_binding(param) { 223 | if (param) { 224 | var l = param[0]; 225 | if (l) { 226 | return bal(remove_min_binding(l), param[1], param[2], param[3]); 227 | } 228 | else { 229 | return param[3]; 230 | } 231 | } 232 | else { 233 | return Pervasives.invalid_arg("Map.remove_min_elt"); 234 | } 235 | } 236 | 237 | function remove(x, param) { 238 | if (param) { 239 | var r = param[3]; 240 | var d = param[2]; 241 | var v = param[1]; 242 | var l = param[0]; 243 | var c = $$String.compare(x, v); 244 | if (c) { 245 | if (c < 0) { 246 | return bal(remove(x, l), v, d, r); 247 | } 248 | else { 249 | return bal(l, v, d, remove(x, r)); 250 | } 251 | } 252 | else { 253 | var t1 = l; 254 | var t2 = r; 255 | if (t1) { 256 | if (t2) { 257 | var match = min_binding(t2); 258 | return bal(t1, match[0], match[1], remove_min_binding(t2)); 259 | } 260 | else { 261 | return t1; 262 | } 263 | } 264 | else { 265 | return t2; 266 | } 267 | } 268 | } 269 | else { 270 | return /* Empty */0; 271 | } 272 | } 273 | 274 | function iter(f, _param) { 275 | while(true) { 276 | var param = _param; 277 | if (param) { 278 | iter(f, param[0]); 279 | f(param[1], param[2]); 280 | _param = param[3]; 281 | continue ; 282 | 283 | } 284 | else { 285 | return /* () */0; 286 | } 287 | }; 288 | } 289 | 290 | function map(f, param) { 291 | if (param) { 292 | var l$prime = map(f, param[0]); 293 | var d$prime = f(param[2]); 294 | var r$prime = map(f, param[3]); 295 | return /* Node */[ 296 | l$prime, 297 | param[1], 298 | d$prime, 299 | r$prime, 300 | param[4] 301 | ]; 302 | } 303 | else { 304 | return /* Empty */0; 305 | } 306 | } 307 | 308 | function mapi(f, param) { 309 | if (param) { 310 | var v = param[1]; 311 | var l$prime = mapi(f, param[0]); 312 | var d$prime = f(v, param[2]); 313 | var r$prime = mapi(f, param[3]); 314 | return /* Node */[ 315 | l$prime, 316 | v, 317 | d$prime, 318 | r$prime, 319 | param[4] 320 | ]; 321 | } 322 | else { 323 | return /* Empty */0; 324 | } 325 | } 326 | 327 | function fold(f, _m, _accu) { 328 | while(true) { 329 | var accu = _accu; 330 | var m = _m; 331 | if (m) { 332 | _accu = f(m[1], m[2], fold(f, m[0], accu)); 333 | _m = m[3]; 334 | continue ; 335 | 336 | } 337 | else { 338 | return accu; 339 | } 340 | }; 341 | } 342 | 343 | function for_all(p, _param) { 344 | while(true) { 345 | var param = _param; 346 | if (param) { 347 | if (p(param[1], param[2])) { 348 | if (for_all(p, param[0])) { 349 | _param = param[3]; 350 | continue ; 351 | 352 | } 353 | else { 354 | return /* false */0; 355 | } 356 | } 357 | else { 358 | return /* false */0; 359 | } 360 | } 361 | else { 362 | return /* true */1; 363 | } 364 | }; 365 | } 366 | 367 | function exists(p, _param) { 368 | while(true) { 369 | var param = _param; 370 | if (param) { 371 | if (p(param[1], param[2])) { 372 | return /* true */1; 373 | } 374 | else if (exists(p, param[0])) { 375 | return /* true */1; 376 | } 377 | else { 378 | _param = param[3]; 379 | continue ; 380 | 381 | } 382 | } 383 | else { 384 | return /* false */0; 385 | } 386 | }; 387 | } 388 | 389 | function add_min_binding(k, v, param) { 390 | if (param) { 391 | return bal(add_min_binding(k, v, param[0]), param[1], param[2], param[3]); 392 | } 393 | else { 394 | return singleton(k, v); 395 | } 396 | } 397 | 398 | function add_max_binding(k, v, param) { 399 | if (param) { 400 | return bal(param[0], param[1], param[2], add_max_binding(k, v, param[3])); 401 | } 402 | else { 403 | return singleton(k, v); 404 | } 405 | } 406 | 407 | function join(l, v, d, r) { 408 | if (l) { 409 | if (r) { 410 | var rh = r[4]; 411 | var lh = l[4]; 412 | if (lh > (rh + 2 | 0)) { 413 | return bal(l[0], l[1], l[2], join(l[3], v, d, r)); 414 | } 415 | else if (rh > (lh + 2 | 0)) { 416 | return bal(join(l, v, d, r[0]), r[1], r[2], r[3]); 417 | } 418 | else { 419 | return create(l, v, d, r); 420 | } 421 | } 422 | else { 423 | return add_max_binding(v, d, l); 424 | } 425 | } 426 | else { 427 | return add_min_binding(v, d, r); 428 | } 429 | } 430 | 431 | function concat(t1, t2) { 432 | if (t1) { 433 | if (t2) { 434 | var match = min_binding(t2); 435 | return join(t1, match[0], match[1], remove_min_binding(t2)); 436 | } 437 | else { 438 | return t1; 439 | } 440 | } 441 | else { 442 | return t2; 443 | } 444 | } 445 | 446 | function concat_or_join(t1, v, d, t2) { 447 | if (d) { 448 | return join(t1, v, d[0], t2); 449 | } 450 | else { 451 | return concat(t1, t2); 452 | } 453 | } 454 | 455 | function split(x, param) { 456 | if (param) { 457 | var r = param[3]; 458 | var d = param[2]; 459 | var v = param[1]; 460 | var l = param[0]; 461 | var c = $$String.compare(x, v); 462 | if (c) { 463 | if (c < 0) { 464 | var match = split(x, l); 465 | return /* tuple */[ 466 | match[0], 467 | match[1], 468 | join(match[2], v, d, r) 469 | ]; 470 | } 471 | else { 472 | var match$1 = split(x, r); 473 | return /* tuple */[ 474 | join(l, v, d, match$1[0]), 475 | match$1[1], 476 | match$1[2] 477 | ]; 478 | } 479 | } 480 | else { 481 | return /* tuple */[ 482 | l, 483 | /* Some */[d], 484 | r 485 | ]; 486 | } 487 | } 488 | else { 489 | return /* tuple */[ 490 | /* Empty */0, 491 | /* None */0, 492 | /* Empty */0 493 | ]; 494 | } 495 | } 496 | 497 | function merge(f, s1, s2) { 498 | var exit = 0; 499 | if (s1) { 500 | var v1 = s1[1]; 501 | if (s1[4] >= height(s2)) { 502 | var match = split(v1, s2); 503 | return concat_or_join(merge(f, s1[0], match[0]), v1, f(v1, /* Some */[s1[2]], match[1]), merge(f, s1[3], match[2])); 504 | } 505 | else { 506 | exit = 1; 507 | } 508 | } 509 | else if (s2) { 510 | exit = 1; 511 | } 512 | else { 513 | return /* Empty */0; 514 | } 515 | if (exit === 1) { 516 | if (s2) { 517 | var v2 = s2[1]; 518 | var match$1 = split(v2, s1); 519 | return concat_or_join(merge(f, match$1[0], s2[0]), v2, f(v2, match$1[1], /* Some */[s2[2]]), merge(f, match$1[2], s2[3])); 520 | } 521 | else { 522 | return /* impossible branch */0; 523 | } 524 | } 525 | 526 | } 527 | 528 | function filter(p, param) { 529 | if (param) { 530 | var d = param[2]; 531 | var v = param[1]; 532 | var l$prime = filter(p, param[0]); 533 | var pvd = p(v, d); 534 | var r$prime = filter(p, param[3]); 535 | if (pvd) { 536 | return join(l$prime, v, d, r$prime); 537 | } 538 | else { 539 | return concat(l$prime, r$prime); 540 | } 541 | } 542 | else { 543 | return /* Empty */0; 544 | } 545 | } 546 | 547 | function partition(p, param) { 548 | if (param) { 549 | var d = param[2]; 550 | var v = param[1]; 551 | var match = partition(p, param[0]); 552 | var lf = match[1]; 553 | var lt = match[0]; 554 | var pvd = p(v, d); 555 | var match$1 = partition(p, param[3]); 556 | var rf = match$1[1]; 557 | var rt = match$1[0]; 558 | if (pvd) { 559 | return /* tuple */[ 560 | join(lt, v, d, rt), 561 | concat(lf, rf) 562 | ]; 563 | } 564 | else { 565 | return /* tuple */[ 566 | concat(lt, rt), 567 | join(lf, v, d, rf) 568 | ]; 569 | } 570 | } 571 | else { 572 | return /* tuple */[ 573 | /* Empty */0, 574 | /* Empty */0 575 | ]; 576 | } 577 | } 578 | 579 | function cons_enum(_m, _e) { 580 | while(true) { 581 | var e = _e; 582 | var m = _m; 583 | if (m) { 584 | _e = /* More */[ 585 | m[1], 586 | m[2], 587 | m[3], 588 | e 589 | ]; 590 | _m = m[0]; 591 | continue ; 592 | 593 | } 594 | else { 595 | return e; 596 | } 597 | }; 598 | } 599 | 600 | function compare(cmp, m1, m2) { 601 | var _e1 = cons_enum(m1, /* End */0); 602 | var _e2 = cons_enum(m2, /* End */0); 603 | while(true) { 604 | var e2 = _e2; 605 | var e1 = _e1; 606 | if (e1) { 607 | if (e2) { 608 | var c = $$String.compare(e1[0], e2[0]); 609 | if (c !== 0) { 610 | return c; 611 | } 612 | else { 613 | var c$1 = cmp(e1[1], e2[1]); 614 | if (c$1 !== 0) { 615 | return c$1; 616 | } 617 | else { 618 | _e2 = cons_enum(e2[2], e2[3]); 619 | _e1 = cons_enum(e1[2], e1[3]); 620 | continue ; 621 | 622 | } 623 | } 624 | } 625 | else { 626 | return 1; 627 | } 628 | } 629 | else if (e2) { 630 | return -1; 631 | } 632 | else { 633 | return 0; 634 | } 635 | }; 636 | } 637 | 638 | function equal(cmp, m1, m2) { 639 | var _e1 = cons_enum(m1, /* End */0); 640 | var _e2 = cons_enum(m2, /* End */0); 641 | while(true) { 642 | var e2 = _e2; 643 | var e1 = _e1; 644 | if (e1) { 645 | if (e2) { 646 | if ($$String.compare(e1[0], e2[0])) { 647 | return /* false */0; 648 | } 649 | else if (cmp(e1[1], e2[1])) { 650 | _e2 = cons_enum(e2[2], e2[3]); 651 | _e1 = cons_enum(e1[2], e1[3]); 652 | continue ; 653 | 654 | } 655 | else { 656 | return /* false */0; 657 | } 658 | } 659 | else { 660 | return /* false */0; 661 | } 662 | } 663 | else if (e2) { 664 | return /* false */0; 665 | } 666 | else { 667 | return /* true */1; 668 | } 669 | }; 670 | } 671 | 672 | function cardinal(param) { 673 | if (param) { 674 | return (cardinal(param[0]) + 1 | 0) + cardinal(param[3]) | 0; 675 | } 676 | else { 677 | return 0; 678 | } 679 | } 680 | 681 | function bindings_aux(_accu, _param) { 682 | while(true) { 683 | var param = _param; 684 | var accu = _accu; 685 | if (param) { 686 | _param = param[0]; 687 | _accu = /* :: */[ 688 | /* tuple */[ 689 | param[1], 690 | param[2] 691 | ], 692 | bindings_aux(accu, param[3]) 693 | ]; 694 | continue ; 695 | 696 | } 697 | else { 698 | return accu; 699 | } 700 | }; 701 | } 702 | 703 | function bindings(s) { 704 | return bindings_aux(/* [] */0, s); 705 | } 706 | 707 | var empty = /* Empty */0; 708 | 709 | var choose = min_binding; 710 | 711 | exports.empty = empty; 712 | exports.is_empty = is_empty; 713 | exports.mem = mem; 714 | exports.add = add; 715 | exports.singleton = singleton; 716 | exports.remove = remove; 717 | exports.merge = merge; 718 | exports.compare = compare; 719 | exports.equal = equal; 720 | exports.iter = iter; 721 | exports.fold = fold; 722 | exports.for_all = for_all; 723 | exports.exists = exists; 724 | exports.filter = filter; 725 | exports.partition = partition; 726 | exports.cardinal = cardinal; 727 | exports.bindings = bindings; 728 | exports.min_binding = min_binding; 729 | exports.max_binding = max_binding; 730 | exports.choose = choose; 731 | exports.split = split; 732 | exports.find = find; 733 | exports.map = map; 734 | exports.mapi = mapi; 735 | 736 | }) 737 | /* No side effect */ 738 | -------------------------------------------------------------------------------- /examples/bs-string-map/lib/goog/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rescript-lang/bucklescript-addons/c544020712b9507b8b4900444320c2448f9c7ae5/examples/bs-string-map/lib/goog/.gitignore -------------------------------------------------------------------------------- /examples/bs-string-map/lib/goog/string_map.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.7.1 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | goog.module("bs-string-map.string_map"); 4 | 5 | var Caml_builtin_exceptions = goog.require("bs-platform.caml_builtin_exceptions"); 6 | var Pervasives = goog.require("bs-platform.pervasives"); 7 | var $$String = goog.require("bs-platform.string"); 8 | 9 | function height(param) { 10 | if (param) { 11 | return param[4]; 12 | } 13 | else { 14 | return 0; 15 | } 16 | } 17 | 18 | function create(l, x, d, r) { 19 | var hl = height(l); 20 | var hr = height(r); 21 | return /* Node */[ 22 | l, 23 | x, 24 | d, 25 | r, 26 | hl >= hr ? hl + 1 | 0 : hr + 1 | 0 27 | ]; 28 | } 29 | 30 | function singleton(x, d) { 31 | return /* Node */[ 32 | /* Empty */0, 33 | x, 34 | d, 35 | /* Empty */0, 36 | 1 37 | ]; 38 | } 39 | 40 | function bal(l, x, d, r) { 41 | var hl = l ? l[4] : 0; 42 | var hr = r ? r[4] : 0; 43 | if (hl > (hr + 2 | 0)) { 44 | if (l) { 45 | var lr = l[3]; 46 | var ld = l[2]; 47 | var lv = l[1]; 48 | var ll = l[0]; 49 | if (height(ll) >= height(lr)) { 50 | return create(ll, lv, ld, create(lr, x, d, r)); 51 | } 52 | else if (lr) { 53 | return create(create(ll, lv, ld, lr[0]), lr[1], lr[2], create(lr[3], x, d, r)); 54 | } 55 | else { 56 | return Pervasives.invalid_arg("Map.bal"); 57 | } 58 | } 59 | else { 60 | return Pervasives.invalid_arg("Map.bal"); 61 | } 62 | } 63 | else if (hr > (hl + 2 | 0)) { 64 | if (r) { 65 | var rr = r[3]; 66 | var rd = r[2]; 67 | var rv = r[1]; 68 | var rl = r[0]; 69 | if (height(rr) >= height(rl)) { 70 | return create(create(l, x, d, rl), rv, rd, rr); 71 | } 72 | else if (rl) { 73 | return create(create(l, x, d, rl[0]), rl[1], rl[2], create(rl[3], rv, rd, rr)); 74 | } 75 | else { 76 | return Pervasives.invalid_arg("Map.bal"); 77 | } 78 | } 79 | else { 80 | return Pervasives.invalid_arg("Map.bal"); 81 | } 82 | } 83 | else { 84 | return /* Node */[ 85 | l, 86 | x, 87 | d, 88 | r, 89 | hl >= hr ? hl + 1 | 0 : hr + 1 | 0 90 | ]; 91 | } 92 | } 93 | 94 | function is_empty(param) { 95 | if (param) { 96 | return /* false */0; 97 | } 98 | else { 99 | return /* true */1; 100 | } 101 | } 102 | 103 | function add(x, data, param) { 104 | if (param) { 105 | var r = param[3]; 106 | var d = param[2]; 107 | var v = param[1]; 108 | var l = param[0]; 109 | var c = $$String.compare(x, v); 110 | if (c) { 111 | if (c < 0) { 112 | return bal(add(x, data, l), v, d, r); 113 | } 114 | else { 115 | return bal(l, v, d, add(x, data, r)); 116 | } 117 | } 118 | else { 119 | return /* Node */[ 120 | l, 121 | x, 122 | data, 123 | r, 124 | param[4] 125 | ]; 126 | } 127 | } 128 | else { 129 | return /* Node */[ 130 | /* Empty */0, 131 | x, 132 | data, 133 | /* Empty */0, 134 | 1 135 | ]; 136 | } 137 | } 138 | 139 | function find(x, _param) { 140 | while(true) { 141 | var param = _param; 142 | if (param) { 143 | var c = $$String.compare(x, param[1]); 144 | if (c) { 145 | _param = c < 0 ? param[0] : param[3]; 146 | continue ; 147 | 148 | } 149 | else { 150 | return param[2]; 151 | } 152 | } 153 | else { 154 | throw Caml_builtin_exceptions.not_found; 155 | } 156 | }; 157 | } 158 | 159 | function mem(x, _param) { 160 | while(true) { 161 | var param = _param; 162 | if (param) { 163 | var c = $$String.compare(x, param[1]); 164 | if (c) { 165 | _param = c < 0 ? param[0] : param[3]; 166 | continue ; 167 | 168 | } 169 | else { 170 | return /* true */1; 171 | } 172 | } 173 | else { 174 | return /* false */0; 175 | } 176 | }; 177 | } 178 | 179 | function min_binding(_param) { 180 | while(true) { 181 | var param = _param; 182 | if (param) { 183 | var l = param[0]; 184 | if (l) { 185 | _param = l; 186 | continue ; 187 | 188 | } 189 | else { 190 | return /* tuple */[ 191 | param[1], 192 | param[2] 193 | ]; 194 | } 195 | } 196 | else { 197 | throw Caml_builtin_exceptions.not_found; 198 | } 199 | }; 200 | } 201 | 202 | function max_binding(_param) { 203 | while(true) { 204 | var param = _param; 205 | if (param) { 206 | var r = param[3]; 207 | if (r) { 208 | _param = r; 209 | continue ; 210 | 211 | } 212 | else { 213 | return /* tuple */[ 214 | param[1], 215 | param[2] 216 | ]; 217 | } 218 | } 219 | else { 220 | throw Caml_builtin_exceptions.not_found; 221 | } 222 | }; 223 | } 224 | 225 | function remove_min_binding(param) { 226 | if (param) { 227 | var l = param[0]; 228 | if (l) { 229 | return bal(remove_min_binding(l), param[1], param[2], param[3]); 230 | } 231 | else { 232 | return param[3]; 233 | } 234 | } 235 | else { 236 | return Pervasives.invalid_arg("Map.remove_min_elt"); 237 | } 238 | } 239 | 240 | function remove(x, param) { 241 | if (param) { 242 | var r = param[3]; 243 | var d = param[2]; 244 | var v = param[1]; 245 | var l = param[0]; 246 | var c = $$String.compare(x, v); 247 | if (c) { 248 | if (c < 0) { 249 | return bal(remove(x, l), v, d, r); 250 | } 251 | else { 252 | return bal(l, v, d, remove(x, r)); 253 | } 254 | } 255 | else { 256 | var t1 = l; 257 | var t2 = r; 258 | if (t1) { 259 | if (t2) { 260 | var match = min_binding(t2); 261 | return bal(t1, match[0], match[1], remove_min_binding(t2)); 262 | } 263 | else { 264 | return t1; 265 | } 266 | } 267 | else { 268 | return t2; 269 | } 270 | } 271 | } 272 | else { 273 | return /* Empty */0; 274 | } 275 | } 276 | 277 | function iter(f, _param) { 278 | while(true) { 279 | var param = _param; 280 | if (param) { 281 | iter(f, param[0]); 282 | f(param[1], param[2]); 283 | _param = param[3]; 284 | continue ; 285 | 286 | } 287 | else { 288 | return /* () */0; 289 | } 290 | }; 291 | } 292 | 293 | function map(f, param) { 294 | if (param) { 295 | var l$prime = map(f, param[0]); 296 | var d$prime = f(param[2]); 297 | var r$prime = map(f, param[3]); 298 | return /* Node */[ 299 | l$prime, 300 | param[1], 301 | d$prime, 302 | r$prime, 303 | param[4] 304 | ]; 305 | } 306 | else { 307 | return /* Empty */0; 308 | } 309 | } 310 | 311 | function mapi(f, param) { 312 | if (param) { 313 | var v = param[1]; 314 | var l$prime = mapi(f, param[0]); 315 | var d$prime = f(v, param[2]); 316 | var r$prime = mapi(f, param[3]); 317 | return /* Node */[ 318 | l$prime, 319 | v, 320 | d$prime, 321 | r$prime, 322 | param[4] 323 | ]; 324 | } 325 | else { 326 | return /* Empty */0; 327 | } 328 | } 329 | 330 | function fold(f, _m, _accu) { 331 | while(true) { 332 | var accu = _accu; 333 | var m = _m; 334 | if (m) { 335 | _accu = f(m[1], m[2], fold(f, m[0], accu)); 336 | _m = m[3]; 337 | continue ; 338 | 339 | } 340 | else { 341 | return accu; 342 | } 343 | }; 344 | } 345 | 346 | function for_all(p, _param) { 347 | while(true) { 348 | var param = _param; 349 | if (param) { 350 | if (p(param[1], param[2])) { 351 | if (for_all(p, param[0])) { 352 | _param = param[3]; 353 | continue ; 354 | 355 | } 356 | else { 357 | return /* false */0; 358 | } 359 | } 360 | else { 361 | return /* false */0; 362 | } 363 | } 364 | else { 365 | return /* true */1; 366 | } 367 | }; 368 | } 369 | 370 | function exists(p, _param) { 371 | while(true) { 372 | var param = _param; 373 | if (param) { 374 | if (p(param[1], param[2])) { 375 | return /* true */1; 376 | } 377 | else if (exists(p, param[0])) { 378 | return /* true */1; 379 | } 380 | else { 381 | _param = param[3]; 382 | continue ; 383 | 384 | } 385 | } 386 | else { 387 | return /* false */0; 388 | } 389 | }; 390 | } 391 | 392 | function add_min_binding(k, v, param) { 393 | if (param) { 394 | return bal(add_min_binding(k, v, param[0]), param[1], param[2], param[3]); 395 | } 396 | else { 397 | return singleton(k, v); 398 | } 399 | } 400 | 401 | function add_max_binding(k, v, param) { 402 | if (param) { 403 | return bal(param[0], param[1], param[2], add_max_binding(k, v, param[3])); 404 | } 405 | else { 406 | return singleton(k, v); 407 | } 408 | } 409 | 410 | function join(l, v, d, r) { 411 | if (l) { 412 | if (r) { 413 | var rh = r[4]; 414 | var lh = l[4]; 415 | if (lh > (rh + 2 | 0)) { 416 | return bal(l[0], l[1], l[2], join(l[3], v, d, r)); 417 | } 418 | else if (rh > (lh + 2 | 0)) { 419 | return bal(join(l, v, d, r[0]), r[1], r[2], r[3]); 420 | } 421 | else { 422 | return create(l, v, d, r); 423 | } 424 | } 425 | else { 426 | return add_max_binding(v, d, l); 427 | } 428 | } 429 | else { 430 | return add_min_binding(v, d, r); 431 | } 432 | } 433 | 434 | function concat(t1, t2) { 435 | if (t1) { 436 | if (t2) { 437 | var match = min_binding(t2); 438 | return join(t1, match[0], match[1], remove_min_binding(t2)); 439 | } 440 | else { 441 | return t1; 442 | } 443 | } 444 | else { 445 | return t2; 446 | } 447 | } 448 | 449 | function concat_or_join(t1, v, d, t2) { 450 | if (d) { 451 | return join(t1, v, d[0], t2); 452 | } 453 | else { 454 | return concat(t1, t2); 455 | } 456 | } 457 | 458 | function split(x, param) { 459 | if (param) { 460 | var r = param[3]; 461 | var d = param[2]; 462 | var v = param[1]; 463 | var l = param[0]; 464 | var c = $$String.compare(x, v); 465 | if (c) { 466 | if (c < 0) { 467 | var match = split(x, l); 468 | return /* tuple */[ 469 | match[0], 470 | match[1], 471 | join(match[2], v, d, r) 472 | ]; 473 | } 474 | else { 475 | var match$1 = split(x, r); 476 | return /* tuple */[ 477 | join(l, v, d, match$1[0]), 478 | match$1[1], 479 | match$1[2] 480 | ]; 481 | } 482 | } 483 | else { 484 | return /* tuple */[ 485 | l, 486 | /* Some */[d], 487 | r 488 | ]; 489 | } 490 | } 491 | else { 492 | return /* tuple */[ 493 | /* Empty */0, 494 | /* None */0, 495 | /* Empty */0 496 | ]; 497 | } 498 | } 499 | 500 | function merge(f, s1, s2) { 501 | var exit = 0; 502 | if (s1) { 503 | var v1 = s1[1]; 504 | if (s1[4] >= height(s2)) { 505 | var match = split(v1, s2); 506 | return concat_or_join(merge(f, s1[0], match[0]), v1, f(v1, /* Some */[s1[2]], match[1]), merge(f, s1[3], match[2])); 507 | } 508 | else { 509 | exit = 1; 510 | } 511 | } 512 | else if (s2) { 513 | exit = 1; 514 | } 515 | else { 516 | return /* Empty */0; 517 | } 518 | if (exit === 1) { 519 | if (s2) { 520 | var v2 = s2[1]; 521 | var match$1 = split(v2, s1); 522 | return concat_or_join(merge(f, match$1[0], s2[0]), v2, f(v2, match$1[1], /* Some */[s2[2]]), merge(f, match$1[2], s2[3])); 523 | } 524 | else { 525 | return /* impossible branch */0; 526 | } 527 | } 528 | 529 | } 530 | 531 | function filter(p, param) { 532 | if (param) { 533 | var d = param[2]; 534 | var v = param[1]; 535 | var l$prime = filter(p, param[0]); 536 | var pvd = p(v, d); 537 | var r$prime = filter(p, param[3]); 538 | if (pvd) { 539 | return join(l$prime, v, d, r$prime); 540 | } 541 | else { 542 | return concat(l$prime, r$prime); 543 | } 544 | } 545 | else { 546 | return /* Empty */0; 547 | } 548 | } 549 | 550 | function partition(p, param) { 551 | if (param) { 552 | var d = param[2]; 553 | var v = param[1]; 554 | var match = partition(p, param[0]); 555 | var lf = match[1]; 556 | var lt = match[0]; 557 | var pvd = p(v, d); 558 | var match$1 = partition(p, param[3]); 559 | var rf = match$1[1]; 560 | var rt = match$1[0]; 561 | if (pvd) { 562 | return /* tuple */[ 563 | join(lt, v, d, rt), 564 | concat(lf, rf) 565 | ]; 566 | } 567 | else { 568 | return /* tuple */[ 569 | concat(lt, rt), 570 | join(lf, v, d, rf) 571 | ]; 572 | } 573 | } 574 | else { 575 | return /* tuple */[ 576 | /* Empty */0, 577 | /* Empty */0 578 | ]; 579 | } 580 | } 581 | 582 | function cons_enum(_m, _e) { 583 | while(true) { 584 | var e = _e; 585 | var m = _m; 586 | if (m) { 587 | _e = /* More */[ 588 | m[1], 589 | m[2], 590 | m[3], 591 | e 592 | ]; 593 | _m = m[0]; 594 | continue ; 595 | 596 | } 597 | else { 598 | return e; 599 | } 600 | }; 601 | } 602 | 603 | function compare(cmp, m1, m2) { 604 | var _e1 = cons_enum(m1, /* End */0); 605 | var _e2 = cons_enum(m2, /* End */0); 606 | while(true) { 607 | var e2 = _e2; 608 | var e1 = _e1; 609 | if (e1) { 610 | if (e2) { 611 | var c = $$String.compare(e1[0], e2[0]); 612 | if (c !== 0) { 613 | return c; 614 | } 615 | else { 616 | var c$1 = cmp(e1[1], e2[1]); 617 | if (c$1 !== 0) { 618 | return c$1; 619 | } 620 | else { 621 | _e2 = cons_enum(e2[2], e2[3]); 622 | _e1 = cons_enum(e1[2], e1[3]); 623 | continue ; 624 | 625 | } 626 | } 627 | } 628 | else { 629 | return 1; 630 | } 631 | } 632 | else if (e2) { 633 | return -1; 634 | } 635 | else { 636 | return 0; 637 | } 638 | }; 639 | } 640 | 641 | function equal(cmp, m1, m2) { 642 | var _e1 = cons_enum(m1, /* End */0); 643 | var _e2 = cons_enum(m2, /* End */0); 644 | while(true) { 645 | var e2 = _e2; 646 | var e1 = _e1; 647 | if (e1) { 648 | if (e2) { 649 | if ($$String.compare(e1[0], e2[0])) { 650 | return /* false */0; 651 | } 652 | else if (cmp(e1[1], e2[1])) { 653 | _e2 = cons_enum(e2[2], e2[3]); 654 | _e1 = cons_enum(e1[2], e1[3]); 655 | continue ; 656 | 657 | } 658 | else { 659 | return /* false */0; 660 | } 661 | } 662 | else { 663 | return /* false */0; 664 | } 665 | } 666 | else if (e2) { 667 | return /* false */0; 668 | } 669 | else { 670 | return /* true */1; 671 | } 672 | }; 673 | } 674 | 675 | function cardinal(param) { 676 | if (param) { 677 | return (cardinal(param[0]) + 1 | 0) + cardinal(param[3]) | 0; 678 | } 679 | else { 680 | return 0; 681 | } 682 | } 683 | 684 | function bindings_aux(_accu, _param) { 685 | while(true) { 686 | var param = _param; 687 | var accu = _accu; 688 | if (param) { 689 | _param = param[0]; 690 | _accu = /* :: */[ 691 | /* tuple */[ 692 | param[1], 693 | param[2] 694 | ], 695 | bindings_aux(accu, param[3]) 696 | ]; 697 | continue ; 698 | 699 | } 700 | else { 701 | return accu; 702 | } 703 | }; 704 | } 705 | 706 | function bindings(s) { 707 | return bindings_aux(/* [] */0, s); 708 | } 709 | 710 | var empty = /* Empty */0; 711 | 712 | var choose = min_binding; 713 | 714 | exports.empty = empty; 715 | exports.is_empty = is_empty; 716 | exports.mem = mem; 717 | exports.add = add; 718 | exports.singleton = singleton; 719 | exports.remove = remove; 720 | exports.merge = merge; 721 | exports.compare = compare; 722 | exports.equal = equal; 723 | exports.iter = iter; 724 | exports.fold = fold; 725 | exports.for_all = for_all; 726 | exports.exists = exists; 727 | exports.filter = filter; 728 | exports.partition = partition; 729 | exports.cardinal = cardinal; 730 | exports.bindings = bindings; 731 | exports.min_binding = min_binding; 732 | exports.max_binding = max_binding; 733 | exports.choose = choose; 734 | exports.split = split; 735 | exports.find = find; 736 | exports.map = map; 737 | exports.mapi = mapi; 738 | /* No side effect */ 739 | -------------------------------------------------------------------------------- /examples/bs-string-map/lib/js/string_map.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.7.1 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | var Caml_builtin_exceptions = require("bs-platform/lib/js/caml_builtin_exceptions"); 5 | var Pervasives = require("bs-platform/lib/js/pervasives"); 6 | var $$String = require("bs-platform/lib/js/string"); 7 | 8 | function height(param) { 9 | if (param) { 10 | return param[4]; 11 | } 12 | else { 13 | return 0; 14 | } 15 | } 16 | 17 | function create(l, x, d, r) { 18 | var hl = height(l); 19 | var hr = height(r); 20 | return /* Node */[ 21 | l, 22 | x, 23 | d, 24 | r, 25 | hl >= hr ? hl + 1 | 0 : hr + 1 | 0 26 | ]; 27 | } 28 | 29 | function singleton(x, d) { 30 | return /* Node */[ 31 | /* Empty */0, 32 | x, 33 | d, 34 | /* Empty */0, 35 | 1 36 | ]; 37 | } 38 | 39 | function bal(l, x, d, r) { 40 | var hl = l ? l[4] : 0; 41 | var hr = r ? r[4] : 0; 42 | if (hl > (hr + 2 | 0)) { 43 | if (l) { 44 | var lr = l[3]; 45 | var ld = l[2]; 46 | var lv = l[1]; 47 | var ll = l[0]; 48 | if (height(ll) >= height(lr)) { 49 | return create(ll, lv, ld, create(lr, x, d, r)); 50 | } 51 | else if (lr) { 52 | return create(create(ll, lv, ld, lr[0]), lr[1], lr[2], create(lr[3], x, d, r)); 53 | } 54 | else { 55 | return Pervasives.invalid_arg("Map.bal"); 56 | } 57 | } 58 | else { 59 | return Pervasives.invalid_arg("Map.bal"); 60 | } 61 | } 62 | else if (hr > (hl + 2 | 0)) { 63 | if (r) { 64 | var rr = r[3]; 65 | var rd = r[2]; 66 | var rv = r[1]; 67 | var rl = r[0]; 68 | if (height(rr) >= height(rl)) { 69 | return create(create(l, x, d, rl), rv, rd, rr); 70 | } 71 | else if (rl) { 72 | return create(create(l, x, d, rl[0]), rl[1], rl[2], create(rl[3], rv, rd, rr)); 73 | } 74 | else { 75 | return Pervasives.invalid_arg("Map.bal"); 76 | } 77 | } 78 | else { 79 | return Pervasives.invalid_arg("Map.bal"); 80 | } 81 | } 82 | else { 83 | return /* Node */[ 84 | l, 85 | x, 86 | d, 87 | r, 88 | hl >= hr ? hl + 1 | 0 : hr + 1 | 0 89 | ]; 90 | } 91 | } 92 | 93 | function is_empty(param) { 94 | if (param) { 95 | return /* false */0; 96 | } 97 | else { 98 | return /* true */1; 99 | } 100 | } 101 | 102 | function add(x, data, param) { 103 | if (param) { 104 | var r = param[3]; 105 | var d = param[2]; 106 | var v = param[1]; 107 | var l = param[0]; 108 | var c = $$String.compare(x, v); 109 | if (c) { 110 | if (c < 0) { 111 | return bal(add(x, data, l), v, d, r); 112 | } 113 | else { 114 | return bal(l, v, d, add(x, data, r)); 115 | } 116 | } 117 | else { 118 | return /* Node */[ 119 | l, 120 | x, 121 | data, 122 | r, 123 | param[4] 124 | ]; 125 | } 126 | } 127 | else { 128 | return /* Node */[ 129 | /* Empty */0, 130 | x, 131 | data, 132 | /* Empty */0, 133 | 1 134 | ]; 135 | } 136 | } 137 | 138 | function find(x, _param) { 139 | while(true) { 140 | var param = _param; 141 | if (param) { 142 | var c = $$String.compare(x, param[1]); 143 | if (c) { 144 | _param = c < 0 ? param[0] : param[3]; 145 | continue ; 146 | 147 | } 148 | else { 149 | return param[2]; 150 | } 151 | } 152 | else { 153 | throw Caml_builtin_exceptions.not_found; 154 | } 155 | }; 156 | } 157 | 158 | function mem(x, _param) { 159 | while(true) { 160 | var param = _param; 161 | if (param) { 162 | var c = $$String.compare(x, param[1]); 163 | if (c) { 164 | _param = c < 0 ? param[0] : param[3]; 165 | continue ; 166 | 167 | } 168 | else { 169 | return /* true */1; 170 | } 171 | } 172 | else { 173 | return /* false */0; 174 | } 175 | }; 176 | } 177 | 178 | function min_binding(_param) { 179 | while(true) { 180 | var param = _param; 181 | if (param) { 182 | var l = param[0]; 183 | if (l) { 184 | _param = l; 185 | continue ; 186 | 187 | } 188 | else { 189 | return /* tuple */[ 190 | param[1], 191 | param[2] 192 | ]; 193 | } 194 | } 195 | else { 196 | throw Caml_builtin_exceptions.not_found; 197 | } 198 | }; 199 | } 200 | 201 | function max_binding(_param) { 202 | while(true) { 203 | var param = _param; 204 | if (param) { 205 | var r = param[3]; 206 | if (r) { 207 | _param = r; 208 | continue ; 209 | 210 | } 211 | else { 212 | return /* tuple */[ 213 | param[1], 214 | param[2] 215 | ]; 216 | } 217 | } 218 | else { 219 | throw Caml_builtin_exceptions.not_found; 220 | } 221 | }; 222 | } 223 | 224 | function remove_min_binding(param) { 225 | if (param) { 226 | var l = param[0]; 227 | if (l) { 228 | return bal(remove_min_binding(l), param[1], param[2], param[3]); 229 | } 230 | else { 231 | return param[3]; 232 | } 233 | } 234 | else { 235 | return Pervasives.invalid_arg("Map.remove_min_elt"); 236 | } 237 | } 238 | 239 | function remove(x, param) { 240 | if (param) { 241 | var r = param[3]; 242 | var d = param[2]; 243 | var v = param[1]; 244 | var l = param[0]; 245 | var c = $$String.compare(x, v); 246 | if (c) { 247 | if (c < 0) { 248 | return bal(remove(x, l), v, d, r); 249 | } 250 | else { 251 | return bal(l, v, d, remove(x, r)); 252 | } 253 | } 254 | else { 255 | var t1 = l; 256 | var t2 = r; 257 | if (t1) { 258 | if (t2) { 259 | var match = min_binding(t2); 260 | return bal(t1, match[0], match[1], remove_min_binding(t2)); 261 | } 262 | else { 263 | return t1; 264 | } 265 | } 266 | else { 267 | return t2; 268 | } 269 | } 270 | } 271 | else { 272 | return /* Empty */0; 273 | } 274 | } 275 | 276 | function iter(f, _param) { 277 | while(true) { 278 | var param = _param; 279 | if (param) { 280 | iter(f, param[0]); 281 | f(param[1], param[2]); 282 | _param = param[3]; 283 | continue ; 284 | 285 | } 286 | else { 287 | return /* () */0; 288 | } 289 | }; 290 | } 291 | 292 | function map(f, param) { 293 | if (param) { 294 | var l$prime = map(f, param[0]); 295 | var d$prime = f(param[2]); 296 | var r$prime = map(f, param[3]); 297 | return /* Node */[ 298 | l$prime, 299 | param[1], 300 | d$prime, 301 | r$prime, 302 | param[4] 303 | ]; 304 | } 305 | else { 306 | return /* Empty */0; 307 | } 308 | } 309 | 310 | function mapi(f, param) { 311 | if (param) { 312 | var v = param[1]; 313 | var l$prime = mapi(f, param[0]); 314 | var d$prime = f(v, param[2]); 315 | var r$prime = mapi(f, param[3]); 316 | return /* Node */[ 317 | l$prime, 318 | v, 319 | d$prime, 320 | r$prime, 321 | param[4] 322 | ]; 323 | } 324 | else { 325 | return /* Empty */0; 326 | } 327 | } 328 | 329 | function fold(f, _m, _accu) { 330 | while(true) { 331 | var accu = _accu; 332 | var m = _m; 333 | if (m) { 334 | _accu = f(m[1], m[2], fold(f, m[0], accu)); 335 | _m = m[3]; 336 | continue ; 337 | 338 | } 339 | else { 340 | return accu; 341 | } 342 | }; 343 | } 344 | 345 | function for_all(p, _param) { 346 | while(true) { 347 | var param = _param; 348 | if (param) { 349 | if (p(param[1], param[2])) { 350 | if (for_all(p, param[0])) { 351 | _param = param[3]; 352 | continue ; 353 | 354 | } 355 | else { 356 | return /* false */0; 357 | } 358 | } 359 | else { 360 | return /* false */0; 361 | } 362 | } 363 | else { 364 | return /* true */1; 365 | } 366 | }; 367 | } 368 | 369 | function exists(p, _param) { 370 | while(true) { 371 | var param = _param; 372 | if (param) { 373 | if (p(param[1], param[2])) { 374 | return /* true */1; 375 | } 376 | else if (exists(p, param[0])) { 377 | return /* true */1; 378 | } 379 | else { 380 | _param = param[3]; 381 | continue ; 382 | 383 | } 384 | } 385 | else { 386 | return /* false */0; 387 | } 388 | }; 389 | } 390 | 391 | function add_min_binding(k, v, param) { 392 | if (param) { 393 | return bal(add_min_binding(k, v, param[0]), param[1], param[2], param[3]); 394 | } 395 | else { 396 | return singleton(k, v); 397 | } 398 | } 399 | 400 | function add_max_binding(k, v, param) { 401 | if (param) { 402 | return bal(param[0], param[1], param[2], add_max_binding(k, v, param[3])); 403 | } 404 | else { 405 | return singleton(k, v); 406 | } 407 | } 408 | 409 | function join(l, v, d, r) { 410 | if (l) { 411 | if (r) { 412 | var rh = r[4]; 413 | var lh = l[4]; 414 | if (lh > (rh + 2 | 0)) { 415 | return bal(l[0], l[1], l[2], join(l[3], v, d, r)); 416 | } 417 | else if (rh > (lh + 2 | 0)) { 418 | return bal(join(l, v, d, r[0]), r[1], r[2], r[3]); 419 | } 420 | else { 421 | return create(l, v, d, r); 422 | } 423 | } 424 | else { 425 | return add_max_binding(v, d, l); 426 | } 427 | } 428 | else { 429 | return add_min_binding(v, d, r); 430 | } 431 | } 432 | 433 | function concat(t1, t2) { 434 | if (t1) { 435 | if (t2) { 436 | var match = min_binding(t2); 437 | return join(t1, match[0], match[1], remove_min_binding(t2)); 438 | } 439 | else { 440 | return t1; 441 | } 442 | } 443 | else { 444 | return t2; 445 | } 446 | } 447 | 448 | function concat_or_join(t1, v, d, t2) { 449 | if (d) { 450 | return join(t1, v, d[0], t2); 451 | } 452 | else { 453 | return concat(t1, t2); 454 | } 455 | } 456 | 457 | function split(x, param) { 458 | if (param) { 459 | var r = param[3]; 460 | var d = param[2]; 461 | var v = param[1]; 462 | var l = param[0]; 463 | var c = $$String.compare(x, v); 464 | if (c) { 465 | if (c < 0) { 466 | var match = split(x, l); 467 | return /* tuple */[ 468 | match[0], 469 | match[1], 470 | join(match[2], v, d, r) 471 | ]; 472 | } 473 | else { 474 | var match$1 = split(x, r); 475 | return /* tuple */[ 476 | join(l, v, d, match$1[0]), 477 | match$1[1], 478 | match$1[2] 479 | ]; 480 | } 481 | } 482 | else { 483 | return /* tuple */[ 484 | l, 485 | /* Some */[d], 486 | r 487 | ]; 488 | } 489 | } 490 | else { 491 | return /* tuple */[ 492 | /* Empty */0, 493 | /* None */0, 494 | /* Empty */0 495 | ]; 496 | } 497 | } 498 | 499 | function merge(f, s1, s2) { 500 | var exit = 0; 501 | if (s1) { 502 | var v1 = s1[1]; 503 | if (s1[4] >= height(s2)) { 504 | var match = split(v1, s2); 505 | return concat_or_join(merge(f, s1[0], match[0]), v1, f(v1, /* Some */[s1[2]], match[1]), merge(f, s1[3], match[2])); 506 | } 507 | else { 508 | exit = 1; 509 | } 510 | } 511 | else if (s2) { 512 | exit = 1; 513 | } 514 | else { 515 | return /* Empty */0; 516 | } 517 | if (exit === 1) { 518 | if (s2) { 519 | var v2 = s2[1]; 520 | var match$1 = split(v2, s1); 521 | return concat_or_join(merge(f, match$1[0], s2[0]), v2, f(v2, match$1[1], /* Some */[s2[2]]), merge(f, match$1[2], s2[3])); 522 | } 523 | else { 524 | return /* impossible branch */0; 525 | } 526 | } 527 | 528 | } 529 | 530 | function filter(p, param) { 531 | if (param) { 532 | var d = param[2]; 533 | var v = param[1]; 534 | var l$prime = filter(p, param[0]); 535 | var pvd = p(v, d); 536 | var r$prime = filter(p, param[3]); 537 | if (pvd) { 538 | return join(l$prime, v, d, r$prime); 539 | } 540 | else { 541 | return concat(l$prime, r$prime); 542 | } 543 | } 544 | else { 545 | return /* Empty */0; 546 | } 547 | } 548 | 549 | function partition(p, param) { 550 | if (param) { 551 | var d = param[2]; 552 | var v = param[1]; 553 | var match = partition(p, param[0]); 554 | var lf = match[1]; 555 | var lt = match[0]; 556 | var pvd = p(v, d); 557 | var match$1 = partition(p, param[3]); 558 | var rf = match$1[1]; 559 | var rt = match$1[0]; 560 | if (pvd) { 561 | return /* tuple */[ 562 | join(lt, v, d, rt), 563 | concat(lf, rf) 564 | ]; 565 | } 566 | else { 567 | return /* tuple */[ 568 | concat(lt, rt), 569 | join(lf, v, d, rf) 570 | ]; 571 | } 572 | } 573 | else { 574 | return /* tuple */[ 575 | /* Empty */0, 576 | /* Empty */0 577 | ]; 578 | } 579 | } 580 | 581 | function cons_enum(_m, _e) { 582 | while(true) { 583 | var e = _e; 584 | var m = _m; 585 | if (m) { 586 | _e = /* More */[ 587 | m[1], 588 | m[2], 589 | m[3], 590 | e 591 | ]; 592 | _m = m[0]; 593 | continue ; 594 | 595 | } 596 | else { 597 | return e; 598 | } 599 | }; 600 | } 601 | 602 | function compare(cmp, m1, m2) { 603 | var _e1 = cons_enum(m1, /* End */0); 604 | var _e2 = cons_enum(m2, /* End */0); 605 | while(true) { 606 | var e2 = _e2; 607 | var e1 = _e1; 608 | if (e1) { 609 | if (e2) { 610 | var c = $$String.compare(e1[0], e2[0]); 611 | if (c !== 0) { 612 | return c; 613 | } 614 | else { 615 | var c$1 = cmp(e1[1], e2[1]); 616 | if (c$1 !== 0) { 617 | return c$1; 618 | } 619 | else { 620 | _e2 = cons_enum(e2[2], e2[3]); 621 | _e1 = cons_enum(e1[2], e1[3]); 622 | continue ; 623 | 624 | } 625 | } 626 | } 627 | else { 628 | return 1; 629 | } 630 | } 631 | else if (e2) { 632 | return -1; 633 | } 634 | else { 635 | return 0; 636 | } 637 | }; 638 | } 639 | 640 | function equal(cmp, m1, m2) { 641 | var _e1 = cons_enum(m1, /* End */0); 642 | var _e2 = cons_enum(m2, /* End */0); 643 | while(true) { 644 | var e2 = _e2; 645 | var e1 = _e1; 646 | if (e1) { 647 | if (e2) { 648 | if ($$String.compare(e1[0], e2[0])) { 649 | return /* false */0; 650 | } 651 | else if (cmp(e1[1], e2[1])) { 652 | _e2 = cons_enum(e2[2], e2[3]); 653 | _e1 = cons_enum(e1[2], e1[3]); 654 | continue ; 655 | 656 | } 657 | else { 658 | return /* false */0; 659 | } 660 | } 661 | else { 662 | return /* false */0; 663 | } 664 | } 665 | else if (e2) { 666 | return /* false */0; 667 | } 668 | else { 669 | return /* true */1; 670 | } 671 | }; 672 | } 673 | 674 | function cardinal(param) { 675 | if (param) { 676 | return (cardinal(param[0]) + 1 | 0) + cardinal(param[3]) | 0; 677 | } 678 | else { 679 | return 0; 680 | } 681 | } 682 | 683 | function bindings_aux(_accu, _param) { 684 | while(true) { 685 | var param = _param; 686 | var accu = _accu; 687 | if (param) { 688 | _param = param[0]; 689 | _accu = /* :: */[ 690 | /* tuple */[ 691 | param[1], 692 | param[2] 693 | ], 694 | bindings_aux(accu, param[3]) 695 | ]; 696 | continue ; 697 | 698 | } 699 | else { 700 | return accu; 701 | } 702 | }; 703 | } 704 | 705 | function bindings(s) { 706 | return bindings_aux(/* [] */0, s); 707 | } 708 | 709 | var empty = /* Empty */0; 710 | 711 | var choose = min_binding; 712 | 713 | exports.empty = empty; 714 | exports.is_empty = is_empty; 715 | exports.mem = mem; 716 | exports.add = add; 717 | exports.singleton = singleton; 718 | exports.remove = remove; 719 | exports.merge = merge; 720 | exports.compare = compare; 721 | exports.equal = equal; 722 | exports.iter = iter; 723 | exports.fold = fold; 724 | exports.for_all = for_all; 725 | exports.exists = exists; 726 | exports.filter = filter; 727 | exports.partition = partition; 728 | exports.cardinal = cardinal; 729 | exports.bindings = bindings; 730 | exports.min_binding = min_binding; 731 | exports.max_binding = max_binding; 732 | exports.choose = choose; 733 | exports.split = split; 734 | exports.find = find; 735 | exports.map = map; 736 | exports.mapi = mapi; 737 | /* No side effect */ 738 | -------------------------------------------------------------------------------- /examples/bs-string-map/lib/js/test/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rescript-lang/bucklescript-addons/c544020712b9507b8b4900444320c2448f9c7ae5/examples/bs-string-map/lib/js/test/.gitignore -------------------------------------------------------------------------------- /examples/bs-string-map/lib/js/test/test_map.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.7.1 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | var Bs_mocha = require("bs-mocha/lib/js/bs_mocha"); 5 | var Block = require("bs-platform/lib/js/block"); 6 | var String_map = require("../string_map"); 7 | 8 | var v = String_map.add("1", 1, String_map.add("3", 3, String_map.empty)); 9 | 10 | Bs_mocha.from_pair_suites("test/test_map.ml", /* :: */[ 11 | /* tuple */[ 12 | 'File "test/test_map.ml", line 10, characters 4-11', 13 | function () { 14 | return /* Eq */Block.__(0, [ 15 | String_map.find("3", v), 16 | 3 17 | ]); 18 | } 19 | ], 20 | /* [] */0 21 | ]); 22 | 23 | /* v Not a pure module */ 24 | -------------------------------------------------------------------------------- /examples/bs-string-map/lib/ocaml/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rescript-lang/bucklescript-addons/c544020712b9507b8b4900444320c2448f9c7ae5/examples/bs-string-map/lib/ocaml/.gitignore -------------------------------------------------------------------------------- /examples/bs-string-map/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bs-string-map", 3 | "version": "1.0.2", 4 | "description": "map data structure", 5 | "scripts": { 6 | "postinstall": "./scripts/install.sh", 7 | "test": "bsc -bs-package-name $npm_package_name -bs-package-output lib/js/test -bs-package-include bs-mocha -I src -I test -c -bs-files test/*.ml test/*.mli && mocha lib/js/test/*.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/bloomberg/bucklescript-addons.git" 12 | }, 13 | "keywords": [ 14 | "ocaml", 15 | "bucklescript", 16 | "map" 17 | ], 18 | "author": "Hongbo Zhang", 19 | "license": "ISC", 20 | "bugs": { 21 | "url": "https://github.com/bloomberg/bucklescript-addons/issues" 22 | }, 23 | "homepage": "https://github.com/bloomberg/bucklescript-addons#readme", 24 | "dependencies": { 25 | "bs-platform": "^0.8.0" 26 | }, 27 | "devDependencies": { 28 | "bs-mocha": "^0.1.3" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /examples/bs-string-map/scripts/install.sh: -------------------------------------------------------------------------------- 1 | 2 | #!/bin/sh 3 | 4 | set -e 5 | 6 | ./node_modules/.bin/bsc -bs-package-name bs-string-map -bs-package-output lib/js -bs-package-output amdjs:lib/amdjs -bs-no-any-assert -bs-package-output goog:lib/goog -I src -c -bs-files src/*.mli src/*.ml 7 | 8 | # install 9 | cp ./src/*.cm* ./lib/ocaml 10 | -------------------------------------------------------------------------------- /examples/bs-string-map/src/string_map.ml: -------------------------------------------------------------------------------- 1 | (***********************************************************************) 2 | (* *) 3 | (* OCaml *) 4 | (* *) 5 | (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *) 6 | (* *) 7 | (* Copyright 1996 Institut National de Recherche en Informatique et *) 8 | (* en Automatique. All rights reserved. This file is distributed *) 9 | (* under the terms of the GNU Library General Public License, with *) 10 | (* the special exception on linking described in file ../LICENSE. *) 11 | (* *) 12 | (***********************************************************************) 13 | 14 | type 'a t = 15 | | Empty 16 | | Node of 'a t * string * 'a * 'a t * int 17 | 18 | let height = function 19 | Empty -> 0 20 | | Node(_,_,_,_,h) -> h 21 | 22 | let create l x d r = 23 | let hl = height l and hr = height r in 24 | Node(l, x, d, r, (if hl >= hr then hl + 1 else hr + 1)) 25 | 26 | let singleton x d = Node(Empty, x, d, Empty, 1) 27 | 28 | let bal l x d r = 29 | let hl = match l with Empty -> 0 | Node(_,_,_,_,h) -> h in 30 | let hr = match r with Empty -> 0 | Node(_,_,_,_,h) -> h in 31 | if hl > hr + 2 then begin 32 | match l with 33 | Empty -> invalid_arg "Map.bal" 34 | | Node(ll, lv, ld, lr, _) -> 35 | if height ll >= height lr then 36 | create ll lv ld (create lr x d r) 37 | else begin 38 | match lr with 39 | Empty -> invalid_arg "Map.bal" 40 | | Node(lrl, lrv, lrd, lrr, _)-> 41 | create (create ll lv ld lrl) lrv lrd (create lrr x d r) 42 | end 43 | end else if hr > hl + 2 then begin 44 | match r with 45 | Empty -> invalid_arg "Map.bal" 46 | | Node(rl, rv, rd, rr, _) -> 47 | if height rr >= height rl then 48 | create (create l x d rl) rv rd rr 49 | else begin 50 | match rl with 51 | Empty -> invalid_arg "Map.bal" 52 | | Node(rll, rlv, rld, rlr, _) -> 53 | create (create l x d rll) rlv rld (create rlr rv rd rr) 54 | end 55 | end else 56 | Node(l, x, d, r, (if hl >= hr then hl + 1 else hr + 1)) 57 | 58 | let empty = Empty 59 | 60 | let is_empty = function Empty -> true | _ -> false 61 | 62 | let rec add x data = function 63 | Empty -> 64 | Node(Empty, x, data, Empty, 1) 65 | | Node(l, v, d, r, h) -> 66 | let c = String.compare x v in 67 | if c = 0 then 68 | Node(l, x, data, r, h) 69 | else if c < 0 then 70 | bal (add x data l) v d r 71 | else 72 | bal l v d (add x data r) 73 | 74 | let rec find x = function 75 | Empty -> 76 | raise Not_found 77 | | Node(l, v, d, r, _) -> 78 | let c = String.compare x v in 79 | if c = 0 then d 80 | else find x (if c < 0 then l else r) 81 | 82 | let rec mem x = function 83 | Empty -> 84 | false 85 | | Node(l, v, d, r, _) -> 86 | let c = String.compare x v in 87 | c = 0 || mem x (if c < 0 then l else r) 88 | 89 | let rec min_binding = function 90 | Empty -> raise Not_found 91 | | Node(Empty, x, d, r, _) -> (x, d) 92 | | Node(l, x, d, r, _) -> min_binding l 93 | 94 | let rec max_binding = function 95 | Empty -> raise Not_found 96 | | Node(l, x, d, Empty, _) -> (x, d) 97 | | Node(l, x, d, r, _) -> max_binding r 98 | 99 | let rec remove_min_binding = function 100 | Empty -> invalid_arg "Map.remove_min_elt" 101 | | Node(Empty, x, d, r, _) -> r 102 | | Node(l, x, d, r, _) -> bal (remove_min_binding l) x d r 103 | 104 | let merge t1 t2 = 105 | match (t1, t2) with 106 | (Empty, t) -> t 107 | | (t, Empty) -> t 108 | | (_, _) -> 109 | let (x, d) = min_binding t2 in 110 | bal t1 x d (remove_min_binding t2) 111 | 112 | let rec remove x = function 113 | Empty -> 114 | Empty 115 | | Node(l, v, d, r, h) -> 116 | let c = String.compare x v in 117 | if c = 0 then 118 | merge l r 119 | else if c < 0 then 120 | bal (remove x l) v d r 121 | else 122 | bal l v d (remove x r) 123 | 124 | let rec iter f = function 125 | Empty -> () 126 | | Node(l, v, d, r, _) -> 127 | iter f l; f v d [@bs]; iter f r 128 | 129 | let rec map f = function 130 | Empty -> 131 | Empty 132 | | Node(l, v, d, r, h) -> 133 | let l' = map f l in 134 | let d' = f d [@bs] in 135 | let r' = map f r in 136 | Node(l', v, d', r', h) 137 | 138 | let rec mapi f = function 139 | Empty -> 140 | Empty 141 | | Node(l, v, d, r, h) -> 142 | let l' = mapi f l in 143 | let d' = f v d [@bs] in 144 | let r' = mapi f r in 145 | Node(l', v, d', r', h) 146 | 147 | let rec fold f m accu = 148 | match m with 149 | Empty -> accu 150 | | Node(l, v, d, r, _) -> 151 | fold f r (f v d (fold f l accu) [@bs]) 152 | 153 | let rec for_all p = function 154 | Empty -> true 155 | | Node(l, v, d, r, _) -> p v d [@bs] && for_all p l && for_all p r 156 | 157 | let rec exists p = function 158 | Empty -> false 159 | | Node(l, v, d, r, _) -> p v d[@bs] || exists p l || exists p r 160 | 161 | (* Beware: those two functions assume that the added k is *strictly* 162 | smaller (or bigger) than all the present keys in the tree; it 163 | does not test for equality with the current min (or max) key. 164 | 165 | Indeed, they are only used during the "join" operation which 166 | respects this precondition. 167 | *) 168 | 169 | let rec add_min_binding k v = function 170 | | Empty -> singleton k v 171 | | Node (l, x, d, r, h) -> 172 | bal (add_min_binding k v l) x d r 173 | 174 | let rec add_max_binding k v = function 175 | | Empty -> singleton k v 176 | | Node (l, x, d, r, h) -> 177 | bal l x d (add_max_binding k v r) 178 | 179 | (* Same as create and bal, but no assumptions are made on the 180 | relative heights of l and r. *) 181 | 182 | let rec join l v d r = 183 | match (l, r) with 184 | (Empty, _) -> add_min_binding v d r 185 | | (_, Empty) -> add_max_binding v d l 186 | | (Node(ll, lv, ld, lr, lh), Node(rl, rv, rd, rr, rh)) -> 187 | if lh > rh + 2 then bal ll lv ld (join lr v d r) else 188 | if rh > lh + 2 then bal (join l v d rl) rv rd rr else 189 | create l v d r 190 | 191 | (* Merge two trees l and r into one. 192 | All elements of l must precede the elements of r. 193 | No assumption on the heights of l and r. *) 194 | 195 | let concat t1 t2 = 196 | match (t1, t2) with 197 | (Empty, t) -> t 198 | | (t, Empty) -> t 199 | | (_, _) -> 200 | let (x, d) = min_binding t2 in 201 | join t1 x d (remove_min_binding t2) 202 | 203 | let concat_or_join t1 v d t2 = 204 | match d with 205 | | Some d -> join t1 v d t2 206 | | None -> concat t1 t2 207 | 208 | let rec split x = function 209 | Empty -> 210 | (Empty, None, Empty) 211 | | Node(l, v, d, r, _) -> 212 | let c = String.compare x v in 213 | if c = 0 then (l, Some d, r) 214 | else if c < 0 then 215 | let (ll, pres, rl) = split x l in (ll, pres, join rl v d r) 216 | else 217 | let (lr, pres, rr) = split x r in (join l v d lr, pres, rr) 218 | 219 | let rec merge f s1 s2 = 220 | match (s1, s2) with 221 | (Empty, Empty) -> Empty 222 | | (Node (l1, v1, d1, r1, h1), _) when h1 >= height s2 -> 223 | let (l2, d2, r2) = split v1 s2 in 224 | concat_or_join (merge f l1 l2) v1 (f v1 (Some d1) d2 [@bs]) (merge f r1 r2) 225 | | (_, Node (l2, v2, d2, r2, h2)) -> 226 | let (l1, d1, r1) = split v2 s1 in 227 | concat_or_join (merge f l1 l2) v2 (f v2 d1 (Some d2) [@bs]) (merge f r1 r2) 228 | | _ -> 229 | assert false 230 | 231 | let rec filter p = function 232 | Empty -> Empty 233 | | Node(l, v, d, r, _) -> 234 | (* call [p] in the expected left-to-right order *) 235 | let l' = filter p l in 236 | let pvd = p v d [@bs] in 237 | let r' = filter p r in 238 | if pvd then join l' v d r' else concat l' r' 239 | 240 | let rec partition p = function 241 | Empty -> (Empty, Empty) 242 | | Node(l, v, d, r, _) -> 243 | (* call [p] in the expected left-to-right order *) 244 | let (lt, lf) = partition p l in 245 | let pvd = p v d [@bs] in 246 | let (rt, rf) = partition p r in 247 | if pvd 248 | then (join lt v d rt, concat lf rf) 249 | else (concat lt rt, join lf v d rf) 250 | 251 | type 'a enumeration = End | More of string * 'a * 'a t * 'a enumeration 252 | 253 | let rec cons_enum m e = 254 | match m with 255 | Empty -> e 256 | | Node(l, v, d, r, _) -> cons_enum l (More(v, d, r, e)) 257 | 258 | let compare cmp m1 m2 = 259 | let rec compare_aux e1 e2 = 260 | match (e1, e2) with 261 | (End, End) -> 0 262 | | (End, _) -> -1 263 | | (_, End) -> 1 264 | | (More(v1, d1, r1, e1), More(v2, d2, r2, e2)) -> 265 | let c = String.compare v1 v2 in 266 | if c <> 0 then c else 267 | let c = cmp d1 d2 [@bs] in 268 | if c <> 0 then c else 269 | compare_aux (cons_enum r1 e1) (cons_enum r2 e2) 270 | in compare_aux (cons_enum m1 End) (cons_enum m2 End) 271 | 272 | let equal cmp m1 m2 = 273 | let rec equal_aux e1 e2 = 274 | match (e1, e2) with 275 | (End, End) -> true 276 | | (End, _) -> false 277 | | (_, End) -> false 278 | | (More(v1, d1, r1, e1), More(v2, d2, r2, e2)) -> 279 | String.compare v1 v2 = 0 && cmp d1 d2 [@bs] && 280 | equal_aux (cons_enum r1 e1) (cons_enum r2 e2) 281 | in equal_aux (cons_enum m1 End) (cons_enum m2 End) 282 | 283 | let rec cardinal = function 284 | Empty -> 0 285 | | Node(l, _, _, r, _) -> cardinal l + 1 + cardinal r 286 | 287 | let rec bindings_aux accu = function 288 | Empty -> accu 289 | | Node(l, v, d, r, _) -> bindings_aux ((v, d) :: bindings_aux accu r) l 290 | 291 | let bindings s = 292 | bindings_aux [] s 293 | 294 | let choose = min_binding 295 | 296 | 297 | -------------------------------------------------------------------------------- /examples/bs-string-map/src/string_map.mli: -------------------------------------------------------------------------------- 1 | type +'a t 2 | val empty: 'a t 3 | val is_empty: 'a t -> bool 4 | val mem: string -> 'a t -> bool 5 | val add: string -> 'a -> 'a t -> 'a t 6 | val singleton: string -> 'a -> 'a t 7 | val remove: string -> 'a t -> 'a t 8 | val merge: 9 | (string -> 'a option -> 'b option -> 'c option [@bs]) -> 'a t -> 'b t -> 'c t 10 | val compare: ('a -> 'a -> int [@bs]) -> 'a t -> 'a t -> int 11 | val equal: ('a -> 'a -> bool [@bs]) -> 'a t -> 'a t -> bool 12 | val iter: (string -> 'a -> unit [@bs]) -> 'a t -> unit 13 | val fold: (string -> 'a -> 'b -> 'b [@bs]) -> 'a t -> 'b -> 'b 14 | val for_all: (string -> 'a -> bool [@bs]) -> 'a t -> bool 15 | val exists: (string -> 'a -> bool [@bs]) -> 'a t -> bool 16 | val filter: (string -> 'a -> bool [@bs]) -> 'a t -> 'a t 17 | val partition: (string -> 'a -> bool [@bs]) -> 'a t -> 'a t * 'a t 18 | val cardinal: 'a t -> int 19 | val bindings: 'a t -> (string * 'a) list 20 | val min_binding: 'a t -> (string * 'a) 21 | val max_binding: 'a t -> (string * 'a) 22 | val choose: 'a t -> (string * 'a) 23 | val split: string -> 'a t -> 'a t * 'a option * 'a t 24 | val find: string -> 'a t -> 'a 25 | val map: ('a -> 'b [@bs]) -> 'a t -> 'b t 26 | val mapi: (string -> 'a -> 'b [@bs]) -> 'a t -> 'b t 27 | -------------------------------------------------------------------------------- /examples/bs-string-map/test/test_map.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.7.1 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | var Bs_mocha = require("bs-mocha/lib/js/bs_mocha"); 5 | var Block = require("bs-platform/lib/js/block"); 6 | var String_map = require("bs-string-map/lib/js/string_map"); 7 | 8 | var v = String_map.add("1", 1, String_map.add("3", 3, String_map.empty)); 9 | 10 | Bs_mocha.from_pair_suites("test/test_map.ml", /* :: */[ 11 | /* tuple */[ 12 | 'File "test/test_map.ml", line 10, characters 4-11', 13 | function () { 14 | return /* Eq */Block.__(0, [ 15 | String_map.find("3", v), 16 | 3 17 | ]); 18 | } 19 | ], 20 | /* [] */0 21 | ]); 22 | 23 | /* v Not a pure module */ 24 | -------------------------------------------------------------------------------- /examples/bs-string-map/test/test_map.ml: -------------------------------------------------------------------------------- 1 | 2 | 3 | let v = 4 | String_map.( 5 | empty 6 | |> add "3" 3 7 | |> add "1" 1 ) 8 | 9 | ;;Bs_mocha.from_pair_suites __FILE__ 10 | [ __LOC__, begin fun [@bs] () -> Bs_mocha.Eq (String_map.find "3" v, 3) end] 11 | -------------------------------------------------------------------------------- /examples/bs-string-map/test/test_map.mli: -------------------------------------------------------------------------------- 1 | (** *) 2 | -------------------------------------------------------------------------------- /examples/hello/main_entry.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | console.log("hello world"); 6 | 7 | /* Not a pure module */ 8 | -------------------------------------------------------------------------------- /examples/hello/main_entry.ml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Js.log "hello world" 4 | -------------------------------------------------------------------------------- /examples/hello/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "bs-platform": "0.9.3" 4 | }, 5 | "scripts" : { 6 | "build" : "bsc -c main_entry.ml" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /examples/node-http-server/README.md: -------------------------------------------------------------------------------- 1 | 2 | # [BuckleScript](http://bloomberg.github.io/bucklescript/) Demo 3 | 4 | 5 | 6 | 7 | ```sh 8 | npm install 9 | npm run build 10 | ``` 11 | -------------------------------------------------------------------------------- /examples/node-http-server/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "devDependencies": { 3 | "bs-platform": "0.9.3" 4 | }, 5 | "scripts" : { 6 | "build" : "bsc -I src -c -bs-main src/test_http_server.ml" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /examples/node-http-server/src/http_types.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | 6 | /* No side effect */ 7 | -------------------------------------------------------------------------------- /examples/node-http-server/src/http_types.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2015-2016 Bloomberg Finance L.P. 2 | * 3 | * This program is free software: you can redistribute it and/or modify 4 | * it under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or 6 | * (at your option) any later version. 7 | * 8 | * In addition to the permissions granted to you by the LGPL, you may combine 9 | * or link a "work that uses the Library" with a publicly distributed version 10 | * of this file to produce a combined library or application, then distribute 11 | * that combined work under the terms of your choosing, with no requirement 12 | * to comply with the obligations normally placed on you by section 4 of the 13 | * LGPL version 3 (or the corresponding section of a later version of the LGPL 14 | * should you choose to use a later version). 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with this program; if not, write to the Free Software 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) 24 | 25 | (** A simple binding to node [http] module *) 26 | type req 27 | 28 | class type _resp = object 29 | method statusCode : int [@@bs.set] 30 | method setHeader : string -> string -> unit 31 | method _end : string -> unit 32 | end [@bs] 33 | 34 | type resp = _resp Js.t 35 | 36 | class type _server = object 37 | method listen : int -> string -> (unit -> unit [@bs]) -> unit 38 | end [@bs] 39 | type server = _server Js.t 40 | 41 | class type _http = object 42 | method createServer : (req -> resp -> unit [@bs] ) -> server 43 | end [@bs] 44 | 45 | type http = _http Js.t 46 | 47 | 48 | external http : http = "" [@@bs.module] 49 | -------------------------------------------------------------------------------- /examples/node-http-server/src/test_http_server.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | var Pervasives = require("bs-platform/lib/js/pervasives"); 5 | var Http = require("http"); 6 | 7 | var hostname = "127.0.0.1"; 8 | 9 | function create_server(http) { 10 | var server = http.createServer(function (_, resp) { 11 | resp.statusCode = 200; 12 | resp.setHeader("Content-Type", "text/plain"); 13 | return resp.end("Hello world\n"); 14 | }); 15 | return server.listen(3000, hostname, function () { 16 | console.log("Server running at http://" + (hostname + (":" + (Pervasives.string_of_int(3000) + "/")))); 17 | return /* () */0; 18 | }); 19 | } 20 | 21 | create_server(Http); 22 | 23 | /* Not a pure module */ 24 | -------------------------------------------------------------------------------- /examples/node-http-server/src/test_http_server.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2015-2016 Bloomberg Finance L.P. 2 | * 3 | * This program is free software: you can redistribute it and/or modify 4 | * it under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or 6 | * (at your option) any later version. 7 | * 8 | * In addition to the permissions granted to you by the LGPL, you may combine 9 | * or link a "work that uses the Library" with a publicly distributed version 10 | * of this file to produce a combined library or application, then distribute 11 | * that combined work under the terms of your choosing, with no requirement 12 | * to comply with the obligations normally placed on you by section 4 of the 13 | * LGPL version 3 (or the corresponding section of a later version of the LGPL 14 | * should you choose to use a later version). 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with this program; if not, write to the Free Software 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) 24 | 25 | 26 | let port = 3000 27 | let hostname = "127.0.0.1" 28 | let create_server http = 29 | let server = http##createServer begin fun [@bs] req resp -> 30 | resp##statusCode #= 200; 31 | resp##setHeader "Content-Type" "text/plain"; 32 | resp##_end "Hello world\n" 33 | end 34 | in 35 | server##listen port hostname begin fun [@bs] () -> 36 | Js.log ("Server running at http://"^ hostname ^ ":" ^ Pervasives.string_of_int port ^ "/") 37 | end 38 | 39 | 40 | let () = 41 | create_server Http_types.http 42 | 43 | 44 | 45 | (* local variables: *) 46 | (* compile-command: "npm run --no-color build" *) 47 | (* end: *) 48 | -------------------------------------------------------------------------------- /examples/node-http-server/src/test_http_server.mli: -------------------------------------------------------------------------------- 1 | (** *) 2 | -------------------------------------------------------------------------------- /examples/reason-demo/README.md: -------------------------------------------------------------------------------- 1 | ```sh 2 | npm install 3 | npm run build 4 | ``` 5 | -------------------------------------------------------------------------------- /examples/reason-demo/hello.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE BY BUCKLESCRIPT VERSION 0.7.1 , PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | console.log("HELLO, REASON & BUCKLESCRIPT"); 6 | 7 | /* Not a pure module */ 8 | -------------------------------------------------------------------------------- /examples/reason-demo/hello.re: -------------------------------------------------------------------------------- 1 | Js.log "HELLO, REASON & BUCKLESCRIPT" -------------------------------------------------------------------------------- /examples/reason-demo/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "devDependencies": { 3 | "reason": "git+https://github.com/facebook/reason.git" 4 | }, 5 | "dependencies": { 6 | "bs-platform": "^0.7.1" 7 | }, 8 | "scripts" : { 9 | "build" : "source dependencyEnv && bsc -pp ./node_modules/reason/refmt_impl.native -impl hello.re" 10 | } 11 | , "name" : "test" 12 | } 13 | --------------------------------------------------------------------------------