From ece324f4c2f259a60acf96ce68687dfb59c47b95 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 13 Nov 2014 20:32:41 +0100 Subject: [PATCH] CCList.init added --- core/CCList.ml | 29 +++++++++++++++++++++++++++-- core/CCList.mli | 4 ++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/core/CCList.ml b/core/CCList.ml index 80d31608..06ab1500 100644 --- a/core/CCList.ml +++ b/core/CCList.ml @@ -63,7 +63,11 @@ let append l1 l2 = and safe l1 l2 = List.rev_append (List.rev l1) l2 in - direct direct_depth_append_ l1 l2 + match l1 with + | [] -> l2 + | [x] -> x::l2 + | [x;y] -> x::y::l2 + | _ -> direct direct_depth_append_ l1 l2 let (@) = append @@ -112,6 +116,21 @@ let fold_right f l acc = l = fold_right (fun x y->x::y) l []) *) +let init len f = + let rec init_rec acc i f = + if i=0 then f i :: acc + else init_rec (f i :: acc) (i-1) f + in + if len<0 then invalid_arg "init" + else if len=0 then [] + else init_rec [] (len-1) f + +(*$T + init 0 (fun _ -> 0) = [] + init 1 (fun x->x) = [0] + init 1000 (fun x->x) = 0--999 +*) + let rec compare f l1 l2 = match l1, l2 with | [], [] -> 0 | _, [] -> 1 @@ -125,6 +144,10 @@ let rec equal f l1 l2 = match l1, l2 with | [], _ | _, [] -> false | x1::l1', x2::l2' -> f x1 x2 && equal f l1' l2' +(*$T + equal CCInt.equal (1--1_000_000) (1--1_000_000) +*) + let flat_map f l = let rec aux f l kont = match l with | [] -> kont [] @@ -142,13 +165,15 @@ let flat_map f l = (*$T flat_map (fun x -> [x+1; x*2]) [10;100] = [11;20;101;200] + List.length (flat_map (fun x->[x]) (1--300_000)) = 300_000 *) let flatten l = fold_right append l [] (*$T flatten [[1]; [2;3;4]; []; []; [5;6]] = 1--6 - *) + flatten (init 300_001 (fun x->[x])) = 0--300_000 +*) let product f l1 l2 = flat_map (fun x -> map (fun y -> f x y) l2) l1 diff --git a/core/CCList.mli b/core/CCList.mli index 160348a5..f00c2af3 100644 --- a/core/CCList.mli +++ b/core/CCList.mli @@ -48,6 +48,10 @@ val filter : ('a -> bool) -> 'a t -> 'a t val fold_right : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b (** Safe version of [fold_right] *) +val init : int -> (int -> 'a) -> 'a t +(** Same as [Array.init] + @since NEXT_RELEASE *) + val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool