Skip to content

Commit

Permalink
t comes first, and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzhang committed Dec 24, 2017
1 parent 435a03e commit 6977cca
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 214 deletions.
130 changes: 77 additions & 53 deletions jscomp/others/bs_List.ml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ let rec copyAuxCont cellX prec =
let rec copyAuxWitFilter f cellX prec =
match cellX with
| [] ->
unsafeMutateTail prec []
()
| h::t ->
if f h [@bs] then
begin
Expand Down Expand Up @@ -186,16 +186,16 @@ let rec copyAuxWithMap2 f cellX cellY prec =
unsafeMutateTail prec next ;
copyAuxWithMap2 f t1 t2 next
| [],_ | _,[] ->
unsafeMutateTail prec []
()

let rec copyAuxWithMapI f i cellX prec =
match cellX with
| [] ->
unsafeMutateTail prec []
| h::t ->
let next = mutableCell (f i h [@bs]) [] in
unsafeMutateTail prec next ;
copyAuxWithMapI f (i + 1) t next
| [] ->
()

let rec takeAux n cell prec =
if n = 0 then true
Expand Down Expand Up @@ -272,15 +272,17 @@ let map xs f =
let cell = mutableCell (f h [@bs]) [] in
copyAuxWithMap f t cell;
cell
let rec map2 f l1 l2 =

let map2 l1 l2 f =
match (l1, l2) with
| (a1::l1, a2::l2) ->
let cell = mutableCell (f a1 a2 [@bs]) [] in
copyAuxWithMap2 f l1 l2 cell;
cell
| [], _ | _, [] -> []

let rec mapi f = function
let rec mapi xs f =
match xs with
[] -> []
| h::t ->
let cell = mutableCell (f 0 h [@bs]) [] in
Expand Down Expand Up @@ -358,114 +360,136 @@ let rec mapRevAux f accu xs =
| [] -> accu
| a::l -> mapRevAux f (f a [@bs] :: accu) l

let mapRev f l =
let mapRev l f =
mapRevAux f [] l


let rec iter f = function
let rec iter xs f =
match xs with
[] -> ()
| a::l -> f a [@bs]; iter f l
| a::l -> f a [@bs]; iter l f

let rec iteri i f = function
let rec iteri xs i f =
match xs with
[] -> ()
| a::l -> f i a [@bs]; iteri (i + 1) f l
| a::l -> f i a [@bs]; iteri l (i + 1) f

let iteri f l = iteri 0 f l
let iteri l f = iteri l 0 f

let rec foldLeft f accu l =
let rec foldLeft l accu f =
match l with
[] -> accu
| a::l -> foldLeft f (f accu a [@bs]) l
| a::l -> foldLeft l (f accu a [@bs]) f

let rec foldRight f l accu =
let rec foldRight l accu f =
match l with
[] -> accu
| a::l -> f a (foldRight f l accu) [@bs]
| a::l -> f a (foldRight l accu f) [@bs]


let rec mapRevAux2 f accu l1 l2 =
match (l1, l2) with
| (a1::l1, a2::l2) -> mapRevAux2 f (f a1 a2 [@bs]:: accu) l1 l2
| (_, _) -> []

let mapRev2 f l1 l2 =
let mapRev2 l1 l2 f =
mapRevAux2 f [] l1 l2

let rec iter2 f l1 l2 =
let rec iter2 l1 l2 f =
match (l1, l2) with
| (a1::l1, a2::l2) -> f a1 a2 [@bs]; iter2 f l1 l2
| (a1::l1, a2::l2) -> f a1 a2 [@bs]; iter2 l1 l2 f
| [],_ | _, [] -> ()

let rec foldLeft2 f accu l1 l2 =
let rec foldLeft2 l1 l2 accu f =
match (l1, l2) with
| (a1::l1, a2::l2) -> foldLeft2 f (f accu a1 a2 [@bs]) l1 l2
| (a1::l1, a2::l2) ->
foldLeft2 l1 l2 (f accu a1 a2 [@bs]) f
| [], _ | _, [] -> accu

let rec foldRight2 f l1 l2 accu =
let rec foldRight2 l1 l2 accu f =
match (l1, l2) with
([], []) -> accu
| (a1::l1, a2::l2) -> f a1 a2 (foldRight2 f l1 l2 accu) [@bs]
| (a1::l1, a2::l2) -> f a1 a2 (foldRight2 l1 l2 accu f) [@bs]
| _, [] | [], _ -> accu

