Skip to content

Commit 50efc3c

Browse files
committed
add more utilities, improve dev experience, todo: more testing before making utilities available
1 parent 4fdbba0 commit 50efc3c

33 files changed

+695
-198
lines changed

jscomp/bin/bsb_watcher.future.js

+53-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,59 @@ var Process = require('process');
55
var Fs = require('fs');
66
var Child_process = require('child_process');
77

8+
function getExn(x) {
9+
if (x) {
10+
return x[0];
11+
} else {
12+
throw new Error("Bs_option.getExn");
13+
}
14+
}
15+
16+
17+
/* No side effect */
18+
19+
var invalid_argument = /* tuple */[
20+
"Invalid_argument",
21+
-3
22+
];
23+
24+
invalid_argument.tag = 248;
25+
26+
27+
/* Not a pure module */
28+
29+
/* No side effect */
30+
31+
/* No side effect */
32+
33+
/* No side effect */
34+
35+
/* No side effect */
36+
37+
/* stdin Not a pure module */
38+
39+
/* No side effect */
40+
41+
/* imul Not a pure module */
42+
43+
/* repeat Not a pure module */
44+
45+
/* two_ptr_32_dbl Not a pure module */
46+
47+
/* float_of_string Not a pure module */
48+
49+
/* No side effect */
50+
51+
/* No side effect */
52+
53+
/* not_implemented Not a pure module */
54+
55+
/* No side effect */
56+
57+
/* No side effect */
58+
59+
/* No side effect */
60+
861
function filterInPlace(p, a) {
962
var i = 0;
1063
var j = 0;
@@ -25,18 +78,6 @@ function memByRef(x, xs) {
2578
}
2679

2780

28-
/* No side effect */
29-
30-
function getExn(x) {
31-
if (x) {
32-
return x[0];
33-
} else {
34-
var str = " " + (String("File \"bs_option.ml\", line 39, characters 34-42") + ": Bs.Option.unsafeGet");
35-
throw new Error(str);
36-
}
37-
}
38-
39-
4081
/* No side effect */
4182

4283
function undefined_to_opt(x) {

jscomp/others/.depend

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ js_json.cmj : js_types.cmj js_string.cmj js_dict.cmj js_array.cmj \
1212
js_obj.cmj :
1313
bs_dyn.cmj : bs_dyn.cmi
1414
bs_dyn_lib.cmj : bs_dyn.cmj bs_dyn_lib.cmi
15-
bs_array.cmj : js_array.cmj bs_array.cmi
15+
bs_vector.cmj : js_array.cmj bs_vector.cmi
1616
bs_list.cmj : bs_list.cmi
1717
bs_option.cmj : bs_option.cmi
1818
bs_result.cmj :
@@ -31,7 +31,7 @@ js_types.cmi :
3131
js_json.cmi : js_types.cmi js_string.cmj js_dict.cmi
3232
bs_dyn.cmi :
3333
bs_dyn_lib.cmi : bs_dyn.cmi
34-
bs_array.cmi :
34+
bs_vector.cmi : bs_list.cmi
3535
bs_list.cmi :
3636
bs_option.cmi :
3737
js_boolean.cmi :

jscomp/others/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ MAP_FILES= node bs
66

77
SOURCE_LIST= node_path node_fs node_process dict node_module js_array js_string \
88
js_re js_null_undefined node_buffer js_types js_json js_obj \
9-
bs_dyn bs_dyn_lib bs_array bs_list bs_option bs_result \
9+
bs_dyn bs_dyn_lib bs_vector bs_list bs_option bs_result \
1010
node_child_process js_boolean js_math js_dict js_date js_global js_cast js_promise dom
1111

1212
$(addsuffix .cmj, $(SOURCE_LIST)): $(addsuffix .cmj, $(MAP_FILES))

jscomp/others/bs.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module Dyn = Bs_dyn
3030
module Dyn_lib = Bs_dyn_lib
3131
(**/*)
3232

33-
module Array = Bs_array
33+
module Vector = Bs_vector
3434
module List = Bs_list
3535
module Option = Bs_option
3636
module Result = Bs_result

jscomp/others/bs_array.mli

-39
This file was deleted.

jscomp/others/bs_list.ml

+19
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,23 @@ let mapRev f ls = mapRevAux f [] ls
7373

7474
let rec map f ls = rev @@ mapRevAux f [] ls
7575

76+
let rec iter f = function
77+
[] -> ()
78+
| a::l -> f a [@bs]; iter f l
79+
80+
let rec iteri i f = function
81+
[] -> ()
82+
| a::l -> f i a [@bs]; iteri (i + 1) f l
83+
84+
let iteri f l = iteri 0 f l
85+
86+
let rec foldLeft f accu l =
87+
match l with
88+
[] -> accu
89+
| a::l -> foldLeft f (f accu a [@bs]) l
90+
91+
let rec foldRight f l accu =
92+
match l with
93+
[] -> accu
94+
| a::l -> f a (foldRight f l accu) [@bs]
7695

jscomp/others/bs_list.mli

+12
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,15 @@ val rev : 'a t -> 'a t
4444
val mapRev : ('a -> 'b [@bs]) -> 'a t -> 'b t
4545

4646
val map : ('a -> 'b [@bs]) -> 'a t -> 'b t
47+
48+
val iter : ('a -> unit [@bs]) -> 'a t -> unit
49+
50+
val iteri : (int -> 'a -> unit [@bs]) -> 'a t -> unit
51+
52+
val foldLeft : ('a -> 'b -> 'a [@bs]) -> 'a -> 'b list -> 'a
53+
(** [Bs.List.foldLeft f a [b1; ...; bn]] is
54+
[f (... (f (f a b1 [@bs]) b2) ...) bn]. *)
55+
56+
val foldRight : ('a -> 'b -> 'b [@bs]) -> 'a list -> 'b -> 'b
57+
(** [Bs.List.foldRight f [a1; ...; an] b] is
58+
[f a1 (f a2 (... (f an b [@bs]) ...))]. Not tail-recursive. *)

jscomp/others/bs_array.ml jscomp/others/bs_vector.ml

+61-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424

2525
type 'a t = 'a array
2626

27+
external length : 'a array -> int = "%array_length"
28+
external get : 'a array -> int -> 'a = "%array_safe_get"
29+
external set : 'a array -> int -> 'a -> unit = "%array_safe_set"
30+
external make: int -> 'a -> 'a array = "caml_make_vect"
31+
external unsafe_get : 'a t -> int -> 'a = "%array_unsafe_get"
32+
external unsafe_set : 'a t -> int -> 'a -> unit = "%array_unsafe_set"
33+
2734
(** @param a array
2835
@param p predicate
2936
*)
@@ -56,12 +63,10 @@ let iter f xs =
5663
f (Array.unsafe_get xs i) [@bs]
5764
done
5865

59-
(* here [f] is of type ['a -> 'b]
60-
let iterX f xs =
61-
for i = 0 to Array.length xs - 1 do
62-
f (Array.unsafe_get xs i)
63-
done
64-
*)
66+
let iteri f a =
67+
for i = 0 to length a - 1 do f i (unsafe_get a i) [@bs] done
68+
69+
6570

6671
external createUnsafe : int -> 'a t = "Array" [@@bs.new]
6772

@@ -75,11 +80,60 @@ let ofList xs =
7580
| hd::tl -> Array.unsafe_set a i hd; fill (i+1) tl in
7681
fill 0 l
7782

83+
let toList a =
84+
let rec tolist i res =
85+
if i < 0 then res else tolist (i - 1) (unsafe_get a i :: res) in
86+
tolist (length a - 1) []
87+
88+
89+
let init n f =
90+
let v = createUnsafe n in
91+
for i = 0 to n - 1 do
92+
unsafe_set v i (f i [@bs])
93+
done ;
94+
v
95+
96+
let copy x =
97+
let len = length x in
98+
let b = createUnsafe len in
99+
for i = 0 to len - 1 do
100+
unsafe_set b i (unsafe_get x i)
101+
done ;
102+
b
103+
78104
let map f a =
79105
let l = Array.length a in
80106
let r = createUnsafe l in
81107
for i = 0 to l - 1 do
82108
Array.unsafe_set r i (f(Array.unsafe_get a i) [@bs])
83109
done;
84110
r
85-
111+
112+
113+
let foldLeft f x a =
114+
let r = ref x in
115+
for i = 0 to length a - 1 do
116+
r := f !r (unsafe_get a i) [@bs]
117+
done;
118+
!r
119+
120+
121+
let foldRight f a x =
122+
let r = ref x in
123+
for i = length a - 1 downto 0 do
124+
r := f (unsafe_get a i) !r [@bs]
125+
done;
126+
!r
127+
128+
129+
let mapi f a =
130+
let l = length a in
131+
if l = 0 then [||] else begin
132+
let r= createUnsafe l in
133+
for i = 0 to l - 1 do
134+
unsafe_set r i (f i (unsafe_get a i) [@bs])
135+
done;
136+
r
137+
end
138+
139+
(* TODO: add [append] *)

jscomp/others/bs_vector.mli

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 array
26+
27+
val filterInPlace : ('a -> bool [@bs]) -> 'a t -> unit
28+
29+
val empty : 'a t -> unit
30+
31+
val pushBack : 'a -> 'a t -> unit
32+
33+
val copy : 'a t -> 'a t
34+
(** shallow copy *)
35+
36+
val memByRef : 'a -> 'a t -> bool
37+
38+
val iter : ('a -> unit [@bs]) -> 'a t -> unit
39+
val iteri : (int -> 'a -> unit [@bs]) -> 'a t -> unit
40+
val ofList : 'a Bs_list.t -> 'a t
41+
val toList : 'a t -> 'a Bs_list.t
42+
43+
val map : ('a -> 'b [@bs]) -> 'a t -> 'b t
44+
val mapi : (int -> 'a -> 'b [@bs]) -> 'a t -> 'b t
45+
val foldLeft : ('a -> 'b -> 'a [@bs]) -> 'a -> 'b t -> 'a
46+
val foldRight : ('b -> 'a -> 'a [@bs]) -> 'b t -> 'a -> 'a
47+
external length : 'a t -> int = "%array_length"
48+
(** Return the length (number of elements) of the given array. *)
49+
50+
external get : 'a t -> int -> 'a = "%array_safe_get"
51+
(** [Array.get a n] returns the element number [n] of array [a].
52+
The first element has number 0.
53+
The last element has number [Array.length a - 1].
54+
You can also write [a.(n)] instead of [Array.get a n].
55+
56+
Raise [Invalid_argument "index out of bounds"]
57+
if [n] is outside the range 0 to [(Array.length a - 1)]. *)
58+
59+
external set : 'a t -> int -> 'a -> unit = "%array_safe_set"
60+
(** [Array.set a n x] modifies array [a] in place, replacing
61+
element number [n] with [x].
62+
You can also write [a.(n) <- x] instead of [Array.set a n x].
63+
64+
Raise [Invalid_argument "index out of bounds"]
65+
if [n] is outside the range 0 to [Array.length a - 1]. *)
66+
67+
68+
external make : int -> 'a -> 'a t = "caml_make_vect"
69+
(** [Array.make n x] returns a fresh array of length [n],
70+
initialized with [x].
71+
All the elements of this new array are initially
72+
physically equal to [x] (in the sense of the [==] predicate).
73+
Consequently, if [x] is mutable, it is shared among all elements
74+
of the array, and modifying [x] through one of the array entries
75+
will modify all other entries at the same time.
76+
77+
Raise [Invalid_argument] if [n < 0] or [n > Sys.max_array_length].
78+
If the value of [x] is a floating-point number, then the maximum
79+
size is only [Sys.max_array_length / 2].*)
80+
81+
82+
val init : int -> (int -> 'a [@bs]) -> 'a t
83+
(** @raise RangeError *)
84+
85+
(*val append : 'a t -> 'a t -> 'a t *)
86+
(** create a new array, there is no shallow-sharing
87+
between the output and input
88+
*)
89+
90+
external unsafe_get : 'a t -> int -> 'a = "%array_unsafe_get"
91+
external unsafe_set : 'a t -> int -> 'a -> unit = "%array_unsafe_set"
92+
93+

jscomp/runtime/.depend

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ caml_gc.cmj : caml_gc.cmi
2525
js_typed_array.cmj : js.cmj
2626
js_primitive.cmj : js_undefined.cmj js.cmj js_primitive.cmi
2727
caml_basic.cmj : js_undefined.cmj js.cmj caml_basic.cmi
28-
caml_oo.cmj : caml_array.cmj bs_obj.cmj caml_oo.cmi
28+
caml_oo.cmj : bs_obj.cmj caml_oo.cmi
2929
curry.cmj : caml_array.cmj
3030
caml_oo_curry.cmj : curry.cmj caml_oo.cmj
3131
caml_module.cmj :

0 commit comments

Comments
 (0)