mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 11:45:31 -05:00
Implements safe version of List.split
This commit is contained in:
parent
adb9159007
commit
973062158a
2 changed files with 45 additions and 0 deletions
|
|
@ -1353,3 +1353,43 @@ 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])
|
||||
| [i1; i2] ->
|
||||
let (x1, y1) = i1 in
|
||||
let (x2, y2) = i2 in
|
||||
([x1;x2], [y1;y2])
|
||||
| [i1; i2; i3] ->
|
||||
let (x1, y1) = i1 in
|
||||
let (x2, y2) = i2 in
|
||||
let (x3, y3) = i3 in
|
||||
([x1;x2;x3], [y1;y2;y3])
|
||||
| [i1; i2; i3; i4] ->
|
||||
let (x1, y1) = i1 in
|
||||
let (x2, y2) = i2 in
|
||||
let (x3, y3) = i3 in
|
||||
let (x4, y4) = i4 in
|
||||
([x1;x2;x3;x4], [y1;y2;y3;y4])
|
||||
| _ when i=0 -> split_slow ([], []) l
|
||||
| i1 :: i2 :: i3 :: i4 :: i5 :: l' ->
|
||||
let (x1, y1) = i1 in
|
||||
let (x2, y2) = i2 in
|
||||
let (x3, y3) = i3 in
|
||||
let (x4, y4) = i4 in
|
||||
let (x5, y5) = i5 in
|
||||
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
|
||||
|
||||
|
|
|
|||
|
|
@ -518,3 +518,8 @@ end
|
|||
|
||||
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