let rec forAll p = function
let rec forAll xs p =
match xs with
[] -> true
| a::l -> p a [@bs] && forAll p l
| a::l -> p a [@bs] && forAll l p

let rec exists p = function
let rec exists xs p =
match xs with
[] -> false
| a::l -> p a [@bs] || exists p l
| a::l -> p a [@bs] || exists l p

let rec forAll2 p l1 l2 =
let rec forAll2 l1 l2 p =
match (l1, l2) with
(_, []) | [],_ -> true
| (a1::l1, a2::l2) -> p a1 a2 [@bs] && forAll2 p l1 l2
| (a1::l1, a2::l2) -> p a1 a2 [@bs] && forAll2 l1 l2 p


let rec exists2 p l1 l2 =
let rec exists2 l1 l2 p =
match (l1, l2) with
[], _ | _, [] -> false
| (a1::l1, a2::l2) -> p a1 a2 [@bs] || exists2 p l1 l2
| (a1::l1, a2::l2) -> p a1 a2 [@bs] || exists2 l1 l2 p


let rec mem eq x = function
let rec mem xs x eq =
match xs with
[] -> false
| a::l -> eq a x [@bs] || mem eq x l
| a::l -> eq a x [@bs] || mem l x eq

let rec memq x = function
let rec memq xs x =
match xs with
[] -> false
| a::l -> a == x || memq x l
| a::l -> a == x || memq l x

let rec assocOpt eq x = function
[] -> None
| (a,b)::l -> if eq a x [@bs] then Some b else assocOpt eq x l
let rec assocOpt xs x eq =
match xs with
| [] -> None
| (a,b)::l ->
if eq a x [@bs] then Some b
else assocOpt l x eq

let rec assqOpt x = function
let rec assqOpt xs x =
match xs with
[] -> None
| (a,b)::l -> if a == x then Some b else assqOpt x l
| (a,b)::l -> if a == x then Some b else assqOpt l x

let rec memAssoc eq x = function
let rec memAssoc xs x eq =
match xs with
| [] -> false
| (a, b) :: l -> eq a x [@bs] || memAssoc eq x l
| (a, b) :: l -> eq a x [@bs] || memAssoc l x eq

let rec memAssq x = function
let rec memAssq xs x =
match xs with
| [] -> false
| (a, b) :: l -> a == x || memAssq x l
| (a, b) :: l -> a == x || memAssq l x

let rec removeAssoc eq x = function
(*FIXME non-stack-safe *)
let rec removeAssoc xs x eq =
match xs with
| [] -> []
| (a, b as pair) :: l ->
if eq a x [@bs] then l else pair :: removeAssoc eq x l
if eq a x [@bs] then l
else pair :: removeAssoc l x eq

let rec removeAssq x = function
let rec removeAssq xs x =
match xs with
| [] -> []
| (a, b as pair) :: l -> if a == x then l else pair :: removeAssq x l
| (a, b as pair) :: l ->
if a == x then l
else pair :: removeAssq l x

let rec findOpt p = function
let rec findOpt xs p =
match xs with
| [] -> None
| x :: l -> if p x [@bs] then Some x else findOpt p l
| x :: l ->
if p x [@bs] then Some x
else findOpt l p


let rec filter p xs =
let rec filter xs p =
match xs with
| [] -> []
| h::t ->
Expand All @@ -476,10 +500,10 @@ let rec filter p xs =
cell
end
else
filter p t
filter t p


let partition p l =
let partition l p =
match l with
| [] -> [],[]
| h::t ->
Expand Down
54 changes: 28 additions & 26 deletions jscomp/others/bs_List.mli
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ val append : 'a t -> 'a t -> 'a t

val map : 'a t -> ('a -> 'b [@bs]) -> 'b t

