From 973062158ab55e8b119506bbe1a9aa2a694dbf11 Mon Sep 17 00:00:00 2001 From: Bikal Gurung Date: Wed, 28 Jun 2017 22:10:50 +0100 Subject: [PATCH 1/5] Implements safe version of List.split --- src/core/CCList.ml | 40 ++++++++++++++++++++++++++++++++++++++++ src/core/CCList.mli | 5 +++++ 2 files changed, 45 insertions(+) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index ef4254e0..9830afd4 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -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 + diff --git a/src/core/CCList.mli b/src/core/CCList.mli index 65da03b0..b2801713 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -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 *) From 7a9a741bb0f088d45310fe99417d641e9a2b52f5 Mon Sep 17 00:00:00 2001 From: Bikal Gurung Date: Wed, 28 Jun 2017 23:27:06 +0100 Subject: [PATCH 2/5] Adds tests for split function. --- src/core/CCList.ml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 9830afd4..0d79a23b 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -1393,3 +1393,18 @@ let split 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) +*) From 745c0cd78efb4baee422ef05e66f395437272c84 Mon Sep 17 00:00:00 2001 From: Bikal Gurung Date: Thu, 29 Jun 2017 18:13:39 +0100 Subject: [PATCH 3/5] Addresses reviewer comments --- src/core/CCList.ml | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 0d79a23b..c7b5baab 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -1357,30 +1357,18 @@ let pp ?(start="") ?(stop="") ?(sep=", ") pp_item fmt l = 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]) + | [ 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 - | 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, 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 @@ -1388,7 +1376,8 @@ let split l = | (x1, y1) :: l' -> let (x_acc, y_acc) = acc in let x_acc = x1 :: x_acc in - let y_acc = y1 :: y_acc in + let y_acc = y1 :: y_acc + in split_slow (x_acc, y_acc) l' in direct direct_depth_default_ l From eab5fbb36a96a4074721a2729b64f7848b551b46 Mon Sep 17 00:00:00 2001 From: Bikal Gurung Date: Thu, 29 Jun 2017 21:15:01 +0100 Subject: [PATCH 4/5] Addresses reviewer comments. --- src/core/CCList.ml | 86 ++++++++++++++++++++++----------------------- src/core/CCList.mli | 6 ++-- 2 files changed, 45 insertions(+), 47 deletions(-) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index c7b5baab..c0d24c98 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -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) -*) diff --git a/src/core/CCList.mli b/src/core/CCList.mli index b2801713..024a232e 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -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 *) From 2c92771ad939fac0ff6d38164df6c55cbb342c7d Mon Sep 17 00:00:00 2001 From: Bikal Gurung Date: Thu, 29 Jun 2017 21:19:48 +0100 Subject: [PATCH 5/5] Update authors page. --- AUTHORS.adoc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/AUTHORS.adoc b/AUTHORS.adoc index c4c632bc..561fa633 100644 --- a/AUTHORS.adoc +++ b/AUTHORS.adoc @@ -20,4 +20,5 @@ - David Sheets (@dsheets) - Glenn Slotte (glennsl) - @LemonBoy -- Leonid Rozenberg (@rleonid) \ No newline at end of file +- Leonid Rozenberg (@rleonid) +- Bikal Gurung (@bikalgurung) \ No newline at end of file