From 9e2ce41784f1ccd717148ccd8f1ff48d8b59c68e Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Mon, 5 Feb 2018 20:58:01 +0800 Subject: [PATCH 1/3] more docs --- jscomp/others/bs.ml | 87 ++++++++++++++++++++++++++++++++++++++++- jscomp/others/intro.txt | 37 ++++++++++++++++++ scripts/doc_gen.js | 2 +- 3 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 jscomp/others/intro.txt diff --git a/jscomp/others/bs.ml b/jscomp/others/bs.ml index 9038217b6e..8bb8cfe470 100644 --- a/jscomp/others/bs.ml +++ b/jscomp/others/bs.ml @@ -23,7 +23,92 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -(** A stdlib shipped with BuckleScript *) +(** A stdlib shipped with BuckleScript + + This stdlib is still in beta status, but we encourage you to try it out and + provide feedback. + + {b Motivation } + + The motivation of creating such library is to provide BuckleScript users a + better end-to-end user experience, since the original OCaml stdlib was not + writte with JS platform in mind, below are a list of areas this lib aims to + improve: {ol + {- 1. Consistency in name convention: camlCase, and arguments order} + {- 2. Exception thrown functions are all suffixed with {i Exn}, e.g, {i getExn}} + {- 3. Beter peformance and smaller code size running on JS platform} + } + + {b Name Convention} + + For higher order functions, it will be suffixed {b U} if it takes uncurried + callback. + + {[ + val forEach : 'a t -> ('a -> unit) -> unit + val forEachU : 'a t -> ('a -> unit [\@bs]) -> unit + ]} + + In general, uncurried version will be faster, but it is less familiar to + people who have a background in functional programming. + + {b A special encoding for collection safety} + + When we create a collection library for a custom data type, take {i Set} for + example, suppose its element type is a pair of ints, + it needs a custom {i compare} function. However, the {i Set} could not + just be typed as [ Set.t (int * int) ], + its customized {i compare} function needs to be + manifested in the signature, otherwise, if the user create another + customized {i compare} function, and the two collection would mix which + would result in runtime error. + + The original OCaml stdlib solved the problem using {i functor} which is a big + closure in runtime; it makes dead code elimination much harder. + We introduced a phantom type to solve the problem + + {[ + type t = int * int + module I0 = + (val Bs.Dict.comparableU ~cmp:(fun[\@bs] ((a0,a1) : t) ((b0,b1) : t) -> + match compare a0 b0 with + | 0 -> compare a1 b1 + | c -> c + )) + let s0 = Bs.Set.make (module I0) + module I1 = + (val Bs.Dict.comparableU ~cmp:(fun[\@bs] ((a0,a1) : t) ((b0,b1) : t) -> + match compare a1 b1 with + | 0 -> compare a0 b0 + | c -> c + )) + let s1 = Bs.Set.make (module I1) + ]} + + Here the compiler would infer [s0] and [s1] having different type so that + it would not mix. + + {[ + val s0 : Bs.Set.t ((int * int), I0.id) + val s1 : Bs.Set.t ((int * int), I1.id) + ]} + + + {b Collection Hierachy} + + In general, we provide a generic collection module, but also create specialized + modules for commonly used data type, take {i Bs.Set} for example + + {[ + Bs.Set + Bs.Set.Int + Bs.Set.String + ]} + + The specialized module {i Bs.Set.Int}, {i Bs.Set.String} is in general more + efficient + +*) (** {!Bs.Dict} diff --git a/jscomp/others/intro.txt b/jscomp/others/intro.txt new file mode 100644 index 0000000000..41ba942138 --- /dev/null +++ b/jscomp/others/intro.txt @@ -0,0 +1,37 @@ +{!indexlist} + +{1 Libraries shipped with BuckleScript} + +BuckleScript is mostly a compiler, but it does ship some libraries for users' convenience + +{2 4 libraries} + +{!Js} + +This library are mostly {i bindings} to JS, it should work with both NodeJS and Browser. +It is strongly recommended to use qualified name instead of flatten module name. +For example +{[ + [| 1; 2 ; 3 |] + |> Js.Array.map (fun x -> x + 1 ) + |> Js.log +]} + +{!Bs} + +This BuckleScript stdandard library. + +BuckleScript also shipped the vanilla OCaml standard library. +@see OCaml standard library + +{!Node} + +This library contains bindings to NodeJS, it is still work in progress, use it with care, +and we may break API backward compatiblity in the future. + +{!Dom} + +This library are for DOM API, currently it only defines some +types for diferent packages to talk to each other + + diff --git a/scripts/doc_gen.js b/scripts/doc_gen.js index 269f90a859..7b91f15fac 100644 --- a/scripts/doc_gen.js +++ b/scripts/doc_gen.js @@ -35,7 +35,7 @@ var others_files = var odoc_gendir = path.join(__dirname,'..', 'odoc_gen') var bsppx = path.join(__dirname,'..','lib','bsppx.exe') var api_doc_dir = path.join(__dirname,'..','docs','api') -var intro = path.join(__dirname,'..','docs','api','intro.txt') +var intro = path.join(__dirname,'..','jscomp','others','intro.txt') // var generator = `-g ${odoc_gendir}/generator.cmxs` // var generator = `-html` From a5846d9779d160cbb98acf8b06f5865aa171ac7b Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 6 Feb 2018 12:30:06 +0800 Subject: [PATCH 2/3] add more docs --- jscomp/others/bs.ml | 23 ++- jscomp/others/bs_Array.ml | 2 +- jscomp/others/bs_Array.mli | 106 +++++++--- jscomp/others/bs_Dict.mli | 4 +- jscomp/others/bs_List.ml | 58 ++++-- jscomp/others/bs_List.mli | 90 ++++++--- jscomp/others/bs_MutableQueue.mli | 26 ++- jscomp/others/bs_MutableStack.ml | 2 +- jscomp/others/bs_MutableStack.mli | 18 +- jscomp/others/bs_SortArray.mli | 41 ++-- jscomp/others/bs_SortArrayInt.mli | 16 +- jscomp/others/bs_SortArrayString.mli | 16 +- jscomp/others/sort.cppo.mli | 12 +- jscomp/test/bs_array_test.js | 166 +++++++++------- jscomp/test/bs_array_test.ml | 5 + jscomp/test/bs_list_test.js | 284 ++++++++++++++++++++------- jscomp/test/bs_list_test.ml | 24 ++- jscomp/test/bs_sort_test.js | 48 +++-- jscomp/test/bs_sort_test.ml | 1 + jscomp/test/bs_stack_test.ml | 6 +- lib/js/bs_Array.js | 4 +- lib/js/bs_List.js | 108 +++++++++- lib/js/bs_MutableStack.js | 4 +- 23 files changed, 765 insertions(+), 299 deletions(-) diff --git a/jscomp/others/bs.ml b/jscomp/others/bs.ml index 8bb8cfe470..c89a4f60ee 100644 --- a/jscomp/others/bs.ml +++ b/jscomp/others/bs.ml @@ -106,7 +106,12 @@ ]} The specialized module {i Bs.Set.Int}, {i Bs.Set.String} is in general more - efficient + efficient. + + Currently, both {i Bs_Set} and {i Bs.Set} are accessible to users for some + technical rasons, + we {b strongly recommend} users stick to qualified import, {i Bs.Sort}, we may hide + the internal, {i i.e}, {i Bs_Set} in the future *) @@ -122,8 +127,8 @@ module Dict = Bs_Dict (** {!Bs.Array} - - Utililites for Array functions + + {b mutable array}: Utililites functions *) module Array = Bs_Array @@ -162,7 +167,7 @@ module Range = Bs_Range (** {!Bs.Set} - The toplevel provides generic immutable set operations. + The toplevel provides generic {b immutable} set operations. It also has three specialized inner modules {!Bs.Set.Int} and {!Bs.Set.String} @@ -175,7 +180,7 @@ module Set = Bs_Set (** {!Bs.Map}, - The toplevel provides generic immutable map operations. + The toplevel provides generic {b immutable} map operations. It also has three specialized inner modules {!Bs.Map.Int} and {!Bs.Map.String} @@ -187,7 +192,7 @@ module Map = Bs_Map (** {!Bs.MutableSet} - The toplevel provides generic mutable set operations. + The toplevel provides generic {b mutable} set operations. It also has two specialized inner modules {!Bs.MutableSet.Int} and {!Bs.MutableSet.String} @@ -196,7 +201,7 @@ module MutableSet = Bs_MutableSet (** {!Bs.MutableMap} - The toplevel provides generic mutable map operations. + The toplevel provides generic {b mutable} map operations. It also has two specialized inner modules {!Bs.MutableMap.Int} and {!Bs.MutableMap.String} @@ -207,7 +212,7 @@ module MutableMap = Bs_MutableMap (** {!Bs.HashSet} - The toplevel provides generic mutable hash set operations. + The toplevel provides generic {b mutable} hash set operations. It also has two specialized inner modules {!Bs.HashSet.Int} and {!Bs.HashSet.String} @@ -217,7 +222,7 @@ module HashSet = Bs_HashSet (** {!Bs.HashMap} - The toplevel provides generic mutable hash map operations. + The toplevel provides generic {b mutable} hash map operations. It also has two specialized inner modules {!Bs.HashMap.Int} and {!Bs.HashMap.String} diff --git a/jscomp/others/bs_Array.ml b/jscomp/others/bs_Array.ml index ee97d44668..9dd6d6a790 100644 --- a/jscomp/others/bs_Array.ml +++ b/jscomp/others/bs_Array.ml @@ -25,7 +25,7 @@ let getExn arr i = [%assert i >= 0 && i < length arr] ; getUnsafe arr i let set arr i v = - if i >= 0 && i < length arr then setUnsafe arr i v + if i >= 0 && i < length arr then (setUnsafe arr i v; true) else false let setExn arr i v = [%assert i >= 0 && i < length arr]; diff --git a/jscomp/others/bs_Array.mli b/jscomp/others/bs_Array.mli index 445f79ddf2..34ca6214dc 100644 --- a/jscomp/others/bs_Array.mli +++ b/jscomp/others/bs_Array.mli @@ -22,33 +22,39 @@ external length: 'a array -> int = "%array_length" external size: 'a array -> int = "%array_length" (** [size arr] is the same as [length arr] *) -val get : 'a array -> int -> 'a option +val get: 'a array -> int -> 'a option val getExn: 'a array -> int -> 'a (** [getExn arr i] - raise an exception if [i] is out of range + + {b raise} an exception if [i] is out of range *) external getUnsafe: 'a array -> int -> 'a = "%array_unsafe_get" (** [getUnasfe arr i] - It does not do bounds checking, this would cause type error + + {b Unsafe} + + no bounds checking, this would cause type error if [i] does not stay within range *) external getUndefined: 'a array -> int -> 'a Js.undefined = "%array_unsafe_get" -(** [getUndefined arr i] does the samething in the runtime as [getUnsafe], - it is type safe since the return type still track whether it is +(** [getUndefined arr i] + + It does the samething in the runtime as {!getUnsafe}, + it is {i type safe} since the return type still track whether it is in range or not *) -val set : 'a array -> int -> 'a -> unit +val set: 'a array -> int -> 'a -> bool (** [set arr n x] modifies [arr] in place, it replaces the nth element of [arr] with [x] - Nothing happens if [n] is out of range + @return false means not updated due to out of range *) val setExn: 'a array -> int -> 'a -> unit (** [setExn arr i x] - raise an exception if [i] is out of range + {b raise} an exception if [i] is out of range *) external setUnsafe: 'a array -> int -> 'a -> unit = "%array_unsafe_set" @@ -56,23 +62,29 @@ external setUnsafe: 'a array -> int -> 'a -> unit = "%array_unsafe_set" val shuffleInPlace: 'a array -> unit val shuffle: 'a array -> 'a array -(** [shuffle xs] returns a new array *) +(** [shuffle xs] + @return a fresh array *) val reverseInPlace: 'a array -> unit val reverse: 'a array -> 'a array -(** [reverse x] returns a new array *) +(** [reverse x] + @return a fresh array *) external makeUninitialized: int -> 'a Js.undefined array = "Array" [@@bs.new] external makeUninitializedUnsafe: int -> 'a array = "Array" [@@bs.new] +(** [makeUninitializedUnsafe n] + + {b Unsafe} +*) + val make: int -> 'a -> 'a array (** [make n e] - - return an empty array when [n] is negative. - return an array of size [n] with value [e] - *) + return an array of size [n] filled with value [e] + @return an empty array when [n] is negative. +*) val makeByU: int -> (int -> 'a [@bs]) -> 'a array val makeBy: int -> (int -> 'a ) -> 'a array @@ -91,7 +103,11 @@ val makeByAndShuffle: int -> (int -> 'a ) -> 'a array val zip: 'a array -> 'b array -> ('a * 'b) array (** [zip a b] - stop with the shorter array + stop with the shorter array + + @example {[ + zip [|1;2] [|1;2;3|] = [| (1,2); (2;2)|] + ]} *) @@ -100,13 +116,13 @@ val zip: 'a array -> 'b array -> ('a * 'b) array (** [zipBy a b f] - stops with shorter array + stops with shorter array *) val concat: 'a array -> 'a array -> 'a array (** [concat xs ys] - return a fresh array containing the + @return a fresh array containing the concatenation of the arrays [v1] and [v2], so even if [v1] or [v2] is empty, it can not be shared *) @@ -115,7 +131,7 @@ val concatMany: 'a array array -> 'a array (** [concatMany xss] - return a fresh array as the concatenation of [xss] + @return a fresh array as the concatenation of [xss] *) val slice: 'a array -> offset:int -> len:int -> 'a array @@ -134,14 +150,14 @@ val slice: 'a array -> offset:int -> len:int -> 'a array val copy: 'a array -> 'a array (** [copy a] - returns a copy of [a], that is, a fresh array + @return a copy of [a], that is, a fresh array containing the same elements as [a]. *) val fill: 'a array -> offset:int -> len:int -> 'a -> unit -(** [fill arr ofs len x] +(** [fill arr ~offset ~len x] modifies [arr] in place, - storing [x] in elements number [ofs] to [ofs + len - 1]. + storing [x] in elements number [offset] to [offset + len - 1]. [offset] can be negative @@ -151,7 +167,7 @@ val fill: 'a array -> offset:int -> len:int -> 'a -> unit val blit: src:'a array -> srcOffset:int -> dst:'a array -> dstOffset:int -> len:int -> unit -(** [blit v1 o1 v2 o2 len] +(** [blit ~src:v1 ~srcOffset:o1 ~dst:v2 ~dstOffset:o2 ~len] copies [len] elements from array [v1], starting at element number [o1], to array [v2], @@ -177,26 +193,61 @@ val map: 'a array -> ('a -> 'b ) -> 'b array val keepU: 'a array -> ('a -> bool [@bs]) -> 'a array val keep: 'a array -> ('a -> bool ) -> 'a array - +(** [keep xs p ] + @return a new array that keep all elements satisfy [p] +*) val keepMapU: 'a array -> ('a -> 'b option [@bs]) -> 'b array val keepMap: 'a array -> ('a -> 'b option) -> 'b array - +(** [keepMap xs p] + @return a new array that keep all elements that return a non-None applied [p] + + @example {[ + keepMap [|1;2;3|] (fun x -> if x mod 2 then Some x else None) + = [| 2 |] + ]} +*) + val forEachWithIndexU: 'a array -> (int -> 'a -> unit [@bs]) -> unit val forEachWithIndex: 'a array -> (int -> 'a -> unit ) -> unit +(** [forEachWithIndex xs f] + + The same with {!forEach}, except that [f] is supplied with one + more argument: the index starting from 0 +*) val mapWithIndexU: 'a array -> (int -> 'a -> 'b [@bs]) -> 'b array val mapWithIndex: 'a array -> (int -> 'a -> 'b ) -> 'b array +(** [mapWithIndex xs f ] + The same with {!map} except that [f] is supplied with one + more argument: the index starting from 0 +*) + val reduceU: 'b array -> 'a -> ('a -> 'b -> 'a [@bs]) ->'a val reduce: 'b array -> 'a -> ('a -> 'b -> 'a ) ->'a +(** [reduce xs init f] + + @example {[ + reduce [|2;3;4|] 1 (+) = 10 + ]} + +*) val reduceReverseU: 'b array -> 'a -> ('a -> 'b -> 'a [@bs]) -> 'a val reduceReverse: 'b array -> 'a -> ('a -> 'b -> 'a ) -> 'a +(** [reduceReverse xs init f] + @example {[ + reduceReverse [|1;2;3;4|] 100 (-) = 90 + ]} +*) val everyU: 'a array -> ('a -> bool [@bs]) -> bool val every: 'a array -> ('a -> bool ) -> bool - +(** [every xs p] + @return true if all elements satisfy [p] +*) + val every2U: 'a array -> 'b array -> ('a -> 'b -> bool [@bs]) -> bool val every2: 'a array -> 'b array -> ('a -> 'b -> bool ) -> bool (** [every2 a b p] @@ -208,8 +259,8 @@ val cmpU: 'a array -> 'a array -> ('a -> 'a -> int [@bs]) -> int val cmp: 'a array -> 'a array -> ('a -> 'a -> int ) -> int (** [cmp a b] - if [length a <> length b] compared by length - otherwise compare one by one [f ai bi] + - compared by length if [length a <> length b] + - otherwise compare one by one [f ai bi] *) val eqU: 'a array -> 'a array -> ('a -> 'a -> bool [@bs]) -> bool @@ -221,3 +272,4 @@ val eq: 'a array -> 'a array -> ('a -> 'a -> bool ) -> bool *) external truncateToLengthUnsafe: 'a array -> int -> unit = "length" [@@bs.set] +(** {b Unsafe} function *) diff --git a/jscomp/others/bs_Dict.mli b/jscomp/others/bs_Dict.mli index 5aa0877c55..fbfb310ec4 100644 --- a/jscomp/others/bs_Dict.mli +++ b/jscomp/others/bs_Dict.mli @@ -69,7 +69,7 @@ type ('key, 'id) comparable = Unlike normal functions, when created, it comes with a unique identity (guaranteed by the type system). - It can be created using function {!comparable} + It can be created using function {!comparableU} or{!comparable}. The idea of a unique identity when created is that it makes sure two sets would type mismatch if they use different comparison function @@ -97,7 +97,7 @@ type ('key, 'id) hashable = Unlike normal functions, when created, it comes with a unique identity (guaranteed by the type system). - It can be created using function {!hashable} + It can be created using function {!hashableU} or {!hashable}. The idea of a unique identity when created is that it makes sure two hash sets would type mismatch if they use different comparison function diff --git a/jscomp/others/bs_List.ml b/jscomp/others/bs_List.ml index e24382556f..09a874e235 100644 --- a/jscomp/others/bs_List.ml +++ b/jscomp/others/bs_List.ml @@ -88,11 +88,21 @@ let head x = | [] -> None | x::_ -> Some x +let headExn x = + match x with + | [] -> [%assert "headExn"] + | x::_ -> x + let tail x = match x with | [] -> None | _::xs -> Some xs +let tailExn x = + match x with + | [] -> [%assert "tailExn"] + | _::t -> t + let add xs x = x :: xs (* Assume [n >=0] *) @@ -180,25 +190,26 @@ let rec copyAuxWitFilterMap f cellX prec = let rec removeAssocAuxWithMap cellX x prec f = match cellX with - | [] -> () + | [] -> false | ((a,_) as h):: t -> if f a x [@bs] then - unsafeMutateTail prec t + (unsafeMutateTail prec t ; true) else let next = mutableCell h [] in unsafeMutateTail prec next ; removeAssocAuxWithMap t x next f -let rec removeAssocAuxByReference cellX x prec = - match cellX with - | [] -> () - | ((a,_) as h):: t -> - if a == x then - unsafeMutateTail prec t - else +let rec setAssocAuxWithMap cellX x k prec eq = + match cellX with + | [] -> false + | ((a,_) as h) :: t -> + if eq a x [@bs] then + (unsafeMutateTail prec ( (x,k)::t); true) + else let next = mutableCell h [] in unsafeMutateTail prec next ; - removeAssocAuxByReference t x next + setAssocAuxWithMap t x k next eq + let rec copyAuxWithMap cellX prec f = match cellX with @@ -601,14 +612,14 @@ let rec hasU xs x eq = let has xs x eq = hasU xs x (fun [@bs] a b -> eq a b) -let rec assocU xs x eq = +let rec getAssocU xs x eq = match xs with | [] -> None | (a,b)::l -> if eq a x [@bs] then Some b - else assocU l x eq + else getAssocU l x eq -let assoc xs x eq = assocU xs x (fun[@bs] a b -> eq a b) +let getAssoc xs x eq = getAssocU xs x (fun[@bs] a b -> eq a b) let rec hasAssocU xs x eq = @@ -618,7 +629,8 @@ let rec hasAssocU xs x eq = let hasAssoc xs x eq = hasAssocU xs x (fun[@bs] a b -> eq a b) -(* remove the first pair *) + + let removeAssocU xs x eq = match xs with | [] -> [] @@ -626,11 +638,25 @@ let removeAssocU xs x eq = if eq a x [@bs] then l else let cell = mutableCell pair [] in - removeAssocAuxWithMap l x cell eq ; - cell + let removed = removeAssocAuxWithMap l x cell eq in + if removed then + cell + else xs let removeAssoc xs x eq = removeAssocU xs x (fun [@bs] a b -> eq a b) +let setAssocU xs x k eq = + match xs with + | [] -> [] + | (a, _ as pair) :: l -> + if eq a x [@bs] then l + else + let cell = mutableCell pair [] in + let replaced = setAssocAuxWithMap l x k cell eq in + if replaced then cell + else (x,k)::xs + +let setAssoc xs x k eq = setAssocU xs x k (fun [@bs] a b -> eq a b) let rec getByU xs p = match xs with diff --git a/jscomp/others/bs_List.mli b/jscomp/others/bs_List.mli index 188e7a9ff1..984bee0862 100644 --- a/jscomp/others/bs_List.mli +++ b/jscomp/others/bs_List.mli @@ -28,7 +28,7 @@ *) type 'a t = 'a list -(** [t] is compatible with built-in [list] type *) +(** ['a t] is compatible with built-in [list] type *) val length: 'a t -> int (** [length l] @@ -40,8 +40,20 @@ val size: 'a t -> int val head: 'a t -> 'a option +val headExn: 'a t -> 'a +(** [headExn h] + + {b raise} an exception if [h] is emmpty +*) + val tail: 'a t -> 'a t option +val tailExn: 'a t -> 'a t +(** [tailExn h] + + {b raise} an exception if [h] is empty +*) + val add: 'a t -> 'a -> 'a t val get: 'a t -> int -> 'a option @@ -72,6 +84,9 @@ val makeBy: int -> (int -> 'a) -> 'a t - return a list of length [n] with element [i] initialized with [f i] - return the empty list if [n] is negative + @example {[ + makeBy 5 (fun i -> i) = [0;1;2;3;4] + ]} *) val drop: 'a t -> int -> 'a t option @@ -108,6 +123,9 @@ val concatMany: 'a t array -> 'a t val reverseConcat: 'a t -> 'a t -> 'a t (** [reverseConcat xs ys] is equivalent to [concat (reverse xs) ys] + @example {[ + reverseConcat [1;2] [3;4] = [2;1;3;4] + ]} *) val flatten: 'a t t -> 'a t @@ -223,15 +241,19 @@ val every2: 'a t -> 'b t -> ('a -> 'b -> bool) -> bool val cmpU: 'a t -> 'a t -> ('a -> 'a -> int [@bs]) -> int val cmp: 'a t -> 'a t -> ('a -> 'a -> int) -> int (** - [cmp xs ys cmp_elem] - compare lists [xs] and [ys] using [cmp_elem] to compare elements + [cmp xs ys cmpElem] + compare lists [xs] and [ys] using [cmpElem] to compare elements + @example {[ + cmp [1;2;3] [1;2;3] compare = 0 + ]} *) + val eqU: 'a t -> 'a t -> ('a -> 'a -> bool [@bs]) -> bool val eq: 'a t -> 'a t -> ('a -> 'a -> bool) -> bool (** - [eq xs ys eq_elem] - check equality of [xs] and [ys] using [eq_elem] for equality on elements + [eq xs ys eqElem] + check equality of [xs] and [ys] using [eqElem] for equality on elements @example {[ eq [1;2;3] [1;2] (=) = false ;; @@ -260,33 +282,49 @@ val partition: 'a t -> ('a -> bool) -> 'a t * 'a t val unzip: ('a * 'b) t -> 'a t * 'b t -val assocU: ('a * 'c) t -> 'b -> ('a -> 'b -> bool [@bs]) -> 'c option -val assoc: ('a * 'c) t -> 'b -> ('a -> 'b -> bool) -> 'c option -(** - [assoc xs x eq] - return the second element of a pair in [xs] where the first element equals [x], - or [None] if not found +val getAssocU: ('a * 'c) t -> 'b -> ('a -> 'b -> bool [@bs]) -> 'c option +val getAssoc: ('a * 'c) t -> 'b -> ('a -> 'b -> bool) -> 'c option +(** [getAssoc xs k eq] + + return the second element of a pair in [xs] where the first element equals [x], + or [None] if not found + @example {[ + getAssoc [ 1, "a"; 2, "b"; 3, "c"] 2 (=) = Some "b" + ]} *) val hasAssocU: ('a * 'c) t -> 'b -> ('a -> 'b -> bool [@bs]) -> bool -val hasAssoc: ('a * 'c) t -> 'b -> ('a -> 'b -> bool) -> bool -(** - [hasAssoc xs x eq] - return true if there is a pair in [xs] where the first element equals [x] +val hasAssoc: ('a * 'c) t -> 'b -> ('a -> 'b -> bool ) -> bool +(** [hasAssoc xs x eq] + return true if there is a pair in [xs] where the first element equals [x] + @example {[ + hasAssoc [1, "a"; 2, "b"; 3,"c"] 1 (=) = true + ]} *) -val removeAssocU: - ('a * 'c) t -> - 'b -> - ('a -> 'b -> bool [@bs]) -> ('a * 'c) t -val removeAssoc: - ('a * 'c) t -> - 'b -> - ('a -> 'b -> bool) -> ('a * 'c) t -(** - [removeAssoc xs x eq] - remove pairs from list [xs] where the first element equals [x] +val removeAssocU:('a * 'c) t -> 'b -> ('a -> 'b -> bool [@bs]) -> ('a * 'c) t +val removeAssoc: ('a * 'c) t -> 'b -> ('a -> 'b -> bool) -> ('a * 'c) t +(** [removeAssoc xs x eq] + Try to remove the first pair, if not found, leave it untouched. + @example {[ + removeAssoc [1,"a"; 2, "b"; 3, "c" ] 1 (=) = + [2, "b"; 3, "c"] + ]} *) +val setAssocU: ('a * 'c) t -> 'a -> 'c -> ('a -> 'a -> bool [@bs]) -> ('a * 'c) t +val setAssoc: ('a * 'c) t -> 'a -> 'c -> ('a -> 'a -> bool) -> ('a * 'c) t +(** [setAssoc xs x eq] + + @example {[ + setAssoc [1,"a"; 2, "b"; 3, "c"] 2 "x" (=) = + [1,"a"; 2, "x"; 3,"c"] ;; + + setAssoc [1,"a"; 3, "c"] 2 "2" (=) = + [2,"2"; 1,"a"; 3, "c"] + ]} +*) + + diff --git a/jscomp/others/bs_MutableQueue.mli b/jscomp/others/bs_MutableQueue.mli index 5006ec84c0..fc49feaf1a 100644 --- a/jscomp/others/bs_MutableQueue.mli +++ b/jscomp/others/bs_MutableQueue.mli @@ -22,15 +22,17 @@ type 'a t (** The type of queues containing elements of type ['a]. *) val make: unit -> 'a t -(** Return a new queue, initially empty. *) +(** @return a new queue, initially empty. *) val clear: 'a t -> unit -(** Discard all elements from a queue. *) +(** Discard all elements from the queue. *) val isEmpty: 'a t -> bool -(** Return [true] if the given queue is empty, [false] otherwise. *) +(** @return [true] if the given queue is empty, [false] otherwise. *) + val ofArray: 'a array -> 'a t -(** [ofArray a] is equivalent to [Array.forEach a (Queue.add q a)] *) +(** [ofArray a] is equivalent to [Array.forEach a (add q a)] *) + val add: 'a t -> 'a -> unit (** [add q x] adds the element [x] at the end of the queue [q]. *) @@ -41,6 +43,9 @@ val peek: 'a t -> 'a option val peekUndefined: 'a t -> 'a Js.undefined (** [peekUndefined q] returns [undefined] if not found *) val peekExn: 'a t -> 'a +(** [peekExn q] + + {b raise} an exception if [q] is empty *) val pop: 'a t -> 'a option (** [pop q] removes and returns the first element in queue [q].*) @@ -51,25 +56,32 @@ val popUndefined: 'a t -> 'a Js.undefined *) val popExn: 'a t -> 'a +(** [popExn q] + {b raise} an exception if [q] is empty +*) + val copy: 'a t -> 'a t +(** [copy q] + @return a fresh queue +*) val size: 'a t -> int -(** Return the number of elements in a queue. *) +(** @return the number of elements in a queue. *) val mapU: 'a t -> ('a -> 'b [@bs]) -> 'b t val map: 'a t -> ('a -> 'b ) -> 'b t val forEachU: 'a t -> ('a -> unit [@bs]) -> unit val forEach: 'a t -> ('a -> unit ) -> unit -(** [reduce f q] applies [f] in turn to all elements of [q], +(** [forEach q f] applies [f] in turn to all elements of [q], from the least recently entered to the most recently entered. The queue itself is unchanged. *) val reduceU: 'a t -> 'b -> ('b -> 'a -> 'b [@bs]) -> 'b val reduce: 'a t -> 'b -> ('b -> 'a -> 'b ) -> 'b -(** [reduce q accu f] is equivalent to [List.reduce f accu l], +(** [reduce q accu f] is equivalent to [List.reduce l accu f], where [l] is the list of [q]'s elements. The queue remains unchanged. *) diff --git a/jscomp/others/bs_MutableStack.ml b/jscomp/others/bs_MutableStack.ml index 5477cb9576..8bf40a9d1a 100644 --- a/jscomp/others/bs_MutableStack.ml +++ b/jscomp/others/bs_MutableStack.ml @@ -31,7 +31,7 @@ and 'a cell = { } [@@bs.deriving abstract] -let create () = t ~root:Js.null +let make () = t ~root:Js.null let clear s = rootSet s Js.null diff --git a/jscomp/others/bs_MutableStack.mli b/jscomp/others/bs_MutableStack.mli index 9e061af148..cf89ce36d8 100644 --- a/jscomp/others/bs_MutableStack.mli +++ b/jscomp/others/bs_MutableStack.mli @@ -22,16 +22,21 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +(** First in last out stack. -type 'a t + This module implements stacks, with in-place modification. +*) +type 'a t -val create : unit -> 'a t -val clear : 'a t -> unit +val make: unit -> 'a t +(** @return a new stack, initially empty. *) +val clear: 'a t -> unit +(** Discard all elements from the stack. *) val copy : 'a t -> 'a t -(** [copy x] O(1) *) +(** [copy x] O(1) operation, return a new stack *) val push : 'a t -> 'a -> unit @@ -54,7 +59,8 @@ val forEach : 'a t -> ('a -> unit ) -> unit val dynamicPopIterU : 'a t -> ('a -> unit [@bs]) -> unit val dynamicPopIter : 'a t -> ('a -> unit ) -> unit (** [dynamicPopIter s f ] - apply [f] to each element of [s]. The item is poped - before applying [f], [s] will be empty after this opeartion + apply [f] to each element of [s]. The item is poped + before applying [f], [s] will be empty after this opeartion. + This function is useful for worklist algorithm *) diff --git a/jscomp/others/bs_SortArray.mli b/jscomp/others/bs_SortArray.mli index 3d9a70a140..9e96ede547 100644 --- a/jscomp/others/bs_SortArray.mli +++ b/jscomp/others/bs_SortArray.mli @@ -23,14 +23,15 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -(** specalized when key type is [int], more efficient - than the gerneic type -*) +(** A module for Array sort relevant utiliites *) + module Int = Bs_SortArrayInt +(** Specalized when key type is [int], more efficient + than the gerneic type *) -(** specalized when key type is [string], more efficient - than the gerneic type *) module String = Bs_SortArrayString +(** Specalized when key type is [string], more efficient + than the gerneic type *) val strictlySortedLengthU: @@ -45,25 +46,39 @@ val strictlySortedLength: [strictlySortedLenght xs cmp] return [+n] means increasing order [-n] means negative order + + @example{[ + strictlySortedLength [|1;2;3;4;3|] (fun x y -> x < y) = 4;; + strictlySortedLength [||] (fun x y -> x < y) = 0;; + strictlySortedLength [|1|] (fun x y -> x < y) = 1;; + strictlySortedLength [|4;3;2;1|] (fun x y -> x < y) = -4;; + ]} *) val isSortedU: 'a array -> ('a -> 'a -> int [@bs]) -> bool val isSorted: 'a array -> ('a -> 'a -> int) -> bool (** [isSorted arr cmp] - returns true if array is increasingly sorted - , equal is okay - for example - {[ - isSorted [|1;1;2;3;4|] intCmp = true + @return true if array is increasingly sorted (equal is okay ) + @example {[ + isSorted [|1;1;2;3;4|] (fun x y -> compare x y)) = true ]} *) val stableSortInPlaceByU: 'a array -> ('a -> 'a -> int [@bs]) -> unit -val stableSortInPlaceBy: 'a array -> ('a -> 'a -> int ) -> unit +val stableSortInPlaceBy: 'a array -> ('a -> 'a -> int ) -> unit +(** [stableSortBy xs cmp] + + Sort xs in place using comparator [cmp], the stable means if the elements + are equal, their order will be preserved +*) val stableSortByU: 'a array -> ('a -> 'a -> int [@bs]) -> 'a array val stableSortBy: 'a array -> ('a -> 'a -> int) -> 'a array +(** [stableSort xs cmp] + @return a fresh array + The same as {!stableSortInPlaceBy} except that [xs] is not modified +*) @@ -84,6 +99,10 @@ val binarySearchBy: for example, if [key] is smaller than all elements return [-1] since [lnot (-1) = 0] if [key] is larger than all elements return [- (len + 1)] since [lnot (-(len+1)) = len] + @example {[ + binarySearchBy [|1;2;3;4;33;35;36|] 33 = 4;; + lnot (binarySearchBy [|1;3;5;7|] 4) = 2;; + ]} *) (**/**) diff --git a/jscomp/others/bs_SortArrayInt.mli b/jscomp/others/bs_SortArrayInt.mli index ed5aaa2cfe..11bf3a5f37 100644 --- a/jscomp/others/bs_SortArrayInt.mli +++ b/jscomp/others/bs_SortArrayInt.mli @@ -25,16 +25,18 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +(** This is a specialized module for {!Bs_SortArray}, the docs in that module also + applies here, except the comparator is fixed and inlined +*) -# 29 +# 32 type element = int -# 36 +# 39 val strictlySortedLength: element array -> int (** - [strictlySortedLenght xs] - return [+n] means increasing order - [-n] means negative order + The same as {!Bs_SortArray.strictlySortedLength } except the comparator is fixed + @return [+n] means increasing order [-n] means negative order *) @@ -42,8 +44,12 @@ val isSorted: element array -> bool (** [sorted xs] return true if [xs] is in non strict increasing order *) val stableSortInPlace: element array -> unit +(** + The same as {!Bs_SortArray.stableSortInPlaceBy} except the comparator is fixed +*) val stableSort: element array -> element array +(** The same as {!Bs_SortArray.stableSortBy} except the comparator is fixed *) val binarySearch: element array -> element -> int (** diff --git a/jscomp/others/bs_SortArrayString.mli b/jscomp/others/bs_SortArrayString.mli index 689243d6aa..276f52f2af 100644 --- a/jscomp/others/bs_SortArrayString.mli +++ b/jscomp/others/bs_SortArrayString.mli @@ -25,16 +25,18 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +(** This is a specialized module for {!Bs_SortArray}, the docs in that module also + applies here, except the comparator is fixed and inlined +*) -# 31 +# 34 type element = string -# 36 +# 39 val strictlySortedLength: element array -> int (** - [strictlySortedLenght xs] - return [+n] means increasing order - [-n] means negative order + The same as {!Bs_SortArray.strictlySortedLength } except the comparator is fixed + @return [+n] means increasing order [-n] means negative order *) @@ -42,8 +44,12 @@ val isSorted: element array -> bool (** [sorted xs] return true if [xs] is in non strict increasing order *) val stableSortInPlace: element array -> unit +(** + The same as {!Bs_SortArray.stableSortInPlaceBy} except the comparator is fixed +*) val stableSort: element array -> element array +(** The same as {!Bs_SortArray.stableSortBy} except the comparator is fixed *) val binarySearch: element array -> element -> int (** diff --git a/jscomp/others/sort.cppo.mli b/jscomp/others/sort.cppo.mli index 06005f3b67..4ef544275c 100644 --- a/jscomp/others/sort.cppo.mli +++ b/jscomp/others/sort.cppo.mli @@ -24,6 +24,9 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +(** This is a specialized module for {!Bs_SortArray}, the docs in that module also + applies here, except the comparator is fixed and inlined +*) #ifdef TYPE_INT type element = int @@ -35,9 +38,8 @@ type element = string val strictlySortedLength: element array -> int (** - [strictlySortedLenght xs] - return [+n] means increasing order - [-n] means negative order + The same as {!Bs_SortArray.strictlySortedLength } except the comparator is fixed + @return [+n] means increasing order [-n] means negative order *) @@ -45,8 +47,12 @@ val isSorted: element array -> bool (** [sorted xs] return true if [xs] is in non strict increasing order *) val stableSortInPlace: element array -> unit +(** + The same as {!Bs_SortArray.stableSortInPlaceBy} except the comparator is fixed +*) val stableSort: element array -> element array +(** The same as {!Bs_SortArray.stableSortBy} except the comparator is fixed *) val binarySearch: element array -> element -> int (** diff --git a/jscomp/test/bs_array_test.js b/jscomp/test/bs_array_test.js index 1a8e675c3b..4ca045cc94 100644 --- a/jscomp/test/bs_array_test.js +++ b/jscomp/test/bs_array_test.js @@ -183,13 +183,33 @@ neq("File \"bs_array_test.ml\", line 63, characters 6-13", u, v); eq("File \"bs_array_test.ml\", line 65, characters 5-12", Bs_Array.reduce(u, 0, add), Bs_Array.reduce(v, 0, add)); +eq("File \"bs_array_test.ml\", line 68, characters 5-12", Bs_Array.reduceReverse(/* int array */[], 100, (function (prim, prim$1) { + return prim - prim$1 | 0; + })), 100); + +eq("File \"bs_array_test.ml\", line 69, characters 5-12", Bs_Array.reduceReverse(/* int array */[ + 1, + 2 + ], 100, (function (prim, prim$1) { + return prim - prim$1 | 0; + })), 97); + +eq("File \"bs_array_test.ml\", line 70, characters 5-12", Bs_Array.reduceReverse(/* int array */[ + 1, + 2, + 3, + 4 + ], 100, (function (prim, prim$1) { + return prim - prim$1 | 0; + })), 90); + function addone(x) { return x + 1 | 0; } function makeMatrixExn(sx, sy, init) { if (!(sx >= 0 && sy >= 0)) { - throw new Error("File \"bs_array_test.ml\", line 70, characters 4-10"); + throw new Error("File \"bs_array_test.ml\", line 75, characters 4-10"); } var res = new Array(sx); for(var x = 0 ,x_finish = sx - 1 | 0; x <= x_finish; ++x){ @@ -202,11 +222,11 @@ function makeMatrixExn(sx, sy, init) { return res; } -eq("File \"bs_array_test.ml\", line 82, characters 5-12", Bs_Array.makeBy(0, (function () { +eq("File \"bs_array_test.ml\", line 87, characters 5-12", Bs_Array.makeBy(0, (function () { return 1; })), /* int array */[]); -eq("File \"bs_array_test.ml\", line 83, characters 5-12", Bs_Array.makeBy(3, (function (i) { +eq("File \"bs_array_test.ml\", line 88, characters 5-12", Bs_Array.makeBy(3, (function (i) { return i; })), /* int array */[ 0, @@ -214,7 +234,7 @@ eq("File \"bs_array_test.ml\", line 83, characters 5-12", Bs_Array.makeBy(3, (fu 2 ]); -eq("File \"bs_array_test.ml\", line 84, characters 5-12", makeMatrixExn(3, 4, 1), /* array */[ +eq("File \"bs_array_test.ml\", line 89, characters 5-12", makeMatrixExn(3, 4, 1), /* array */[ /* int array */[ 1, 1, @@ -235,25 +255,25 @@ eq("File \"bs_array_test.ml\", line 84, characters 5-12", makeMatrixExn(3, 4, 1) ] ]); -eq("File \"bs_array_test.ml\", line 87, characters 5-12", makeMatrixExn(3, 0, 0), /* array */[ +eq("File \"bs_array_test.ml\", line 92, characters 5-12", makeMatrixExn(3, 0, 0), /* array */[ /* int array */[], /* int array */[], /* int array */[] ]); -eq("File \"bs_array_test.ml\", line 88, characters 5-12", makeMatrixExn(0, 3, 1), /* array */[]); +eq("File \"bs_array_test.ml\", line 93, characters 5-12", makeMatrixExn(0, 3, 1), /* array */[]); -eq("File \"bs_array_test.ml\", line 89, characters 5-12", makeMatrixExn(1, 1, 1), /* array */[/* int array */[1]]); +eq("File \"bs_array_test.ml\", line 94, characters 5-12", makeMatrixExn(1, 1, 1), /* array */[/* int array */[1]]); -eq("File \"bs_array_test.ml\", line 90, characters 5-12", Bs_Array.copy(/* array */[]), /* array */[]); +eq("File \"bs_array_test.ml\", line 95, characters 5-12", Bs_Array.copy(/* array */[]), /* array */[]); -eq("File \"bs_array_test.ml\", line 91, characters 5-12", Bs_Array.map(/* int array */[], (function (prim) { +eq("File \"bs_array_test.ml\", line 96, characters 5-12", Bs_Array.map(/* int array */[], (function (prim) { return prim + 1 | 0; })), /* int array */[]); -eq("File \"bs_array_test.ml\", line 92, characters 5-12", Bs_Array.mapWithIndex(/* int array */[], add), /* int array */[]); +eq("File \"bs_array_test.ml\", line 97, characters 5-12", Bs_Array.mapWithIndex(/* int array */[], add), /* int array */[]); -eq("File \"bs_array_test.ml\", line 93, characters 5-12", Bs_Array.mapWithIndex(/* int array */[ +eq("File \"bs_array_test.ml\", line 98, characters 5-12", Bs_Array.mapWithIndex(/* int array */[ 1, 2, 3 @@ -263,14 +283,14 @@ eq("File \"bs_array_test.ml\", line 93, characters 5-12", Bs_Array.mapWithIndex( 5 ]); -eq("File \"bs_array_test.ml\", line 94, characters 5-12", Bs_List.ofArray(/* array */[]), /* [] */0); +eq("File \"bs_array_test.ml\", line 99, characters 5-12", Bs_List.ofArray(/* array */[]), /* [] */0); -eq("File \"bs_array_test.ml\", line 95, characters 5-12", Bs_List.ofArray(/* int array */[1]), /* :: */[ +eq("File \"bs_array_test.ml\", line 100, characters 5-12", Bs_List.ofArray(/* int array */[1]), /* :: */[ 1, /* [] */0 ]); -eq("File \"bs_array_test.ml\", line 96, characters 5-12", Bs_List.ofArray(/* int array */[ +eq("File \"bs_array_test.ml\", line 101, characters 5-12", Bs_List.ofArray(/* int array */[ 1, 2, 3 @@ -285,7 +305,7 @@ eq("File \"bs_array_test.ml\", line 96, characters 5-12", Bs_List.ofArray(/* int ] ]); -eq("File \"bs_array_test.ml\", line 97, characters 5-12", Bs_Array.map(/* int array */[ +eq("File \"bs_array_test.ml\", line 102, characters 5-12", Bs_Array.map(/* int array */[ 1, 2, 3 @@ -297,14 +317,14 @@ eq("File \"bs_array_test.ml\", line 97, characters 5-12", Bs_Array.map(/* int ar 4 ]); -eq("File \"bs_array_test.ml\", line 98, characters 5-12", Bs_List.toArray(/* [] */0), /* array */[]); +eq("File \"bs_array_test.ml\", line 103, characters 5-12", Bs_List.toArray(/* [] */0), /* array */[]); -eq("File \"bs_array_test.ml\", line 99, characters 5-12", Bs_List.toArray(/* :: */[ +eq("File \"bs_array_test.ml\", line 104, characters 5-12", Bs_List.toArray(/* :: */[ 1, /* [] */0 ]), /* int array */[1]); -eq("File \"bs_array_test.ml\", line 100, characters 5-12", Bs_List.toArray(/* :: */[ +eq("File \"bs_array_test.ml\", line 105, characters 5-12", Bs_List.toArray(/* :: */[ 1, /* :: */[ 2, @@ -315,7 +335,7 @@ eq("File \"bs_array_test.ml\", line 100, characters 5-12", Bs_List.toArray(/* :: 2 ]); -eq("File \"bs_array_test.ml\", line 101, characters 5-12", Bs_List.toArray(/* :: */[ +eq("File \"bs_array_test.ml\", line 106, characters 5-12", Bs_List.toArray(/* :: */[ 1, /* :: */[ 2, @@ -350,7 +370,7 @@ var v2 = Bs_Array.keepMap(v$1, (function (x) { } })); -eq("File \"bs_array_test.ml\", line 108, characters 5-12", v0, /* array */[ +eq("File \"bs_array_test.ml\", line 113, characters 5-12", v0, /* array */[ 0, 2, 4, @@ -358,14 +378,14 @@ eq("File \"bs_array_test.ml\", line 108, characters 5-12", v0, /* array */[ 8 ]); -eq("File \"bs_array_test.ml\", line 109, characters 5-12", v1, /* int array */[ +eq("File \"bs_array_test.ml\", line 114, characters 5-12", v1, /* int array */[ 0, 3, 6, 9 ]); -eq("File \"bs_array_test.ml\", line 110, characters 5-12", v2, /* array */[ +eq("File \"bs_array_test.ml\", line 115, characters 5-12", v2, /* array */[ 1, 3, 5, @@ -381,12 +401,12 @@ var a$2 = /* array */[ 5 ]; -eq("File \"bs_array_test.ml\", line 114, characters 5-12", Bs_Array.slice(a$2, 0, 2), /* int array */[ +eq("File \"bs_array_test.ml\", line 119, characters 5-12", Bs_Array.slice(a$2, 0, 2), /* int array */[ 1, 2 ]); -eq("File \"bs_array_test.ml\", line 115, characters 5-12", Bs_Array.slice(a$2, 0, 5), /* array */[ +eq("File \"bs_array_test.ml\", line 120, characters 5-12", Bs_Array.slice(a$2, 0, 5), /* array */[ 1, 2, 3, @@ -394,7 +414,7 @@ eq("File \"bs_array_test.ml\", line 115, characters 5-12", Bs_Array.slice(a$2, 0 5 ]); -eq("File \"bs_array_test.ml\", line 116, characters 5-12", Bs_Array.slice(a$2, 0, 15), /* array */[ +eq("File \"bs_array_test.ml\", line 121, characters 5-12", Bs_Array.slice(a$2, 0, 15), /* array */[ 1, 2, 3, @@ -402,40 +422,40 @@ eq("File \"bs_array_test.ml\", line 116, characters 5-12", Bs_Array.slice(a$2, 0 5 ]); -eq("File \"bs_array_test.ml\", line 117, characters 5-12", Bs_Array.slice(a$2, 5, 1), /* int array */[]); +eq("File \"bs_array_test.ml\", line 122, characters 5-12", Bs_Array.slice(a$2, 5, 1), /* int array */[]); -eq("File \"bs_array_test.ml\", line 118, characters 5-12", Bs_Array.slice(a$2, 4, 1), /* int array */[5]); +eq("File \"bs_array_test.ml\", line 123, characters 5-12", Bs_Array.slice(a$2, 4, 1), /* int array */[5]); -eq("File \"bs_array_test.ml\", line 119, characters 5-12", Bs_Array.slice(a$2, -1, 1), /* int array */[5]); +eq("File \"bs_array_test.ml\", line 124, characters 5-12", Bs_Array.slice(a$2, -1, 1), /* int array */[5]); -eq("File \"bs_array_test.ml\", line 120, characters 5-12", Bs_Array.slice(a$2, -1, 2), /* int array */[5]); +eq("File \"bs_array_test.ml\", line 125, characters 5-12", Bs_Array.slice(a$2, -1, 2), /* int array */[5]); -eq("File \"bs_array_test.ml\", line 121, characters 5-12", Bs_Array.slice(a$2, -2, 1), /* int array */[4]); +eq("File \"bs_array_test.ml\", line 126, characters 5-12", Bs_Array.slice(a$2, -2, 1), /* int array */[4]); -eq("File \"bs_array_test.ml\", line 122, characters 5-12", Bs_Array.slice(a$2, -2, 2), /* int array */[ +eq("File \"bs_array_test.ml\", line 127, characters 5-12", Bs_Array.slice(a$2, -2, 2), /* int array */[ 4, 5 ]); -eq("File \"bs_array_test.ml\", line 123, characters 5-12", Bs_Array.slice(a$2, -2, 3), /* int array */[ +eq("File \"bs_array_test.ml\", line 128, characters 5-12", Bs_Array.slice(a$2, -2, 3), /* int array */[ 4, 5 ]); -eq("File \"bs_array_test.ml\", line 124, characters 5-12", Bs_Array.slice(a$2, -10, 3), /* int array */[ +eq("File \"bs_array_test.ml\", line 129, characters 5-12", Bs_Array.slice(a$2, -10, 3), /* int array */[ 1, 2, 3 ]); -eq("File \"bs_array_test.ml\", line 125, characters 5-12", Bs_Array.slice(a$2, -10, 4), /* int array */[ +eq("File \"bs_array_test.ml\", line 130, characters 5-12", Bs_Array.slice(a$2, -10, 4), /* int array */[ 1, 2, 3, 4 ]); -eq("File \"bs_array_test.ml\", line 126, characters 5-12", Bs_Array.slice(a$2, -10, 5), /* array */[ +eq("File \"bs_array_test.ml\", line 131, characters 5-12", Bs_Array.slice(a$2, -10, 5), /* array */[ 1, 2, 3, @@ -443,7 +463,7 @@ eq("File \"bs_array_test.ml\", line 126, characters 5-12", Bs_Array.slice(a$2, - 5 ]); -eq("File \"bs_array_test.ml\", line 127, characters 5-12", Bs_Array.slice(a$2, -10, 6), /* array */[ +eq("File \"bs_array_test.ml\", line 132, characters 5-12", Bs_Array.slice(a$2, -10, 6), /* array */[ 1, 2, 3, @@ -451,9 +471,9 @@ eq("File \"bs_array_test.ml\", line 127, characters 5-12", Bs_Array.slice(a$2, - 5 ]); -eq("File \"bs_array_test.ml\", line 128, characters 5-12", Bs_Array.slice(a$2, 0, 0), /* int array */[]); +eq("File \"bs_array_test.ml\", line 133, characters 5-12", Bs_Array.slice(a$2, 0, 0), /* int array */[]); -eq("File \"bs_array_test.ml\", line 129, characters 5-12", Bs_Array.slice(a$2, 0, -1), /* int array */[]); +eq("File \"bs_array_test.ml\", line 134, characters 5-12", Bs_Array.slice(a$2, 0, -1), /* int array */[]); var a$3 = Bs_Array.makeBy(10, (function (x) { return x; @@ -461,7 +481,7 @@ var a$3 = Bs_Array.makeBy(10, (function (x) { Bs_Array.fill(a$3, 0, 3, 0); -eq("File \"bs_array_test.ml\", line 134, characters 6-13", Bs_Array.copy(a$3), /* array */[ +eq("File \"bs_array_test.ml\", line 139, characters 6-13", Bs_Array.copy(a$3), /* array */[ 0, 0, 0, @@ -476,7 +496,7 @@ eq("File \"bs_array_test.ml\", line 134, characters 6-13", Bs_Array.copy(a$3), / Bs_Array.fill(a$3, 2, 8, 1); -eq("File \"bs_array_test.ml\", line 136, characters 5-12", Bs_Array.copy(a$3), /* array */[ +eq("File \"bs_array_test.ml\", line 141, characters 5-12", Bs_Array.copy(a$3), /* array */[ 0, 0, 1, @@ -491,7 +511,7 @@ eq("File \"bs_array_test.ml\", line 136, characters 5-12", Bs_Array.copy(a$3), / Bs_Array.fill(a$3, 8, 1, 9); -eq("File \"bs_array_test.ml\", line 138, characters 5-12", Bs_Array.copy(a$3), /* array */[ +eq("File \"bs_array_test.ml\", line 143, characters 5-12", Bs_Array.copy(a$3), /* array */[ 0, 0, 1, @@ -506,7 +526,7 @@ eq("File \"bs_array_test.ml\", line 138, characters 5-12", Bs_Array.copy(a$3), / Bs_Array.fill(a$3, 8, 2, 9); -eq("File \"bs_array_test.ml\", line 140, characters 5-12", Bs_Array.copy(a$3), /* array */[ +eq("File \"bs_array_test.ml\", line 145, characters 5-12", Bs_Array.copy(a$3), /* array */[ 0, 0, 1, @@ -521,7 +541,7 @@ eq("File \"bs_array_test.ml\", line 140, characters 5-12", Bs_Array.copy(a$3), / Bs_Array.fill(a$3, 8, 3, 12); -eq("File \"bs_array_test.ml\", line 142, characters 5-12", Bs_Array.copy(a$3), /* array */[ +eq("File \"bs_array_test.ml\", line 147, characters 5-12", Bs_Array.copy(a$3), /* array */[ 0, 0, 1, @@ -536,7 +556,7 @@ eq("File \"bs_array_test.ml\", line 142, characters 5-12", Bs_Array.copy(a$3), / Bs_Array.fill(a$3, -2, 3, 11); -eq("File \"bs_array_test.ml\", line 144, characters 5-12", Bs_Array.copy(a$3), /* array */[ +eq("File \"bs_array_test.ml\", line 149, characters 5-12", Bs_Array.copy(a$3), /* array */[ 0, 0, 1, @@ -551,7 +571,7 @@ eq("File \"bs_array_test.ml\", line 144, characters 5-12", Bs_Array.copy(a$3), / Bs_Array.fill(a$3, -3, 3, 10); -eq("File \"bs_array_test.ml\", line 146, characters 5-12", Bs_Array.copy(a$3), /* array */[ +eq("File \"bs_array_test.ml\", line 151, characters 5-12", Bs_Array.copy(a$3), /* array */[ 0, 0, 1, @@ -566,7 +586,7 @@ eq("File \"bs_array_test.ml\", line 146, characters 5-12", Bs_Array.copy(a$3), / Bs_Array.fill(a$3, -3, 1, 7); -eq("File \"bs_array_test.ml\", line 148, characters 5-12", Bs_Array.copy(a$3), /* array */[ +eq("File \"bs_array_test.ml\", line 153, characters 5-12", Bs_Array.copy(a$3), /* array */[ 0, 0, 1, @@ -581,7 +601,7 @@ eq("File \"bs_array_test.ml\", line 148, characters 5-12", Bs_Array.copy(a$3), / Bs_Array.fill(a$3, -13, 1, 7); -eq("File \"bs_array_test.ml\", line 150, characters 5-12", Bs_Array.copy(a$3), /* array */[ +eq("File \"bs_array_test.ml\", line 155, characters 5-12", Bs_Array.copy(a$3), /* array */[ 7, 0, 1, @@ -596,11 +616,11 @@ eq("File \"bs_array_test.ml\", line 150, characters 5-12", Bs_Array.copy(a$3), / Bs_Array.fill(a$3, -13, 12, 7); -eq("File \"bs_array_test.ml\", line 152, characters 5-12", Bs_Array.copy(a$3), Bs_Array.make(10, 7)); +eq("File \"bs_array_test.ml\", line 157, characters 5-12", Bs_Array.copy(a$3), Bs_Array.make(10, 7)); Bs_Array.fill(a$3, 0, -1, 2); -eq("File \"bs_array_test.ml\", line 154, characters 5-12", Bs_Array.copy(a$3), Bs_Array.make(10, 7)); +eq("File \"bs_array_test.ml\", line 159, characters 5-12", Bs_Array.copy(a$3), Bs_Array.make(10, 7)); var a0 = Bs_Array.makeBy(10, (function (x) { return x; @@ -610,7 +630,7 @@ var b0 = Bs_Array.make(10, 3); Bs_Array.blit(a0, 1, b0, 2, 5); -eq("File \"bs_array_test.ml\", line 160, characters 5-12", Bs_Array.copy(b0), /* array */[ +eq("File \"bs_array_test.ml\", line 165, characters 5-12", Bs_Array.copy(b0), /* array */[ 3, 3, 1, @@ -625,7 +645,7 @@ eq("File \"bs_array_test.ml\", line 160, characters 5-12", Bs_Array.copy(b0), /* Bs_Array.blit(a0, -1, b0, 2, 5); -eq("File \"bs_array_test.ml\", line 163, characters 5-12", Bs_Array.copy(b0), /* array */[ +eq("File \"bs_array_test.ml\", line 168, characters 5-12", Bs_Array.copy(b0), /* array */[ 3, 3, 9, @@ -640,7 +660,7 @@ eq("File \"bs_array_test.ml\", line 163, characters 5-12", Bs_Array.copy(b0), /* Bs_Array.blit(a0, -1, b0, -2, 5); -eq("File \"bs_array_test.ml\", line 166, characters 5-12", Bs_Array.copy(b0), /* array */[ +eq("File \"bs_array_test.ml\", line 171, characters 5-12", Bs_Array.copy(b0), /* array */[ 3, 3, 9, @@ -655,7 +675,7 @@ eq("File \"bs_array_test.ml\", line 166, characters 5-12", Bs_Array.copy(b0), /* Bs_Array.blit(a0, -2, b0, -2, 2); -eq("File \"bs_array_test.ml\", line 169, characters 5-12", Bs_Array.copy(b0), /* array */[ +eq("File \"bs_array_test.ml\", line 174, characters 5-12", Bs_Array.copy(b0), /* array */[ 3, 3, 9, @@ -670,11 +690,11 @@ eq("File \"bs_array_test.ml\", line 169, characters 5-12", Bs_Array.copy(b0), /* Bs_Array.blit(a0, -11, b0, -11, 100); -eq("File \"bs_array_test.ml\", line 172, characters 5-12", Bs_Array.copy(b0), a0); +eq("File \"bs_array_test.ml\", line 177, characters 5-12", Bs_Array.copy(b0), a0); Bs_Array.blit(a0, -11, b0, -11, 2); -eq("File \"bs_array_test.ml\", line 174, characters 5-12", Bs_Array.copy(b0), a0); +eq("File \"bs_array_test.ml\", line 179, characters 5-12", Bs_Array.copy(b0), a0); var aa = Bs_Array.makeBy(10, (function (x) { return x; @@ -682,7 +702,7 @@ var aa = Bs_Array.makeBy(10, (function (x) { Bs_Array.blit(aa, -1, aa, 1, 2); -eq("File \"bs_array_test.ml\", line 177, characters 5-12", Bs_Array.copy(aa), /* array */[ +eq("File \"bs_array_test.ml\", line 182, characters 5-12", Bs_Array.copy(aa), /* array */[ 0, 9, 2, @@ -697,7 +717,7 @@ eq("File \"bs_array_test.ml\", line 177, characters 5-12", Bs_Array.copy(aa), /* Bs_Array.blit(aa, -2, aa, 1, 2); -eq("File \"bs_array_test.ml\", line 179, characters 5-12", Bs_Array.copy(aa), /* array */[ +eq("File \"bs_array_test.ml\", line 184, characters 5-12", Bs_Array.copy(aa), /* array */[ 0, 8, 9, @@ -712,7 +732,7 @@ eq("File \"bs_array_test.ml\", line 179, characters 5-12", Bs_Array.copy(aa), /* Bs_Array.blit(aa, -5, aa, 4, 3); -eq("File \"bs_array_test.ml\", line 181, characters 5-12", Bs_Array.copy(aa), /* array */[ +eq("File \"bs_array_test.ml\", line 186, characters 5-12", Bs_Array.copy(aa), /* array */[ 0, 8, 9, @@ -727,7 +747,7 @@ eq("File \"bs_array_test.ml\", line 181, characters 5-12", Bs_Array.copy(aa), /* Bs_Array.blit(aa, 4, aa, 5, 3); -eq("File \"bs_array_test.ml\", line 183, characters 5-12", Bs_Array.copy(aa), /* array */[ +eq("File \"bs_array_test.ml\", line 188, characters 5-12", Bs_Array.copy(aa), /* array */[ 0, 8, 9, @@ -742,32 +762,32 @@ eq("File \"bs_array_test.ml\", line 183, characters 5-12", Bs_Array.copy(aa), /* function id$1(_, x) { var u = Bs_Array.copy(x); - return eq("File \"bs_array_test.ml\", line 186, characters 5-12", Bs_Array.reverse(x), (Bs_Array.reverseInPlace(u), u)); + return eq("File \"bs_array_test.ml\", line 191, characters 5-12", Bs_Array.reverse(x), (Bs_Array.reverseInPlace(u), u)); } -id$1("File \"bs_array_test.ml\", line 191, characters 5-12", /* array */[]); +id$1("File \"bs_array_test.ml\", line 196, characters 5-12", /* array */[]); -id$1("File \"bs_array_test.ml\", line 192, characters 5-12", /* int array */[1]); +id$1("File \"bs_array_test.ml\", line 197, characters 5-12", /* int array */[1]); -id$1("File \"bs_array_test.ml\", line 193, characters 5-12", /* int array */[ +id$1("File \"bs_array_test.ml\", line 198, characters 5-12", /* int array */[ 1, 2 ]); -id$1("File \"bs_array_test.ml\", line 194, characters 5-12", /* int array */[ +id$1("File \"bs_array_test.ml\", line 199, characters 5-12", /* int array */[ 1, 2, 3 ]); -id$1("File \"bs_array_test.ml\", line 195, characters 5-12", /* int array */[ +id$1("File \"bs_array_test.ml\", line 200, characters 5-12", /* int array */[ 1, 2, 3, 4 ]); -eq("File \"bs_array_test.ml\", line 199, characters 5-12", Bs_Array.concat(/* int array */[], /* int array */[ +eq("File \"bs_array_test.ml\", line 204, characters 5-12", Bs_Array.concat(/* int array */[], /* int array */[ 1, 2, 3 @@ -777,9 +797,9 @@ eq("File \"bs_array_test.ml\", line 199, characters 5-12", Bs_Array.concat(/* in 3 ]); -eq("File \"bs_array_test.ml\", line 200, characters 5-12", Bs_Array.concat(/* array */[], /* array */[]), /* array */[]); +eq("File \"bs_array_test.ml\", line 205, characters 5-12", Bs_Array.concat(/* array */[], /* array */[]), /* array */[]); -eq("File \"bs_array_test.ml\", line 201, characters 5-12", Bs_Array.concat(/* int array */[ +eq("File \"bs_array_test.ml\", line 206, characters 5-12", Bs_Array.concat(/* int array */[ 3, 2 ], /* int array */[ @@ -794,7 +814,7 @@ eq("File \"bs_array_test.ml\", line 201, characters 5-12", Bs_Array.concat(/* in 3 ]); -eq("File \"bs_array_test.ml\", line 202, characters 5-12", Bs_Array.concatMany(/* array */[ +eq("File \"bs_array_test.ml\", line 207, characters 5-12", Bs_Array.concatMany(/* array */[ /* int array */[ 3, 2 @@ -812,7 +832,7 @@ eq("File \"bs_array_test.ml\", line 202, characters 5-12", Bs_Array.concatMany(/ 3 ]); -eq("File \"bs_array_test.ml\", line 203, characters 5-12", Bs_Array.concatMany(/* array */[ +eq("File \"bs_array_test.ml\", line 208, characters 5-12", Bs_Array.concatMany(/* array */[ /* int array */[ 3, 2 @@ -833,7 +853,7 @@ eq("File \"bs_array_test.ml\", line 203, characters 5-12", Bs_Array.concatMany(/ 0 ]); -eq("File \"bs_array_test.ml\", line 204, characters 5-12", Bs_Array.concatMany(/* array */[ +eq("File \"bs_array_test.ml\", line 209, characters 5-12", Bs_Array.concatMany(/* array */[ /* int array */[], /* int array */[ 3, @@ -855,12 +875,12 @@ eq("File \"bs_array_test.ml\", line 204, characters 5-12", Bs_Array.concatMany(/ 0 ]); -eq("File \"bs_array_test.ml\", line 205, characters 5-12", Bs_Array.concatMany(/* array */[ +eq("File \"bs_array_test.ml\", line 210, characters 5-12", Bs_Array.concatMany(/* array */[ /* array */[], /* array */[] ]), /* array */[]); -Mt.from_pair_suites("File \"bs_array_test.ml\", line 207, characters 23-30", suites[0]); +Mt.from_pair_suites("File \"bs_array_test.ml\", line 212, characters 23-30", suites[0]); var A = 0; diff --git a/jscomp/test/bs_array_test.ml b/jscomp/test/bs_array_test.ml index 10fbcfa95f..a08bc682de 100644 --- a/jscomp/test/bs_array_test.ml +++ b/jscomp/test/bs_array_test.ml @@ -63,6 +63,11 @@ let () = neq __LOC__ u v (* unlikely*); let sum x = A.reduce x 0 add in eq __LOC__ ( sum u) (sum v) + +let () = + eq __LOC__ (A.reduceReverse [||] 100 (-)) 100; + eq __LOC__ (A.reduceReverse [|1;2|] 100 (-)) 97; + eq __LOC__ (A.reduceReverse [|1;2;3;4|] 100 (-) ) 90 let addone = fun [@bs] x -> x + 1 let makeMatrixExn sx sy init = diff --git a/jscomp/test/bs_list_test.js b/jscomp/test/bs_list_test.js index 411508837a..184ca7382c 100644 --- a/jscomp/test/bs_list_test.js +++ b/jscomp/test/bs_list_test.js @@ -892,10 +892,72 @@ function succx(x) { return x + 1 | 0; } -function eq$1(x, y) { +function eqx(x, y) { return +(x === y); } +b("File \"bs_list_test.ml\", line 176, characters 4-11", Bs_List.hasAssoc(/* :: */[ + /* tuple */[ + 1, + "1" + ], + /* :: */[ + /* tuple */[ + 2, + "2" + ], + /* :: */[ + /* tuple */[ + 3, + "3" + ], + /* [] */0 + ] + ] + ], 2, Caml_obj.caml_equal)); + +b("File \"bs_list_test.ml\", line 177, characters 4-11", 1 - Bs_List.hasAssoc(/* :: */[ + /* tuple */[ + 1, + "1" + ], + /* :: */[ + /* tuple */[ + 2, + "2" + ], + /* :: */[ + /* tuple */[ + 3, + "3" + ], + /* [] */0 + ] + ] + ], 4, Caml_obj.caml_equal)); + +b("File \"bs_list_test.ml\", line 178, characters 4-11", Bs_List.hasAssoc(/* :: */[ + /* tuple */[ + 1, + "1" + ], + /* :: */[ + /* tuple */[ + 2, + "2" + ], + /* :: */[ + /* tuple */[ + 3, + "3" + ], + /* [] */0 + ] + ] + ], 4, (function (x, y) { + return +((x + 1 | 0) === y); + }))); + eq("REMOVEASSOQ", Bs_List.removeAssoc(/* :: */[ /* tuple */[ 1, @@ -1048,7 +1110,7 @@ eq("REMOVEASSOQ", Bs_List.removeAssoc(/* :: */[ /* [] */0 ] ] - ], 3, eq$1), /* :: */[ + ], 3, eqx), /* :: */[ /* tuple */[ 1, "1" @@ -1080,7 +1142,7 @@ eq("REMOVEASSOQ", Bs_List.removeAssoc(/* :: */[ /* [] */0 ] ] - ], 1, eq$1), /* :: */[ + ], 1, eqx), /* :: */[ /* tuple */[ 2, "2" @@ -1112,7 +1174,7 @@ eq("REMOVEASSOQ", Bs_List.removeAssoc(/* :: */[ /* [] */0 ] ] - ], 2, eq$1), /* :: */[ + ], 2, eqx), /* :: */[ /* tuple */[ 1, "1" @@ -1126,25 +1188,33 @@ eq("REMOVEASSOQ", Bs_List.removeAssoc(/* :: */[ ] ]); -eq("REMOVEASSOQ", Bs_List.removeAssoc(/* :: */[ - /* tuple */[ - 1, - "1" - ], - /* :: */[ - /* tuple */[ - 2, - "2" - ], - /* :: */[ - /* tuple */[ - 3, - "3" - ], - /* [] */0 - ] - ] - ], 0, eq$1), /* :: */[ +var ll = /* :: */[ + /* tuple */[ + 1, + "1" + ], + /* :: */[ + /* tuple */[ + 2, + "2" + ], + /* :: */[ + /* tuple */[ + 3, + "3" + ], + /* [] */0 + ] + ] +]; + +var ll0 = Bs_List.removeAssoc(ll, 0, eqx); + +b("File \"bs_list_test.ml\", line 189, characters 5-12", +(ll === ll0)); + +var ll1 = Bs_List.setAssoc(ll, 2, "22", Caml_obj.caml_equal); + +eq("File \"bs_list_test.ml\", line 191, characters 5-12", ll1, /* :: */[ /* tuple */[ 1, "1" @@ -1152,7 +1222,7 @@ eq("REMOVEASSOQ", Bs_List.removeAssoc(/* :: */[ /* :: */[ /* tuple */[ 2, - "2" + "22" ], /* :: */[ /* tuple */[ @@ -1164,7 +1234,89 @@ eq("REMOVEASSOQ", Bs_List.removeAssoc(/* :: */[ ] ]); -eq("File \"bs_list_test.ml\", line 188, characters 5-12", /* tuple */[ +var ll2 = Bs_List.setAssoc(ll1, 22, "2", Caml_obj.caml_equal); + +b("File \"bs_list_test.ml\", line 193, characters 4-11", Caml_obj.caml_equal(ll2, /* :: */[ + /* tuple */[ + 22, + "2" + ], + ll1 + ])); + +b("File \"bs_list_test.ml\", line 194, characters 4-11", +(Bs_List.tailExn(ll2) === ll1)); + +b("File \"bs_list_test.ml\", line 195, characters 4-11", Caml_obj.caml_equal(Bs_List.setAssoc(/* :: */[ + /* tuple */[ + 1, + "a" + ], + /* :: */[ + /* tuple */[ + 2, + "b" + ], + /* :: */[ + /* tuple */[ + 3, + "c" + ], + /* [] */0 + ] + ] + ], 2, "x", Caml_obj.caml_equal), /* :: */[ + /* tuple */[ + 1, + "a" + ], + /* :: */[ + /* tuple */[ + 2, + "x" + ], + /* :: */[ + /* tuple */[ + 3, + "c" + ], + /* [] */0 + ] + ] + ])); + +b("File \"bs_list_test.ml\", line 197, characters 4-11", Caml_obj.caml_equal(Bs_List.setAssoc(/* :: */[ + /* tuple */[ + 1, + "a" + ], + /* :: */[ + /* tuple */[ + 3, + "c" + ], + /* [] */0 + ] + ], 2, "2", Caml_obj.caml_equal), /* :: */[ + /* tuple */[ + 2, + "2" + ], + /* :: */[ + /* tuple */[ + 1, + "a" + ], + /* :: */[ + /* tuple */[ + 3, + "c" + ], + /* [] */0 + ] + ] + ])); + +eq("File \"bs_list_test.ml\", line 202, characters 5-12", /* tuple */[ Bs_List.head(length_10_id), Bs_List.tail(length_10_id) ], /* tuple */[ @@ -1172,59 +1324,59 @@ eq("File \"bs_list_test.ml\", line 188, characters 5-12", /* tuple */[ Bs_List.drop(length_10_id, 1) ]); -eq("File \"bs_list_test.ml\", line 189, characters 5-12", Bs_List.head(/* [] */0), /* None */0); +eq("File \"bs_list_test.ml\", line 203, characters 5-12", Bs_List.head(/* [] */0), /* None */0); Bs_List.forEachWithIndex(length_10_id, (function (i, x) { - return eq("File \"bs_list_test.ml\", line 191, characters 9-16", Bs_List.get(length_10_id, i), /* Some */[x]); + return eq("File \"bs_list_test.ml\", line 205, characters 9-16", Bs_List.get(length_10_id, i), /* Some */[x]); })); -eq("File \"bs_list_test.ml\", line 192, characters 5-12", Bs_List.tail(/* [] */0), /* None */0); +eq("File \"bs_list_test.ml\", line 206, characters 5-12", Bs_List.tail(/* [] */0), /* None */0); -eq("File \"bs_list_test.ml\", line 193, characters 5-12", Bs_List.drop(/* [] */0, 3), /* None */0); +eq("File \"bs_list_test.ml\", line 207, characters 5-12", Bs_List.drop(/* [] */0, 3), /* None */0); -eq("File \"bs_list_test.ml\", line 194, characters 5-12", Bs_List.mapWithIndex(/* [] */0, (function (i, x) { +eq("File \"bs_list_test.ml\", line 208, characters 5-12", Bs_List.mapWithIndex(/* [] */0, (function (i, x) { return i + x | 0; })), /* [] */0); -eq("File \"bs_list_test.ml\", line 195, characters 5-12", Bs_List.get(length_10_id, -1), /* None */0); +eq("File \"bs_list_test.ml\", line 209, characters 5-12", Bs_List.get(length_10_id, -1), /* None */0); -eq("File \"bs_list_test.ml\", line 196, characters 5-12", Bs_List.get(length_10_id, 12), /* None */0); +eq("File \"bs_list_test.ml\", line 210, characters 5-12", Bs_List.get(length_10_id, 12), /* None */0); -eq("File \"bs_list_test.ml\", line 197, characters 5-12", sum(/* [] */0), 0); +eq("File \"bs_list_test.ml\", line 211, characters 5-12", sum(/* [] */0), 0); -eq("File \"bs_list_test.ml\", line 198, characters 5-12", sum(length_10_id), 45); +eq("File \"bs_list_test.ml\", line 212, characters 5-12", sum(length_10_id), 45); -eq("File \"bs_list_test.ml\", line 199, characters 5-12", Bs_List.makeBy(0, id), /* [] */0); +eq("File \"bs_list_test.ml\", line 213, characters 5-12", Bs_List.makeBy(0, id), /* [] */0); -eq("File \"bs_list_test.ml\", line 200, characters 5-12", Bs_List.reverse(Bs_List.reverse(length_10_id)), length_10_id); +eq("File \"bs_list_test.ml\", line 214, characters 5-12", Bs_List.reverse(Bs_List.reverse(length_10_id)), length_10_id); -eq("File \"bs_list_test.ml\", line 201, characters 5-12", Bs_List.reverse(Bs_List.reverse(length_8_id)), length_8_id); +eq("File \"bs_list_test.ml\", line 215, characters 5-12", Bs_List.reverse(Bs_List.reverse(length_8_id)), length_8_id); -eq("File \"bs_list_test.ml\", line 202, characters 5-12", Bs_List.reverse(/* [] */0), /* [] */0); +eq("File \"bs_list_test.ml\", line 216, characters 5-12", Bs_List.reverse(/* [] */0), /* [] */0); -eq("File \"bs_list_test.ml\", line 203, characters 5-12", Bs_List.reverse(Bs_List.mapReverse(length_10_id, succx)), Bs_List.map(length_10_id, succx)); +eq("File \"bs_list_test.ml\", line 217, characters 5-12", Bs_List.reverse(Bs_List.mapReverse(length_10_id, succx)), Bs_List.map(length_10_id, succx)); -eq("File \"bs_list_test.ml\", line 206, characters 5-12", Bs_List.reduce(length_10_id, 0, add), 45); +eq("File \"bs_list_test.ml\", line 220, characters 5-12", Bs_List.reduce(length_10_id, 0, add), 45); -eq("File \"bs_list_test.ml\", line 208, characters 5-12", Bs_List.reduceReverse(length_10_id, 0, add), 45); +eq("File \"bs_list_test.ml\", line 222, characters 5-12", Bs_List.reduceReverse(length_10_id, 0, add), 45); -eq("File \"bs_list_test.ml\", line 212, characters 5-12", sum2(length_10_id, length_10_id), 90); +eq("File \"bs_list_test.ml\", line 226, characters 5-12", sum2(length_10_id, length_10_id), 90); -eq("File \"bs_list_test.ml\", line 213, characters 5-12", sum2(length_8_id, length_10_id), 56); +eq("File \"bs_list_test.ml\", line 227, characters 5-12", sum2(length_8_id, length_10_id), 56); -eq("File \"bs_list_test.ml\", line 214, characters 5-12", Bs_List.reduce2(length_10_id, length_8_id, 0, (function (acc, x, y) { +eq("File \"bs_list_test.ml\", line 228, characters 5-12", Bs_List.reduce2(length_10_id, length_8_id, 0, (function (acc, x, y) { return (acc + x | 0) + y | 0; })), 56); -eq("File \"bs_list_test.ml\", line 216, characters 5-12", Bs_List.reduceReverse2(length_10_id, length_8_id, 0, (function (acc, x, y) { +eq("File \"bs_list_test.ml\", line 230, characters 5-12", Bs_List.reduceReverse2(length_10_id, length_8_id, 0, (function (acc, x, y) { return (acc + x | 0) + y | 0; })), 56); -eq("File \"bs_list_test.ml\", line 218, characters 5-12", Bs_List.reduceReverse2(length_10_id, length_10_id, 0, (function (acc, x, y) { +eq("File \"bs_list_test.ml\", line 232, characters 5-12", Bs_List.reduceReverse2(length_10_id, length_10_id, 0, (function (acc, x, y) { return (acc + x | 0) + y | 0; })), 90); -eq("File \"bs_list_test.ml\", line 220, characters 5-12", Bs_List.reduceReverse2(/* :: */[ +eq("File \"bs_list_test.ml\", line 234, characters 5-12", Bs_List.reduceReverse2(/* :: */[ 1, /* :: */[ 2, @@ -1243,7 +1395,7 @@ eq("File \"bs_list_test.ml\", line 220, characters 5-12", Bs_List.reduceReverse2 return (acc + x | 0) + y | 0; })), 6); -eq("File \"bs_list_test.ml\", line 221, characters 5-12", Bs_List.every(/* :: */[ +eq("File \"bs_list_test.ml\", line 235, characters 5-12", Bs_List.every(/* :: */[ 2, /* :: */[ 4, @@ -1254,14 +1406,14 @@ eq("File \"bs_list_test.ml\", line 221, characters 5-12", Bs_List.every(/* :: */ ] ], mod2), /* true */1); -eq("File \"bs_list_test.ml\", line 222, characters 5-12", Bs_List.every(/* :: */[ +eq("File \"bs_list_test.ml\", line 236, characters 5-12", Bs_List.every(/* :: */[ 1, /* [] */0 ], mod2), /* false */0); -eq("File \"bs_list_test.ml\", line 223, characters 5-12", Bs_List.every(/* [] */0, mod2), /* true */1); +eq("File \"bs_list_test.ml\", line 237, characters 5-12", Bs_List.every(/* [] */0, mod2), /* true */1); -eq("File \"bs_list_test.ml\", line 224, characters 5-12", Bs_List.some(/* :: */[ +eq("File \"bs_list_test.ml\", line 238, characters 5-12", Bs_List.some(/* :: */[ 1, /* :: */[ 2, @@ -1272,7 +1424,7 @@ eq("File \"bs_list_test.ml\", line 224, characters 5-12", Bs_List.some(/* :: */[ ] ], mod2), /* true */1); -eq("File \"bs_list_test.ml\", line 225, characters 5-12", Bs_List.some(/* :: */[ +eq("File \"bs_list_test.ml\", line 239, characters 5-12", Bs_List.some(/* :: */[ 1, /* :: */[ 3, @@ -1283,16 +1435,16 @@ eq("File \"bs_list_test.ml\", line 225, characters 5-12", Bs_List.some(/* :: */[ ] ], mod2), /* false */0); -eq("File \"bs_list_test.ml\", line 226, characters 5-12", Bs_List.some(/* [] */0, mod2), /* false */0); +eq("File \"bs_list_test.ml\", line 240, characters 5-12", Bs_List.some(/* [] */0, mod2), /* false */0); -eq("File \"bs_list_test.ml\", line 227, characters 5-12", Bs_List.every2(/* [] */0, /* :: */[ +eq("File \"bs_list_test.ml\", line 241, characters 5-12", Bs_List.every2(/* [] */0, /* :: */[ 1, /* [] */0 ], (function (x, y) { return +(x > y); })), /* true */1); -eq("File \"bs_list_test.ml\", line 228, characters 5-12", Bs_List.every2(/* :: */[ +eq("File \"bs_list_test.ml\", line 242, characters 5-12", Bs_List.every2(/* :: */[ 2, /* [] */0 ], /* :: */[ @@ -1302,7 +1454,7 @@ eq("File \"bs_list_test.ml\", line 228, characters 5-12", Bs_List.every2(/* :: * return +(x > y); })), /* true */1); -eq("File \"bs_list_test.ml\", line 229, characters 5-12", Bs_List.every2(/* :: */[ +eq("File \"bs_list_test.ml\", line 243, characters 5-12", Bs_List.every2(/* :: */[ 2, /* :: */[ 3, @@ -1318,14 +1470,14 @@ eq("File \"bs_list_test.ml\", line 229, characters 5-12", Bs_List.every2(/* :: * return +(x > y); })), /* false */0); -eq("File \"bs_list_test.ml\", line 230, characters 5-12", Bs_List.some2(/* [] */0, /* :: */[ +eq("File \"bs_list_test.ml\", line 244, characters 5-12", Bs_List.some2(/* [] */0, /* :: */[ 1, /* [] */0 ], (function (x, y) { return +(x > y); })), /* false */0); -eq("File \"bs_list_test.ml\", line 231, characters 5-12", Bs_List.some2(/* :: */[ +eq("File \"bs_list_test.ml\", line 245, characters 5-12", Bs_List.some2(/* :: */[ 2, /* :: */[ 3, @@ -1341,7 +1493,7 @@ eq("File \"bs_list_test.ml\", line 231, characters 5-12", Bs_List.some2(/* :: */ return +(x > y); })), /* true */1); -eq("File \"bs_list_test.ml\", line 232, characters 5-12", Bs_List.some2(/* :: */[ +eq("File \"bs_list_test.ml\", line 246, characters 5-12", Bs_List.some2(/* :: */[ 0, /* :: */[ 3, @@ -1357,7 +1509,7 @@ eq("File \"bs_list_test.ml\", line 232, characters 5-12", Bs_List.some2(/* :: */ return +(x > y); })), /* false */0); -eq("File \"bs_list_test.ml\", line 233, characters 5-12", Bs_List.has(/* :: */[ +eq("File \"bs_list_test.ml\", line 247, characters 5-12", Bs_List.has(/* :: */[ 1, /* :: */[ 2, @@ -1370,7 +1522,7 @@ eq("File \"bs_list_test.ml\", line 233, characters 5-12", Bs_List.has(/* :: */[ return +("" + x === s); })), /* true */1); -eq("File \"bs_list_test.ml\", line 234, characters 5-12", Bs_List.has(/* :: */[ +eq("File \"bs_list_test.ml\", line 248, characters 5-12", Bs_List.has(/* :: */[ 1, /* :: */[ 2, @@ -1384,7 +1536,7 @@ eq("File \"bs_list_test.ml\", line 234, characters 5-12", Bs_List.has(/* :: */[ })), /* false */0); function makeTest(n) { - return eq("File \"bs_list_test.ml\", line 237, characters 5-12", Bs_List.make(n, 3), Bs_List.makeBy(n, (function () { + return eq("File \"bs_list_test.ml\", line 251, characters 5-12", Bs_List.make(n, 3), Bs_List.makeBy(n, (function () { return 3; }))); } @@ -1397,7 +1549,7 @@ makeTest(2); makeTest(3); -b("File \"bs_list_test.ml\", line 246, characters 4-11", 1 - Bs_List.eq(/* :: */[ +b("File \"bs_list_test.ml\", line 260, characters 4-11", 1 - Bs_List.eq(/* :: */[ 1, /* :: */[ 2, @@ -1416,7 +1568,7 @@ b("File \"bs_list_test.ml\", line 246, characters 4-11", 1 - Bs_List.eq(/* :: */ return +(x === y); }))); -b("File \"bs_list_test.ml\", line 247, characters 4-11", Bs_List.eq(/* :: */[ +b("File \"bs_list_test.ml\", line 261, characters 4-11", Bs_List.eq(/* :: */[ 1, /* :: */[ 2, @@ -1438,7 +1590,7 @@ b("File \"bs_list_test.ml\", line 247, characters 4-11", Bs_List.eq(/* :: */[ return +(x === y); }))); -b("File \"bs_list_test.ml\", line 248, characters 4-11", 1 - Bs_List.eq(/* :: */[ +b("File \"bs_list_test.ml\", line 262, characters 4-11", 1 - Bs_List.eq(/* :: */[ 1, /* :: */[ 2, @@ -1472,7 +1624,7 @@ var u1 = Bs_List.keepMap(u0, (function (x) { } })); -eq("File \"bs_list_test.ml\", line 252, characters 5-12", u1, /* :: */[ +eq("File \"bs_list_test.ml\", line 266, characters 5-12", u1, /* :: */[ 1, /* :: */[ 8, diff --git a/jscomp/test/bs_list_test.ml b/jscomp/test/bs_list_test.ml index 4122126ade..cbf7b79af9 100644 --- a/jscomp/test/bs_list_test.ml +++ b/jscomp/test/bs_list_test.ml @@ -172,16 +172,30 @@ let succx = (fun x -> x + 1) let () = let (=~) = eq "REMOVEASSOQ" in - let eq = fun x y -> (x : int) = y in + let eqx = fun x y -> (x : int) = y in + b __LOC__ (N.hasAssoc [1,"1";2, "2"; 3, "3"] 2 (=)); + b __LOC__ (not (N.hasAssoc [1,"1";2, "2"; 3, "3"] 4 (=))); + b __LOC__ ( (N.hasAssoc [1,"1";2, "2"; 3, "3"] 4 (fun x y -> x + 1 = y))); N.removeAssoc [1,"1";2,"2"; 3,"3"] 3 (=) =~ [1,"1";2,"2"]; N.removeAssoc [1,"1";2,"2"; 3,"3"] 1 (=) =~ [2,"2"; 3,"3"]; N.removeAssoc [1,"1";2,"2"; 3,"3"] 2 (=) =~ [1,"1"; 3,"3"]; N.removeAssoc [1,"1";2,"2"; 3,"3"] 0 (=) =~ [1,"1"; 2,"2"; 3,"3"]; - N.removeAssoc [1,"1";2,"2"; 3,"3"] 3 eq =~ [1,"1";2,"2"]; - N.removeAssoc [1,"1";2,"2"; 3,"3"] 1 eq =~ [2,"2"; 3,"3"]; - N.removeAssoc [1,"1";2,"2"; 3,"3"] 2 eq =~ [1,"1"; 3,"3"]; - N.removeAssoc [1,"1";2,"2"; 3,"3"] 0 eq =~ [1,"1"; 2,"2"; 3,"3"] + N.removeAssoc [1,"1";2,"2"; 3,"3"] 3 eqx =~ [1,"1";2,"2"]; + N.removeAssoc [1,"1";2,"2"; 3,"3"] 1 eqx =~ [2,"2"; 3,"3"]; + N.removeAssoc [1,"1";2,"2"; 3,"3"] 2 eqx =~ [1,"1"; 3,"3"]; + let ll = [1,"1";2,"2"; 3,"3"] in + let ll0 = N.removeAssoc ll 0 eqx in + b __LOC__ (ll == ll0); + let ll1 = N.setAssoc ll 2 "22" (=) in + eq __LOC__ ll1 [1,"1"; 2, "22"; 3, "3"]; + let ll2 = N.setAssoc ll1 22 "2" (=) in + b __LOC__ (ll2 = ((22, "2") :: ll1)); + b __LOC__ (N.tailExn ll2 == ll1); + b __LOC__ (N.setAssoc [1,"a"; 2, "b"; 3, "c"] 2 "x" (=) = + [1,"a"; 2, "x"; 3,"c"]); + b __LOC__ (N.setAssoc [1,"a"; 3, "c"] 2 "2" (=) = + [2,"2"; 1,"a"; 3, "c"]) let () = diff --git a/jscomp/test/bs_sort_test.js b/jscomp/test/bs_sort_test.js index ca2aa5b8a1..c7b9a3ed74 100644 --- a/jscomp/test/bs_sort_test.js +++ b/jscomp/test/bs_sort_test.js @@ -3,6 +3,7 @@ var Mt = require("./mt.js"); var Bs_Array = require("../../lib/js/bs_Array.js"); var Bs_Range = require("../../lib/js/bs_Range.js"); +var Caml_obj = require("../../lib/js/caml_obj.js"); var Bs_SortArray = require("../../lib/js/bs_SortArray.js"); var Array_data_util = require("./array_data_util.js"); var Bs_SortArrayInt = require("../../lib/js/bs_SortArrayInt.js"); @@ -270,7 +271,14 @@ eq("File \"bs_sort_test.ml\", line 102, characters 5-12", Bs_SortArray.stableSor ] ]); -eq("File \"bs_sort_test.ml\", line 111, characters 5-12", Bs_SortArray.binarySearchBy(/* array */[ +eq("File \"bs_sort_test.ml\", line 111, characters 5-12", Bs_SortArray.binarySearchBy(/* int array */[ + 1, + 3, + 5, + 7 + ], 4, Caml_obj.caml_compare) ^ -1, 2); + +eq("File \"bs_sort_test.ml\", line 112, characters 5-12", Bs_SortArray.binarySearchBy(/* array */[ 1, 2, 3, @@ -280,7 +288,7 @@ eq("File \"bs_sort_test.ml\", line 111, characters 5-12", Bs_SortArray.binarySea 36 ], 33, cmp), 4); -eq("File \"bs_sort_test.ml\", line 112, characters 5-12", Bs_SortArray.binarySearchBy(/* array */[ +eq("File \"bs_sort_test.ml\", line 113, characters 5-12", Bs_SortArray.binarySearchBy(/* array */[ 1, 2, 3, @@ -290,7 +298,7 @@ eq("File \"bs_sort_test.ml\", line 112, characters 5-12", Bs_SortArray.binarySea 36 ], 1, cmp), 0); -eq("File \"bs_sort_test.ml\", line 113, characters 5-12", Bs_SortArray.binarySearchBy(/* array */[ +eq("File \"bs_sort_test.ml\", line 114, characters 5-12", Bs_SortArray.binarySearchBy(/* array */[ 1, 2, 3, @@ -300,7 +308,7 @@ eq("File \"bs_sort_test.ml\", line 113, characters 5-12", Bs_SortArray.binarySea 36 ], 2, cmp), 1); -eq("File \"bs_sort_test.ml\", line 114, characters 5-12", Bs_SortArray.binarySearchBy(/* array */[ +eq("File \"bs_sort_test.ml\", line 115, characters 5-12", Bs_SortArray.binarySearchBy(/* array */[ 1, 2, 3, @@ -310,7 +318,7 @@ eq("File \"bs_sort_test.ml\", line 114, characters 5-12", Bs_SortArray.binarySea 36 ], 3, cmp), 2); -eq("File \"bs_sort_test.ml\", line 115, characters 5-12", Bs_SortArray.binarySearchBy(/* array */[ +eq("File \"bs_sort_test.ml\", line 116, characters 5-12", Bs_SortArray.binarySearchBy(/* array */[ 1, 2, 3, @@ -322,7 +330,7 @@ eq("File \"bs_sort_test.ml\", line 115, characters 5-12", Bs_SortArray.binarySea var aa = Array_data_util.range(0, 1000); -b("File \"bs_sort_test.ml\", line 117, characters 4-11", Bs_Range.every(0, 1000, (function (i) { +b("File \"bs_sort_test.ml\", line 118, characters 4-11", Bs_Range.every(0, 1000, (function (i) { return +(Bs_SortArray.binarySearchBy(aa, i, cmp) === i); }))); @@ -330,15 +338,15 @@ var cc = Bs_Array.map(Array_data_util.range(0, 2000), (function (x) { return (x << 1); })); -eq("File \"bs_sort_test.ml\", line 122, characters 5-12", Bs_SortArray.binarySearchBy(cc, 5000, cmp) ^ -1, 2001); +eq("File \"bs_sort_test.ml\", line 123, characters 5-12", Bs_SortArray.binarySearchBy(cc, 5000, cmp) ^ -1, 2001); -eq("File \"bs_sort_test.ml\", line 123, characters 5-12", Bs_SortArray.binarySearchBy(cc, -1, cmp) ^ -1, 0); +eq("File \"bs_sort_test.ml\", line 124, characters 5-12", Bs_SortArray.binarySearchBy(cc, -1, cmp) ^ -1, 0); -eq("File \"bs_sort_test.ml\", line 124, characters 5-12", Bs_SortArray.binarySearchBy(cc, 0, cmp), 0); +eq("File \"bs_sort_test.ml\", line 125, characters 5-12", Bs_SortArray.binarySearchBy(cc, 0, cmp), 0); -eq("File \"bs_sort_test.ml\", line 126, characters 5-12", Bs_SortArray.binarySearchBy(cc, 1, cmp) ^ -1, 1); +eq("File \"bs_sort_test.ml\", line 127, characters 5-12", Bs_SortArray.binarySearchBy(cc, 1, cmp) ^ -1, 1); -b("File \"bs_sort_test.ml\", line 127, characters 4-11", Bs_Range.every(0, 1999, (function (i) { +b("File \"bs_sort_test.ml\", line 128, characters 4-11", Bs_Range.every(0, 1999, (function (i) { return +((Bs_SortArray.binarySearchBy(cc, (i << 1) + 1 | 0, cmp) ^ -1) === (i + 1 | 0)); }))); @@ -346,27 +354,27 @@ function lt(x, y) { return +(x < y); } -eq("File \"bs_sort_test.ml\", line 134, characters 5-12", Bs_SortArray.strictlySortedLength(/* int array */[], lt), 0); +eq("File \"bs_sort_test.ml\", line 135, characters 5-12", Bs_SortArray.strictlySortedLength(/* int array */[], lt), 0); -eq("File \"bs_sort_test.ml\", line 135, characters 5-12", Bs_SortArray.strictlySortedLength(/* int array */[1], lt), 1); +eq("File \"bs_sort_test.ml\", line 136, characters 5-12", Bs_SortArray.strictlySortedLength(/* int array */[1], lt), 1); -eq("File \"bs_sort_test.ml\", line 136, characters 5-12", Bs_SortArray.strictlySortedLength(/* int array */[ +eq("File \"bs_sort_test.ml\", line 137, characters 5-12", Bs_SortArray.strictlySortedLength(/* int array */[ 1, 1 ], lt), 1); -eq("File \"bs_sort_test.ml\", line 137, characters 5-12", Bs_SortArray.strictlySortedLength(/* int array */[ +eq("File \"bs_sort_test.ml\", line 138, characters 5-12", Bs_SortArray.strictlySortedLength(/* int array */[ 1, 1, 2 ], lt), 1); -eq("File \"bs_sort_test.ml\", line 138, characters 5-12", Bs_SortArray.strictlySortedLength(/* int array */[ +eq("File \"bs_sort_test.ml\", line 139, characters 5-12", Bs_SortArray.strictlySortedLength(/* int array */[ 1, 2 ], lt), 2); -eq("File \"bs_sort_test.ml\", line 139, characters 5-12", Bs_SortArray.strictlySortedLength(/* array */[ +eq("File \"bs_sort_test.ml\", line 140, characters 5-12", Bs_SortArray.strictlySortedLength(/* array */[ 1, 2, 3, @@ -374,7 +382,7 @@ eq("File \"bs_sort_test.ml\", line 139, characters 5-12", Bs_SortArray.strictlyS 3 ], lt), 4); -eq("File \"bs_sort_test.ml\", line 140, characters 5-12", Bs_SortArray.strictlySortedLength(/* array */[ +eq("File \"bs_sort_test.ml\", line 141, characters 5-12", Bs_SortArray.strictlySortedLength(/* array */[ 4, 4, 3, @@ -382,14 +390,14 @@ eq("File \"bs_sort_test.ml\", line 140, characters 5-12", Bs_SortArray.strictlyS 1 ], lt), 1); -eq("File \"bs_sort_test.ml\", line 141, characters 5-12", Bs_SortArray.strictlySortedLength(/* int array */[ +eq("File \"bs_sort_test.ml\", line 142, characters 5-12", Bs_SortArray.strictlySortedLength(/* int array */[ 4, 3, 2, 1 ], lt), -4); -eq("File \"bs_sort_test.ml\", line 142, characters 5-12", Bs_SortArray.strictlySortedLength(/* array */[ +eq("File \"bs_sort_test.ml\", line 143, characters 5-12", Bs_SortArray.strictlySortedLength(/* array */[ 4, 3, 2, diff --git a/jscomp/test/bs_sort_test.ml b/jscomp/test/bs_sort_test.ml index a43b3a37ed..966ce3afb3 100644 --- a/jscomp/test/bs_sort_test.ml +++ b/jscomp/test/bs_sort_test.ml @@ -108,6 +108,7 @@ let () = let () = + eq __LOC__ (lnot (S.binarySearchBy [|1;3;5;7|] 4 compare)) 2 ; eq __LOC__ (S.binarySearchBy [|1;2;3;4;33;35;36|] 33 cmp ) 4 ; eq __LOC__ (S.binarySearchBy [|1;2;3;4;33;35;36|] 1 cmp ) 0; eq __LOC__ (S.binarySearchBy [|1;2;3;4;33;35;36|] 2 cmp ) 1; diff --git a/jscomp/test/bs_stack_test.ml b/jscomp/test/bs_stack_test.ml index 6252d81494..dd745bb92b 100644 --- a/jscomp/test/bs_stack_test.ml +++ b/jscomp/test/bs_stack_test.ml @@ -15,7 +15,7 @@ module Q = Bs.MutableQueue let inOrder (v : t) = let current = ref v in - let s : node S.t = S.create () in + let s : node S.t = S.make () in let q : int Q.t = Q.make () in while !current != Js.undefined do let v = Js.Undefined.getUnsafe !current in @@ -37,7 +37,7 @@ let inOrder (v : t) = let inOrder3 (v : t) = let current = ref v in - let s : node S.t = S.create () in + let s : node S.t = S.make () in let q : int Q.t = Q.make () in while !current != Js.undefined do let v = Js.Undefined.getUnsafe !current in @@ -58,7 +58,7 @@ let inOrder3 (v : t) = let inOrder2 (v : t) = let todo = ref true in let cursor = ref v in - let s : node S.t = S.create () in + let s : node S.t = S.make () in let q : int Q.t = Q.make () in while !todo do if !cursor != Js.undefined then diff --git a/lib/js/bs_Array.js b/lib/js/bs_Array.js index 45e4c5d3d9..f030df1ba2 100644 --- a/lib/js/bs_Array.js +++ b/lib/js/bs_Array.js @@ -22,9 +22,9 @@ function getExn(arr, i) { function set(arr, i, v) { if (i >= 0 && i < arr.length) { arr[i] = v; - return /* () */0; + return /* true */1; } else { - return 0; + return /* false */0; } } diff --git a/lib/js/bs_List.js b/lib/js/bs_List.js index 8a87c9a05f..0a39d8cb1d 100644 --- a/lib/js/bs_List.js +++ b/lib/js/bs_List.js @@ -10,6 +10,14 @@ function head(x) { } } +function headExn(x) { + if (x) { + return x[0]; + } else { + throw new Error("headExn"); + } +} + function tail(x) { if (x) { return /* Some */[x[1]]; @@ -18,6 +26,14 @@ function tail(x) { } } +function tailExn(x) { + if (x) { + return x[1]; + } else { + throw new Error("tailExn"); + } +} + function add(xs, x) { return /* :: */[ x, @@ -219,7 +235,7 @@ function removeAssocAuxWithMap(_cellX, x, _prec, f) { var h = cellX[0]; if (f(h[0], x)) { prec[1] = t; - return /* () */0; + return /* true */1; } else { var next = /* :: */[ h, @@ -232,7 +248,40 @@ function removeAssocAuxWithMap(_cellX, x, _prec, f) { } } else { - return /* () */0; + return /* false */0; + } + }; +} + +function setAssocAuxWithMap(_cellX, x, k, _prec, eq) { + while(true) { + var prec = _prec; + var cellX = _cellX; + if (cellX) { + var t = cellX[1]; + var h = cellX[0]; + if (eq(h[0], x)) { + prec[1] = /* :: */[ + /* tuple */[ + x, + k + ], + t + ]; + return /* true */1; + } else { + var next = /* :: */[ + h, + /* [] */0 + ]; + prec[1] = next; + _prec = next; + _cellX = t; + continue ; + + } + } else { + return /* false */0; } }; } @@ -1087,7 +1136,7 @@ function has(xs, x, eq) { return hasU(xs, x, Curry.__2(eq)); } -function assocU(_xs, x, eq) { +function getAssocU(_xs, x, eq) { while(true) { var xs = _xs; if (xs) { @@ -1105,8 +1154,8 @@ function assocU(_xs, x, eq) { }; } -function assoc(xs, x, eq) { - return assocU(xs, x, Curry.__2(eq)); +function getAssoc(xs, x, eq) { + return getAssocU(xs, x, Curry.__2(eq)); } function hasAssocU(_xs, x, eq) { @@ -1141,8 +1190,12 @@ function removeAssocU(xs, x, eq) { pair, /* [] */0 ]; - removeAssocAuxWithMap(l, x, cell, eq); - return cell; + var removed = removeAssocAuxWithMap(l, x, cell, eq); + if (removed) { + return cell; + } else { + return xs; + } } } else { return /* [] */0; @@ -1153,6 +1206,39 @@ function removeAssoc(xs, x, eq) { return removeAssocU(xs, x, Curry.__2(eq)); } +function setAssocU(xs, x, k, eq) { + if (xs) { + var l = xs[1]; + var pair = xs[0]; + if (eq(pair[0], x)) { + return l; + } else { + var cell = /* :: */[ + pair, + /* [] */0 + ]; + var replaced = setAssocAuxWithMap(l, x, k, cell, eq); + if (replaced) { + return cell; + } else { + return /* :: */[ + /* tuple */[ + x, + k + ], + xs + ]; + } + } + } else { + return /* [] */0; + } +} + +function setAssoc(xs, x, k, eq) { + return setAssocU(xs, x, k, Curry.__2(eq)); +} + function getByU(_xs, p) { while(true) { var xs = _xs; @@ -1316,7 +1402,9 @@ var size = length; exports.length = length; exports.size = size; exports.head = head; +exports.headExn = headExn; exports.tail = tail; +exports.tailExn = tailExn; exports.add = add; exports.get = get; exports.getExn = getExn; @@ -1381,10 +1469,12 @@ exports.keepMap = keepMap; exports.partitionU = partitionU; exports.partition = partition; exports.unzip = unzip; -exports.assocU = assocU; -exports.assoc = assoc; +exports.getAssocU = getAssocU; +exports.getAssoc = getAssoc; exports.hasAssocU = hasAssocU; exports.hasAssoc = hasAssoc; exports.removeAssocU = removeAssocU; exports.removeAssoc = removeAssoc; +exports.setAssocU = setAssocU; +exports.setAssoc = setAssoc; /* No side effect */ diff --git a/lib/js/bs_MutableStack.js b/lib/js/bs_MutableStack.js index 0c3c953558..fcbaf27473 100644 --- a/lib/js/bs_MutableStack.js +++ b/lib/js/bs_MutableStack.js @@ -2,7 +2,7 @@ var Curry = require("./curry.js"); -function create() { +function make() { return { root: null }; @@ -127,7 +127,7 @@ function dynamicPopIter(s, f) { return dynamicPopIterU(s, Curry.__1(f)); } -exports.create = create; +exports.make = make; exports.clear = clear; exports.copy = copy; exports.push = push; From b59ee5ed12fec14f93d6d95c8d5c54b505a4eac3 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 6 Feb 2018 18:24:51 +0800 Subject: [PATCH 3/3] more docs, add missed some2,every2 in array --- jscomp/others/bs_Array.ml | 39 +++++++-- jscomp/others/bs_Array.mli | 27 +++++- jscomp/others/bs_List.ml | 4 +- jscomp/others/bs_List.mli | 63 +++++++++++--- jscomp/test/bs_array_test.js | 162 +++++++++++++++++++++++++++++++++-- jscomp/test/bs_array_test.ml | 20 +++++ jscomp/test/bs_list_test.js | 120 +++++++++++++++++++------- jscomp/test/bs_list_test.ml | 10 ++- lib/js/bs_Array.js | 105 +++++++++++++++++------ lib/js/bs_List.js | 4 +- 10 files changed, 461 insertions(+), 93 deletions(-) diff --git a/jscomp/others/bs_Array.ml b/jscomp/others/bs_Array.ml index 9dd6d6a790..770f4d7f70 100644 --- a/jscomp/others/bs_Array.ml +++ b/jscomp/others/bs_Array.ml @@ -311,29 +311,54 @@ let rec everyAux arr i b len = everyAux arr (i + 1) b len else false +let rec someAux arr i b len = + if i = len then false + else + if b (getUnsafe arr i) [@bs] then true + else someAux arr (i + 1) b len + let everyU arr b = let len = length arr in everyAux arr 0 b len let every arr f = everyU arr (fun[@bs] b -> f b) +let someU arr b = + let len = length arr in + someAux arr 0 b len +let some arr f = someU arr (fun [@bs] b -> f b) + let rec everyAux2 arr1 arr2 i b len = if i = len then true else if b (getUnsafe arr1 i) (getUnsafe arr2 i) [@bs] then everyAux2 arr1 arr2 (i + 1) b len else false +let rec someAux2 arr1 arr2 i b len = + if i = len then false + else if b (getUnsafe arr1 i) (getUnsafe arr2 i) [@bs] then + true + else someAux2 arr1 arr2 (i + 1) b len + + let every2U a b p = - let lena = length a in - let lenb = length b in - if lena <> lenb then false - else - everyAux2 a b 0 p lena + everyAux2 a b 0 p (min (length a) (length b)) let every2 a b p = every2U a b (fun[@bs] a b -> p a b) + +let some2U a b p = + someAux2 a b 0 p (min (length a) (length b)) + +let some2 a b p = some2U a b (fun [@bs] a b -> p a b) -let eqU = every2U -let eq = every2 +let eqU a b p = + let lena = length a in + let lenb = length b in + if lena = lenb then + everyAux2 a b 0 p lena + else false + +let eq a b p = eqU a b (fun [@bs] a b -> p a b ) let rec everyCmpAux2 arr1 arr2 i b len = if i = len then 0 diff --git a/jscomp/others/bs_Array.mli b/jscomp/others/bs_Array.mli index 34ca6214dc..09b2e778f4 100644 --- a/jscomp/others/bs_Array.mli +++ b/jscomp/others/bs_Array.mli @@ -241,6 +241,12 @@ val reduceReverse: 'b array -> 'a -> ('a -> 'b -> 'a ) -> 'a reduceReverse [|1;2;3;4|] 100 (-) = 90 ]} *) + +val someU: 'a array -> ('a -> bool [@bs]) -> bool +val some: 'a array -> ('a -> bool) -> bool +(** [some xs p] + @return true if one of element satifies [p] +*) val everyU: 'a array -> ('a -> bool [@bs]) -> bool val every: 'a array -> ('a -> bool ) -> bool @@ -250,11 +256,26 @@ val every: 'a array -> ('a -> bool ) -> bool val every2U: 'a array -> 'b array -> ('a -> 'b -> bool [@bs]) -> bool val every2: 'a array -> 'b array -> ('a -> 'b -> bool ) -> bool -(** [every2 a b p] - - return false when [length a <> length b] - - return true when every pair is true [f ai bi] +(** [every2 xs ys p] only tests the length of shorter + + @example {[ + every2 [|1;2;3|] [|0;1|] (>) = true;; + (every2 [||] [|1|] (fun x y -> x > y)) = true;; + (every2 [|2;3|] [|1|] (fun x y -> x > y)) = true;; + ]} *) +val some2U: 'a array -> 'b array -> ('a -> 'b -> bool [@bs]) -> bool +val some2: 'a array -> 'b array -> ('a -> 'b -> bool ) -> bool +(** [some2 xs ys p] only tests the length of shorter + + @example {[ + some2 [|0;2|] [|1;0;3|] (>) = true ;; + (some2 [||] [|1|] (fun x y -> x > y)) = false;; + (some2 [|2;3|] [|1;4|] (fun x y -> x > y)) = true;; + ]} +*) + val cmpU: 'a array -> 'a array -> ('a -> 'a -> int [@bs]) -> int val cmp: 'a array -> 'a array -> ('a -> 'a -> int ) -> int (** [cmp a b] diff --git a/jscomp/others/bs_List.ml b/jscomp/others/bs_List.ml index 09a874e235..82b59fb79e 100644 --- a/jscomp/others/bs_List.ml +++ b/jscomp/others/bs_List.ml @@ -568,7 +568,7 @@ let some xs p = someU xs (fun[@bs] x -> p x) let rec every2U l1 l2 p = match (l1, l2) with - (_, []) | [],_ -> true + | _,[] | [], _ -> true | (a1::l1, a2::l2) -> p a1 a2 [@bs] && every2U l1 l2 p let every2 l1 l2 p = every2U l1 l2 (fun[@bs] a b -> p a b) @@ -599,7 +599,7 @@ let eq l1 l2 f = eqU l1 l2 (fun [@bs] x y -> f x y) let rec some2U l1 l2 p = match (l1, l2) with - [], _ | _, [] -> false + | [], _ | _, [] -> false | (a1::l1, a2::l2) -> p a1 a2 [@bs] || some2U l1 l2 p let some2 l1 l2 p = some2U l1 l2 (fun[@bs] a b -> p a b) diff --git a/jscomp/others/bs_List.mli b/jscomp/others/bs_List.mli index 984bee0862..75a2774281 100644 --- a/jscomp/others/bs_List.mli +++ b/jscomp/others/bs_List.mli @@ -228,16 +228,42 @@ val reduceReverse2: ]} *) -val everyU: 'a t -> ('a -> bool [@bs]) -> bool -val every: 'a t -> ('a -> bool) -> bool +val everyU: 'a t -> ('a -> bool [@bs]) -> bool +val every: 'a t -> ('a -> bool ) -> bool +(** [every ls p] + @example {[ + every [] (fun x -> x mod 2 = 0) = true;; + every [2;4] (fun x -> x mod 2 = 0 ) true;; + ]} +*) val someU: 'a t -> ('a -> bool [@bs]) -> bool -val some: 'a t -> ('a -> bool) -> bool +val some: 'a t -> ('a -> bool ) -> bool +(** [some ls p] -val every2U: 'a t -> 'b t -> ('a -> 'b -> bool [@bs]) -> bool -val every2: 'a t -> 'b t -> ('a -> 'b -> bool) -> bool + @example {[ + some [] (fun x -> x mod 2 = 0) = false ;; + some [1;2] (fun x -> x mod 2 = 0) = true;;) + ]} +*) +val every2U: 'a t -> 'b t -> ('a -> 'b -> bool [@bs]) -> bool +val every2: 'a t -> 'b t -> ('a -> 'b -> bool ) -> bool +(** [every2 xs ys p] stop with the shorter list + @example {[ + (every2 [] [1] (fun x y -> x > y)) = true;; + (every2 [2;3] [1] (fun x y -> x > y)) = true;; + ]} +*) +val some2U: 'a t -> 'b t -> ('a -> 'b -> bool [@bs]) -> bool +val some2: 'a t -> 'b t -> ('a -> 'b -> bool) -> bool +(** [some2 xs ys p] + @example {[ + (some2 [] [1] (fun x y -> x > y)) = false;; + (some2 [2;3] [1;4] (fun x y -> x > y)) = true;; + ]} +*) val cmpU: 'a t -> 'a t -> ('a -> 'a -> int [@bs]) -> int val cmp: 'a t -> 'a t -> ('a -> 'a -> int) -> int (** @@ -261,8 +287,6 @@ val eq: 'a t -> 'a t -> ('a -> 'a -> bool) -> bool ]} *) -val some2U: 'a t -> 'b t -> ('a -> 'b -> bool [@bs]) -> bool -val some2: 'a t -> 'b t -> ('a -> 'b -> bool) -> bool val hasU: 'a t -> 'b -> ('a -> 'b -> bool [@bs]) -> bool val has: 'a t -> 'b -> ('a -> 'b -> bool) -> bool @@ -271,15 +295,27 @@ val has: 'a t -> 'b -> ('a -> 'b -> bool) -> bool val getByU: 'a t -> ('a -> bool [@bs]) -> 'a option val getBy: 'a t -> ('a -> bool) -> 'a option -val keepU: 'a t -> ('a -> bool [@bs]) -> 'a t -val keep: 'a t -> ('a -> bool) -> 'a t +val keepU: 'a t -> ('a -> bool [@bs]) -> 'a t +val keep: 'a t -> ('a -> bool) -> 'a t +(** [keep xs p] + @example {[ + keep [1;2;3;4] (fun x -> x mod 2 = 0) = + [2;4] + ]} +*) val keepMapU: 'a t -> ('a -> 'b option [@bs]) -> 'b t val keepMap: 'a t -> ('a -> 'b option) -> 'b t -val partitionU: 'a t -> ('a -> bool [@bs]) -> 'a t * 'a t -val partition: 'a t -> ('a -> bool) -> 'a t * 'a t +val partitionU: 'a t -> ('a -> bool [@bs]) -> 'a t * 'a t +val partition: 'a t -> ('a -> bool) -> 'a t * 'a t +(** [partition xs p] + @example {[ + partition [1;2;3;4] (fun x -> x mod 2 = 0) = + ([2;4], [1;3]) + ]} +*) val unzip: ('a * 'b) t -> 'a t * 'b t val getAssocU: ('a * 'c) t -> 'b -> ('a -> 'b -> bool [@bs]) -> 'c option @@ -314,8 +350,9 @@ val removeAssoc: ('a * 'c) t -> 'b -> ('a -> 'b -> bool) -> ('a * 'c) t val setAssocU: ('a * 'c) t -> 'a -> 'c -> ('a -> 'a -> bool [@bs]) -> ('a * 'c) t val setAssoc: ('a * 'c) t -> 'a -> 'c -> ('a -> 'a -> bool) -> ('a * 'c) t -(** [setAssoc xs x eq] - +(** [setAssoc xs k v eq] + if [k] exists in [xs], replace it with the new [v], otherwise, add + it to the head @example {[ setAssoc [1,"a"; 2, "b"; 3, "c"] 2 "x" (=) = [1,"a"; 2, "x"; 3,"c"] ;; diff --git a/jscomp/test/bs_array_test.js b/jscomp/test/bs_array_test.js index 4ca045cc94..94115ab060 100644 --- a/jscomp/test/bs_array_test.js +++ b/jscomp/test/bs_array_test.js @@ -787,7 +787,153 @@ id$1("File \"bs_array_test.ml\", line 200, characters 5-12", /* int array */[ 4 ]); -eq("File \"bs_array_test.ml\", line 204, characters 5-12", Bs_Array.concat(/* int array */[], /* int array */[ +function every2(xs, ys) { + var partial_arg = Bs_List.toArray(ys); + var partial_arg$1 = Bs_List.toArray(xs); + return (function (param) { + return Bs_Array.every2(partial_arg$1, partial_arg, param); + }); +} + +function some2(xs, ys) { + var partial_arg = Bs_List.toArray(ys); + var partial_arg$1 = Bs_List.toArray(xs); + return (function (param) { + return Bs_Array.some2(partial_arg$1, partial_arg, param); + }); +} + +eq("File \"bs_array_test.ml\", line 210, characters 5-12", every2(/* [] */0, /* :: */[ + 1, + /* [] */0 + ])((function (x, y) { + return +(x > y); + })), /* true */1); + +eq("File \"bs_array_test.ml\", line 211, characters 5-12", every2(/* :: */[ + 2, + /* :: */[ + 3, + /* [] */0 + ] + ], /* :: */[ + 1, + /* [] */0 + ])((function (x, y) { + return +(x > y); + })), /* true */1); + +eq("File \"bs_array_test.ml\", line 212, characters 5-12", every2(/* :: */[ + 2, + /* [] */0 + ], /* :: */[ + 1, + /* [] */0 + ])((function (x, y) { + return +(x > y); + })), /* true */1); + +eq("File \"bs_array_test.ml\", line 213, characters 5-12", every2(/* :: */[ + 2, + /* :: */[ + 3, + /* [] */0 + ] + ], /* :: */[ + 1, + /* :: */[ + 4, + /* [] */0 + ] + ])((function (x, y) { + return +(x > y); + })), /* false */0); + +eq("File \"bs_array_test.ml\", line 214, characters 5-12", every2(/* :: */[ + 2, + /* :: */[ + 3, + /* [] */0 + ] + ], /* :: */[ + 1, + /* :: */[ + 0, + /* [] */0 + ] + ])((function (x, y) { + return +(x > y); + })), /* true */1); + +eq("File \"bs_array_test.ml\", line 215, characters 5-12", some2(/* [] */0, /* :: */[ + 1, + /* [] */0 + ])((function (x, y) { + return +(x > y); + })), /* false */0); + +eq("File \"bs_array_test.ml\", line 216, characters 5-12", some2(/* :: */[ + 2, + /* :: */[ + 3, + /* [] */0 + ] + ], /* :: */[ + 1, + /* [] */0 + ])((function (x, y) { + return +(x > y); + })), /* true */1); + +eq("File \"bs_array_test.ml\", line 217, characters 5-12", some2(/* :: */[ + 2, + /* :: */[ + 3, + /* [] */0 + ] + ], /* :: */[ + 1, + /* :: */[ + 4, + /* [] */0 + ] + ])((function (x, y) { + return +(x > y); + })), /* true */1); + +eq("File \"bs_array_test.ml\", line 218, characters 5-12", some2(/* :: */[ + 0, + /* :: */[ + 3, + /* [] */0 + ] + ], /* :: */[ + 1, + /* :: */[ + 4, + /* [] */0 + ] + ])((function (x, y) { + return +(x > y); + })), /* false */0); + +eq("File \"bs_array_test.ml\", line 219, characters 5-12", some2(/* :: */[ + 0, + /* :: */[ + 3, + /* [] */0 + ] + ], /* :: */[ + 3, + /* :: */[ + 2, + /* [] */0 + ] + ])((function (x, y) { + return +(x > y); + })), /* true */1); + +eq("File \"bs_array_test.ml\", line 224, characters 5-12", Bs_Array.concat(/* int array */[], /* int array */[ 1, 2, 3 @@ -797,9 +943,9 @@ eq("File \"bs_array_test.ml\", line 204, characters 5-12", Bs_Array.concat(/* in 3 ]); -eq("File \"bs_array_test.ml\", line 205, characters 5-12", Bs_Array.concat(/* array */[], /* array */[]), /* array */[]); +eq("File \"bs_array_test.ml\", line 225, characters 5-12", Bs_Array.concat(/* array */[], /* array */[]), /* array */[]); -eq("File \"bs_array_test.ml\", line 206, characters 5-12", Bs_Array.concat(/* int array */[ +eq("File \"bs_array_test.ml\", line 226, characters 5-12", Bs_Array.concat(/* int array */[ 3, 2 ], /* int array */[ @@ -814,7 +960,7 @@ eq("File \"bs_array_test.ml\", line 206, characters 5-12", Bs_Array.concat(/* in 3 ]); -eq("File \"bs_array_test.ml\", line 207, characters 5-12", Bs_Array.concatMany(/* array */[ +eq("File \"bs_array_test.ml\", line 227, characters 5-12", Bs_Array.concatMany(/* array */[ /* int array */[ 3, 2 @@ -832,7 +978,7 @@ eq("File \"bs_array_test.ml\", line 207, characters 5-12", Bs_Array.concatMany(/ 3 ]); -eq("File \"bs_array_test.ml\", line 208, characters 5-12", Bs_Array.concatMany(/* array */[ +eq("File \"bs_array_test.ml\", line 228, characters 5-12", Bs_Array.concatMany(/* array */[ /* int array */[ 3, 2 @@ -853,7 +999,7 @@ eq("File \"bs_array_test.ml\", line 208, characters 5-12", Bs_Array.concatMany(/ 0 ]); -eq("File \"bs_array_test.ml\", line 209, characters 5-12", Bs_Array.concatMany(/* array */[ +eq("File \"bs_array_test.ml\", line 229, characters 5-12", Bs_Array.concatMany(/* array */[ /* int array */[], /* int array */[ 3, @@ -875,12 +1021,12 @@ eq("File \"bs_array_test.ml\", line 209, characters 5-12", Bs_Array.concatMany(/ 0 ]); -eq("File \"bs_array_test.ml\", line 210, characters 5-12", Bs_Array.concatMany(/* array */[ +eq("File \"bs_array_test.ml\", line 230, characters 5-12", Bs_Array.concatMany(/* array */[ /* array */[], /* array */[] ]), /* array */[]); -Mt.from_pair_suites("File \"bs_array_test.ml\", line 212, characters 23-30", suites[0]); +Mt.from_pair_suites("File \"bs_array_test.ml\", line 232, characters 23-30", suites[0]); var A = 0; diff --git a/jscomp/test/bs_array_test.ml b/jscomp/test/bs_array_test.ml index a08bc682de..2cec95a24b 100644 --- a/jscomp/test/bs_array_test.ml +++ b/jscomp/test/bs_array_test.ml @@ -200,6 +200,26 @@ let () = id __LOC__ [|1;2;3;4|] ;; +let () = + let module N = struct + let every2 xs ys = + A.every2 (L.toArray xs ) (L.toArray ys) + let some2 xs ys = + A.some2 (L.toArray xs) (L.toArray ys) + end in + eq __LOC__ (N.every2 [] [1] (fun x y -> x > y)) true; + eq __LOC__ (N.every2 [2;3] [1] (fun x y -> x > y)) true; + eq __LOC__ (N.every2 [2] [1] (fun x y -> x > y)) true; + eq __LOC__ (N.every2 [2;3] [1;4] (fun x y -> x > y)) false; + eq __LOC__ (N.every2 [2;3] [1;0] (fun x y -> x > y)) true; + eq __LOC__ (N.some2 [] [1] (fun x y -> x > y)) false; + eq __LOC__ (N.some2 [2;3] [1] (fun x y -> x > y)) true; + eq __LOC__ (N.some2 [2;3] [1;4] (fun x y -> x > y)) true; + eq __LOC__ (N.some2 [0;3] [1;4] (fun x y -> x > y)) false; + eq __LOC__ (N.some2 [0;3] [3;2] (fun x y -> x > y)) true + + + let () = eq __LOC__ (A.concat [||] [|1;2;3|]) [|1;2;3|]; eq __LOC__ (A.concat [||] [||]) [||]; diff --git a/jscomp/test/bs_list_test.js b/jscomp/test/bs_list_test.js index 184ca7382c..d36cbe208d 100644 --- a/jscomp/test/bs_list_test.js +++ b/jscomp/test/bs_list_test.js @@ -1437,14 +1437,53 @@ eq("File \"bs_list_test.ml\", line 239, characters 5-12", Bs_List.some(/* :: */[ eq("File \"bs_list_test.ml\", line 240, characters 5-12", Bs_List.some(/* [] */0, mod2), /* false */0); -eq("File \"bs_list_test.ml\", line 241, characters 5-12", Bs_List.every2(/* [] */0, /* :: */[ +eq("File \"bs_list_test.ml\", line 241, characters 5-12", Bs_List.has(/* :: */[ + 1, + /* :: */[ + 2, + /* :: */[ + 3, + /* [] */0 + ] + ] + ], "2", (function (x, s) { + return +("" + x === s); + })), /* true */1); + +eq("File \"bs_list_test.ml\", line 242, characters 5-12", Bs_List.has(/* :: */[ + 1, + /* :: */[ + 2, + /* :: */[ + 3, + /* [] */0 + ] + ] + ], "0", (function (x, s) { + return +("" + x === s); + })), /* false */0); + +eq("File \"bs_list_test.ml\", line 245, characters 5-12", Bs_List.every2(/* [] */0, /* :: */[ + 1, + /* [] */0 + ], (function (x, y) { + return +(x > y); + })), /* true */1); + +eq("File \"bs_list_test.ml\", line 246, characters 5-12", Bs_List.every2(/* :: */[ + 2, + /* :: */[ + 3, + /* [] */0 + ] + ], /* :: */[ 1, /* [] */0 ], (function (x, y) { return +(x > y); })), /* true */1); -eq("File \"bs_list_test.ml\", line 242, characters 5-12", Bs_List.every2(/* :: */[ +eq("File \"bs_list_test.ml\", line 247, characters 5-12", Bs_List.every2(/* :: */[ 2, /* [] */0 ], /* :: */[ @@ -1454,7 +1493,7 @@ eq("File \"bs_list_test.ml\", line 242, characters 5-12", Bs_List.every2(/* :: * return +(x > y); })), /* true */1); -eq("File \"bs_list_test.ml\", line 243, characters 5-12", Bs_List.every2(/* :: */[ +eq("File \"bs_list_test.ml\", line 248, characters 5-12", Bs_List.every2(/* :: */[ 2, /* :: */[ 3, @@ -1470,14 +1509,43 @@ eq("File \"bs_list_test.ml\", line 243, characters 5-12", Bs_List.every2(/* :: * return +(x > y); })), /* false */0); -eq("File \"bs_list_test.ml\", line 244, characters 5-12", Bs_List.some2(/* [] */0, /* :: */[ +eq("File \"bs_list_test.ml\", line 249, characters 5-12", Bs_List.every2(/* :: */[ + 2, + /* :: */[ + 3, + /* [] */0 + ] + ], /* :: */[ + 1, + /* :: */[ + 0, + /* [] */0 + ] + ], (function (x, y) { + return +(x > y); + })), /* true */1); + +eq("File \"bs_list_test.ml\", line 250, characters 5-12", Bs_List.some2(/* [] */0, /* :: */[ 1, /* [] */0 ], (function (x, y) { return +(x > y); })), /* false */0); -eq("File \"bs_list_test.ml\", line 245, characters 5-12", Bs_List.some2(/* :: */[ +eq("File \"bs_list_test.ml\", line 251, characters 5-12", Bs_List.some2(/* :: */[ + 2, + /* :: */[ + 3, + /* [] */0 + ] + ], /* :: */[ + 1, + /* [] */0 + ], (function (x, y) { + return +(x > y); + })), /* true */1); + +eq("File \"bs_list_test.ml\", line 252, characters 5-12", Bs_List.some2(/* :: */[ 2, /* :: */[ 3, @@ -1493,7 +1561,7 @@ eq("File \"bs_list_test.ml\", line 245, characters 5-12", Bs_List.some2(/* :: */ return +(x > y); })), /* true */1); -eq("File \"bs_list_test.ml\", line 246, characters 5-12", Bs_List.some2(/* :: */[ +eq("File \"bs_list_test.ml\", line 253, characters 5-12", Bs_List.some2(/* :: */[ 0, /* :: */[ 3, @@ -1509,34 +1577,24 @@ eq("File \"bs_list_test.ml\", line 246, characters 5-12", Bs_List.some2(/* :: */ return +(x > y); })), /* false */0); -eq("File \"bs_list_test.ml\", line 247, characters 5-12", Bs_List.has(/* :: */[ - 1, +eq("File \"bs_list_test.ml\", line 254, characters 5-12", Bs_List.some2(/* :: */[ + 0, /* :: */[ - 2, - /* :: */[ - 3, - /* [] */0 - ] + 3, + /* [] */0 ] - ], "2", (function (x, s) { - return +("" + x === s); - })), /* true */1); - -eq("File \"bs_list_test.ml\", line 248, characters 5-12", Bs_List.has(/* :: */[ - 1, + ], /* :: */[ + 3, /* :: */[ 2, - /* :: */[ - 3, - /* [] */0 - ] + /* [] */0 ] - ], "0", (function (x, s) { - return +("" + x === s); - })), /* false */0); + ], (function (x, y) { + return +(x > y); + })), /* true */1); function makeTest(n) { - return eq("File \"bs_list_test.ml\", line 251, characters 5-12", Bs_List.make(n, 3), Bs_List.makeBy(n, (function () { + return eq("File \"bs_list_test.ml\", line 257, characters 5-12", Bs_List.make(n, 3), Bs_List.makeBy(n, (function () { return 3; }))); } @@ -1549,7 +1607,7 @@ makeTest(2); makeTest(3); -b("File \"bs_list_test.ml\", line 260, characters 4-11", 1 - Bs_List.eq(/* :: */[ +b("File \"bs_list_test.ml\", line 266, characters 4-11", 1 - Bs_List.eq(/* :: */[ 1, /* :: */[ 2, @@ -1568,7 +1626,7 @@ b("File \"bs_list_test.ml\", line 260, characters 4-11", 1 - Bs_List.eq(/* :: */ return +(x === y); }))); -b("File \"bs_list_test.ml\", line 261, characters 4-11", Bs_List.eq(/* :: */[ +b("File \"bs_list_test.ml\", line 267, characters 4-11", Bs_List.eq(/* :: */[ 1, /* :: */[ 2, @@ -1590,7 +1648,7 @@ b("File \"bs_list_test.ml\", line 261, characters 4-11", Bs_List.eq(/* :: */[ return +(x === y); }))); -b("File \"bs_list_test.ml\", line 262, characters 4-11", 1 - Bs_List.eq(/* :: */[ +b("File \"bs_list_test.ml\", line 268, characters 4-11", 1 - Bs_List.eq(/* :: */[ 1, /* :: */[ 2, @@ -1624,7 +1682,7 @@ var u1 = Bs_List.keepMap(u0, (function (x) { } })); -eq("File \"bs_list_test.ml\", line 266, characters 5-12", u1, /* :: */[ +eq("File \"bs_list_test.ml\", line 272, characters 5-12", u1, /* :: */[ 1, /* :: */[ 8, diff --git a/jscomp/test/bs_list_test.ml b/jscomp/test/bs_list_test.ml index cbf7b79af9..735e986eee 100644 --- a/jscomp/test/bs_list_test.ml +++ b/jscomp/test/bs_list_test.ml @@ -238,14 +238,20 @@ let () = eq __LOC__ (N.some [1;2;5] mod2) true; eq __LOC__ (N.some [1;3;5] mod2) false; eq __LOC__ (N.some [] mod2) false; + eq __LOC__ (N.has [1;2;3] "2" (fun x s -> string_of_int x = s)) true; + eq __LOC__ (N.has [1;2;3] "0" (fun x s -> string_of_int x = s)) false + +let () = eq __LOC__ (N.every2 [] [1] (fun x y -> x > y)) true; + eq __LOC__ (N.every2 [2;3] [1] (fun x y -> x > y)) true; eq __LOC__ (N.every2 [2] [1] (fun x y -> x > y)) true; eq __LOC__ (N.every2 [2;3] [1;4] (fun x y -> x > y)) false; + eq __LOC__ (N.every2 [2;3] [1;0] (fun x y -> x > y)) true; eq __LOC__ (N.some2 [] [1] (fun x y -> x > y)) false; + eq __LOC__ (N.some2 [2;3] [1] (fun x y -> x > y)) true; eq __LOC__ (N.some2 [2;3] [1;4] (fun x y -> x > y)) true; eq __LOC__ (N.some2 [0;3] [1;4] (fun x y -> x > y)) false; - eq __LOC__ (N.has [1;2;3] "2" (fun x s -> string_of_int x = s)) true; - eq __LOC__ (N.has [1;2;3] "0" (fun x s -> string_of_int x = s)) false + eq __LOC__ (N.some2 [0;3] [3;2] (fun x y -> x > y)) true let makeTest n = eq __LOC__ (N.make n 3) (N.makeBy n (fun _ -> 3)) diff --git a/lib/js/bs_Array.js b/lib/js/bs_Array.js index f030df1ba2..62e09bf3b7 100644 --- a/lib/js/bs_Array.js +++ b/lib/js/bs_Array.js @@ -397,34 +397,89 @@ function every(arr, f) { return everyU(arr, Curry.__1(f)); } +function someU(arr, b) { + var len = arr.length; + var arr$1 = arr; + var _i = 0; + var b$1 = b; + var len$1 = len; + while(true) { + var i = _i; + if (i === len$1) { + return /* false */0; + } else if (b$1(arr$1[i])) { + return /* true */1; + } else { + _i = i + 1 | 0; + continue ; + + } + }; +} + +function some(arr, f) { + return someU(arr, Curry.__1(f)); +} + +function everyAux2(arr1, arr2, _i, b, len) { + while(true) { + var i = _i; + if (i === len) { + return /* true */1; + } else if (b(arr1[i], arr2[i])) { + _i = i + 1 | 0; + continue ; + + } else { + return /* false */0; + } + }; +} + function every2U(a, b, p) { + return everyAux2(a, b, 0, p, Caml_primitive.caml_int_min(a.length, b.length)); +} + +function every2(a, b, p) { + return every2U(a, b, Curry.__2(p)); +} + +function some2U(a, b, p) { + var arr1 = a; + var arr2 = b; + var _i = 0; + var b$1 = p; + var len = Caml_primitive.caml_int_min(a.length, b.length); + while(true) { + var i = _i; + if (i === len) { + return /* false */0; + } else if (b$1(arr1[i], arr2[i])) { + return /* true */1; + } else { + _i = i + 1 | 0; + continue ; + + } + }; +} + +function some2(a, b, p) { + return some2U(a, b, Curry.__2(p)); +} + +function eqU(a, b, p) { var lena = a.length; var lenb = b.length; - if (lena !== lenb) { - return /* false */0; + if (lena === lenb) { + return everyAux2(a, b, 0, p, lena); } else { - var arr1 = a; - var arr2 = b; - var _i = 0; - var b$1 = p; - var len = lena; - while(true) { - var i = _i; - if (i === len) { - return /* true */1; - } else if (b$1(arr1[i], arr2[i])) { - _i = i + 1 | 0; - continue ; - - } else { - return /* false */0; - } - }; + return /* false */0; } } -function every2(a, b, p) { - return every2U(a, b, Curry.__2(p)); +function eq(a, b, p) { + return eqU(a, b, Curry.__2(p)); } function cmpU(a, b, p) { @@ -462,10 +517,6 @@ function cmp(a, b, p) { return cmpU(a, b, Curry.__2(p)); } -var eqU = every2U; - -var eq = every2; - exports.get = get; exports.getExn = getExn; exports.set = set; @@ -505,10 +556,14 @@ exports.reduceU = reduceU; exports.reduce = reduce; exports.reduceReverseU = reduceReverseU; exports.reduceReverse = reduceReverse; +exports.someU = someU; +exports.some = some; exports.everyU = everyU; exports.every = every; exports.every2U = every2U; exports.every2 = every2; +exports.some2U = some2U; +exports.some2 = some2; exports.cmpU = cmpU; exports.cmp = cmp; exports.eqU = eqU; diff --git a/lib/js/bs_List.js b/lib/js/bs_List.js index 0a39d8cb1d..c3b9bb1857 100644 --- a/lib/js/bs_List.js +++ b/lib/js/bs_List.js @@ -1452,12 +1452,12 @@ exports.someU = someU; exports.some = some; exports.every2U = every2U; exports.every2 = every2; +exports.some2U = some2U; +exports.some2 = some2; exports.cmpU = cmpU; exports.cmp = cmp; exports.eqU = eqU; exports.eq = eq; -exports.some2U = some2U; -exports.some2 = some2; exports.hasU = hasU; exports.has = has; exports.getByU = getByU;