val map2 : ('a -> 'b -> 'c [@bs]) -> 'a t -> 'b t -> 'c t
val map2 : 'a t -> 'b t -> ('a -> 'b -> 'c [@bs]) -> 'c t

val mapi : (int -> 'a -> 'b [@bs]) -> 'a t -> 'b t
val mapi : 'a t -> (int -> 'a -> 'b [@bs]) -> 'b t

val init : int -> (int -> 'a [@bs]) -> 'a t

Expand All @@ -59,56 +59,58 @@ val rev : 'a t -> 'a t

val flatten : 'a t t -> 'a t

val mapRev : ('a -> 'b [@bs]) -> 'a t -> 'b t
val mapRev : 'a t -> ('a -> 'b [@bs]) -> 'b t

val iter : ('a -> 'b [@bs]) -> 'a t -> unit
val iter : 'a t -> ('a -> 'b [@bs]) -> unit

val iteri : (int -> 'a -> 'b [@bs]) -> 'a t -> unit
val iteri : 'a t -> (int -> 'a -> 'b [@bs]) -> unit

val foldLeft : ('a -> 'b -> 'a [@bs]) -> 'a -> 'b t -> 'a
val foldLeft : 'a t -> 'b -> ('b -> 'a -> 'b [@bs]) ->'b

val foldRight : ('a -> 'b -> 'b [@bs]) -> 'a t -> 'b -> 'b
val foldRight : 'a t -> 'b -> ('a -> 'b -> 'b [@bs]) -> 'b

val mapRev2 : ('a -> 'b -> 'c [@bs]) -> 'a t -> 'b t -> 'd t
val mapRev2 : 'a t -> 'b t -> ('a -> 'b -> 'c [@bs]) -> 'd t

val iter2 : ('a -> 'b -> 'c [@bs]) -> 'a t -> 'b t -> unit
val iter2 : 'a t -> 'b t -> ('a -> 'b -> 'c [@bs]) -> unit

val foldLeft2 :
('a -> 'b -> 'c -> 'a [@bs]) -> 'a -> 'b t -> 'c t -> 'a
'b t -> 'c t -> 'a -> ('a -> 'b -> 'c -> 'a [@bs]) -> 'a

val foldRight2 :
('a -> 'b -> 'c -> 'c [@bs]) -> 'a t -> 'b t -> 'c -> 'c
'a t -> 'b t -> 'c -> ('a -> 'b -> 'c -> 'c [@bs]) -> 'c

val forAll : ('a -> bool [@bs]) -> 'a t -> bool
val forAll : 'a t -> ('a -> bool [@bs]) -> bool

val exists : ('a -> bool [@bs]) -> 'a t -> bool
val exists : 'a t -> ('a -> bool [@bs]) -> bool

val forAll2 : ('a -> 'b -> bool [@bs]) -> 'a t -> 'b t -> bool
val forAll2 : 'a t -> 'b t -> ('a -> 'b -> bool [@bs]) -> bool

val exists2 : ('a -> 'b -> bool [@bs]) -> 'a t -> 'b t -> bool
val exists2 : 'a t -> 'b t -> ('a -> 'b -> bool [@bs]) -> bool

val mem : ('a -> 'b -> bool [@bs]) -> 'b -> 'a t -> bool
val mem : 'a t -> 'b -> ('a -> 'b -> bool [@bs]) -> bool

val memq : 'a -> 'a t -> bool
val memq : 'a t -> 'a ->bool

val assocOpt : ('a -> 'b -> bool [@bs]) -> 'b -> ('a * 'c) t -> 'c option
val assocOpt : ('a * 'c) t -> 'b -> ('a -> 'b -> bool [@bs]) -> 'c option

val assqOpt : 'a -> ('a * 'b) t -> 'b option
val assqOpt : ('a * 'b) t -> 'a -> 'b option

val memAssoc : ('a -> 'b -> bool [@bs]) -> 'b -> ('a * 'c) t -> bool
val memAssoc : ('a * 'c) t -> 'b -> ('a -> 'b -> bool [@bs]) -> bool

val memAssq : 'a -> ('a * 'b) t -> bool
val memAssq : ('a * 'b) t -> 'a -> bool

val removeAssoc :
('a -> 'b -> bool [@bs]) -> 'b -> ('a * 'c) t -> ('a * 'c) t
('a * 'c) t ->
'b ->
('a -> 'b -> bool [@bs]) -> ('a * 'c) t

val removeAssq : 'a -> ('a * 'b) t -> ('a * 'b) t
val removeAssq : ('a * 'b) t -> 'a -> ('a * 'b) t

val findOpt : ('a -> bool [@bs]) -> 'a t -> 'a option
val findOpt : 'a t -> ('a -> bool [@bs]) -> 'a option

val filter : ('a -> bool [@bs]) -> 'a t -> 'a t
val filter : 'a t -> ('a -> bool [@bs]) -> 'a t

val partition : ('a -> bool [@bs]) -> 'a t -> 'a t * 'a t
val partition : 'a t -> ('a -> bool [@bs]) -> 'a t * 'a t

val unzip : ('a * 'b) t -> 'a t * 'b t

Expand Down
Loading

0 comments on commit 6977cca

Please sign in to comment.