mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
Addresses reviewer comments.
This commit is contained in:
parent
745c0cd78e
commit
eab5fbb36a
2 changed files with 45 additions and 47 deletions
|
|
@ -387,6 +387,48 @@ let combine_gen l1 l2 =
|
|||
res1 = res2)
|
||||
*)
|
||||
|
||||
let split l =
|
||||
let rec direct i l = match l with
|
||||
| [] -> ([],[])
|
||||
| [x1, y1] -> [x1], [y1]
|
||||
| [x1, y1; x2, y2] -> [x1;x2], [y1;y2]
|
||||
| [x1, y1; x2, y2; x3, y3] -> [x1;x2;x3], [y1;y2;y3]
|
||||
| [x1, y1; x2, y2; x3, y3; x4, y4] -> [x1;x2;x3;x4], [y1;y2;y3;y4]
|
||||
| _ when i=0 -> split_slow ([], []) l
|
||||
| (x1, y1) ::
|
||||
(x2, y2) ::
|
||||
(x3, y3) ::
|
||||
(x4, y4) ::
|
||||
(x5, y5) :: l' ->
|
||||
let rx, ry = direct (i-1) l'
|
||||
in
|
||||
(x1 :: x2 :: x3 :: x4 :: x5 :: rx,
|
||||
y1 :: y2 :: y3 :: y4 :: y5 :: ry)
|
||||
and split_slow acc l = match l with
|
||||
| [] -> acc
|
||||
| (x1, y1) :: l' ->
|
||||
let acc = (x1 :: fst acc, y1 :: snd acc)
|
||||
in
|
||||
split_slow acc l'
|
||||
in
|
||||
direct direct_depth_default_ l
|
||||
|
||||
(*$Q
|
||||
(Q.(list (pair int string))) (fun l -> \
|
||||
let (l1, l2) = split l in \
|
||||
List.length l1 = List.length l \
|
||||
&& List.length l2 = List.length l)
|
||||
|
||||
(Q.(list (pair string int))) (fun l -> \
|
||||
let l = ("hello", 10) :: l in \
|
||||
let (l1, l2) = split l in \
|
||||
let i = Random.int @@ List.length l in \
|
||||
let l1_x = List.nth l1 i in \
|
||||
let l2_y = List.nth l2 i in \
|
||||
let (x,y) = List.nth l i in \
|
||||
l1_x = x && l2_y = y)
|
||||
*)
|
||||
|
||||
let return x = [x]
|
||||
|
||||
let (>>=) l f = flat_map f l
|
||||
|
|
@ -1353,47 +1395,3 @@ let pp ?(start="") ?(stop="") ?(sep=", ") pp_item fmt l =
|
|||
(CCFormat.hbox(CCList.pp ~start:"[" ~stop:"]" CCFormat.int)) \
|
||||
[1;2;3])
|
||||
*)
|
||||
|
||||
let split l =
|
||||
let rec direct i l = match l with
|
||||
| [] -> ([],[])
|
||||
| [ x1,y1 ] -> [x1], [y1]
|
||||
| [ x1, y1; x2, y2 ] -> [x1;x2], [y1;y2]
|
||||
| [ x1, y1; x2, y2 ; x3, y3 ] -> [x1;x2;x3], [y1;y2;y3]
|
||||
| [ x1, y1; x2, y2; x3, y3; x4, y4] -> [x1;x2;x3;x4], [y1;y2;y3;y4]
|
||||
| _ when i=0 -> split_slow ([], []) l
|
||||
| (x1, y1) ::
|
||||
(x2, y2) ::
|
||||
(x3, y3) ::
|
||||
(x4, y4) ::
|
||||
(x5, y5) :: l' ->
|
||||
let rx, ry = direct (i-1) l'
|
||||
in
|
||||
(x1 :: x2 :: x3 :: x4 :: x5 :: rx
|
||||
,y1 :: y2 :: y3 :: y4 :: y5 :: ry)
|
||||
and split_slow acc l = match l with
|
||||
| [] -> acc
|
||||
| (x1, y1) :: l' ->
|
||||
let (x_acc, y_acc) = acc in
|
||||
let x_acc = x1 :: x_acc in
|
||||
let y_acc = y1 :: y_acc
|
||||
in
|
||||
split_slow (x_acc, y_acc) l'
|
||||
in
|
||||
direct direct_depth_default_ l
|
||||
|
||||
(*$Q
|
||||
(Q.(list (pair int string))) (fun l -> \
|
||||
let (l1, l2) = split l in \
|
||||
List.length l1 = List.length l \
|
||||
&& List.length l2 = List.length l)
|
||||
|
||||
(Q.(list (pair string int))) (fun l -> \
|
||||
let l = ("hello", 10) :: l in \
|
||||
let (l1, l2) = split l in \
|
||||
let i = Random.int @@ List.length l in \
|
||||
let l1_x = List.nth l1 i in \
|
||||
let l2_y = List.nth l2 i in \
|
||||
let (x,y) = List.nth l i in \
|
||||
l1_x = x && l2_y = y)
|
||||
*)
|
||||
|
|
|
|||
|
|
@ -92,6 +92,9 @@ val combine_gen : 'a list -> 'b list -> ('a * 'b) gen
|
|||
instead, the output has as many pairs as the smallest input list.
|
||||
@since 1.2 *)
|
||||
|
||||
val split : ('a * 'b) t -> 'a t * 'b t
|
||||
(** A tail-recursive version of {!List.split}. *)
|
||||
|
||||
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
|
||||
|
||||
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
|
||||
|
|
@ -520,6 +523,3 @@ val pp : ?start:string -> ?stop:string -> ?sep:string ->
|
|||
'a printer -> 'a t printer
|
||||
|
||||
(** {2 Lists of pairs} *)
|
||||
|
||||
val split : ('a * 'b) t -> 'a t * 'b t
|
||||
(** Safe version of split *)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue