add labels to CCList in an incompatible way

This commit is contained in:
Simon Cruanes 2015-11-09 22:23:55 +01:00
parent 4b6ff6555b
commit e3b6102020
9 changed files with 219 additions and 195 deletions

View file

@ -111,7 +111,8 @@ let () =
let options = Arg.align
[ "-n", Arg.Set_int n, " size of the collection"
] in
Arg.parse options (CCList.Ref.push to_test) "usage: mem_measure [name*]";
Arg.parse options (CCList.Ref.push ~into:to_test)
"usage: mem_measure [name*]";
match !to_test with
| [] ->
print_list ();

View file

@ -21,7 +21,7 @@ let random_list =
let rec eq t1 t2 = match t1, t2 with
| Empty, Empty -> true
| Node(i1,l1), Node (i2,l2) -> i1=i2 && CCList.equal eq l1 l2
| Node(i1,l1), Node (i2,l2) -> i1=i2 && CCList.equal ~eq l1 l2
| Node _, _
| _, Node _ -> false

View file

@ -25,7 +25,7 @@ module L = struct
let ral = CCRAL.of_list l in
let map_naive () = ignore (try List.map f_ l with Stack_overflow -> [])
and map_tailrec () = ignore (List.rev (List.rev_map f_ l))
and ccmap () = ignore (CCList.map f_ l)
and ccmap () = ignore (CCList.map ~f:f_ l)
and ralmap () = ignore (CCRAL.map ~f:f_ ral)
in
B.throughputN time ~repeat
@ -44,10 +44,10 @@ module L = struct
let bench_flat_map ?(time=2) n =
let l = CCList.(1 -- n) in
let flatten_map_ l = List.flatten (CCList.map f_ l)
let flatten_map_ l = List.flatten (CCList.map ~f:f_ l)
and flatten_ccmap_ l = List.flatten (List.map f_ l) in
B.throughputN time ~repeat
[ "flat_map", CCList.flat_map f_, l
[ "flat_map", CCList.flat_map ~f:f_, l
; "flatten o CCList.map", flatten_ccmap_, l
; "flatten o map", flatten_map_, l
]
@ -73,11 +73,11 @@ module L = struct
let fold_right_append_ l =
List.fold_right List.append l []
and cc_fold_right_append_ l =
CCList.fold_right CCList.append l []
CCList.fold_right ~f:CCList.append l ~x:[]
in
let l =
CCList.Idx.mapi
(fun i x -> CCList.(x -- (x+ min i 100)))
~f:(fun i x -> CCList.(x -- (x+ min i 100)))
CCList.(1 -- n)
in
B.throughputN time ~repeat
@ -472,7 +472,7 @@ module Tbl = struct
let bench_add = bench_add_to modules_int
let bench_add_string_to l n =
let keys = CCList.( 1 -- n |> map (fun i->string_of_int i,i)) in
let keys = CCList.( 1 -- n |> map ~f:(fun i->string_of_int i,i)) in
let make (module T : STRING_MUT) =
let run() =
let t = T.create 50 in
@ -558,7 +558,7 @@ module Tbl = struct
let bench_find = bench_find_to modules_int_find
let bench_find_string_to l n =
let keys = CCList.( 1 -- n |> map (fun i->string_of_int i,i)) in
let keys = CCList.( 1 -- n |> map ~f:(fun i->string_of_int i,i)) in
let make (module T : STRING_MUT) =
let m = T.create n in
List.iter (fun (k,v) -> T.add m k v) keys;
@ -732,8 +732,14 @@ module Batch = struct
end)
module BenchList = Make(struct
include CCList
type 'a t = 'a list
let empty = []
let name = "list"
let (--) = CCList.(--)
let map f l = CCList.map ~f l
let filter_map f l = CCList.filter_map ~f l
let flat_map f l = CCList.flat_map ~f l
let filter f l = CCList.filter ~f l
let equal a b = a=b
let doubleton x y = [ x; y ]
let fold = List.fold_left

View file

@ -36,7 +36,7 @@ let is_empty = function
(* max depth for direct recursion *)
let direct_depth_default_ = 1000
let map f l =
let map ~f l =
let rec direct f i l = match l with
| [] -> []
| [x] -> [f x]
@ -58,7 +58,7 @@ let map f l =
List.rev (List.rev_map f l) = map f l)
*)
let (>|=) l f = map f l
let (>|=) l f = map ~f l
let direct_depth_append_ = 10_000
@ -96,7 +96,7 @@ let cons_maybe o l = match o with
let direct_depth_filter_ = 10_000
let filter p l =
let filter ~f l =
let rec direct i p l = match l with
| [] -> []
| _ when i=0 -> safe p l []
@ -107,7 +107,7 @@ let filter p l =
| x::l' when not (p x) -> safe p l' acc
| x::l' -> safe p l' (x::acc)
in
direct direct_depth_filter_ p l
direct direct_depth_filter_ f l
(*$= & ~printer:CCInt.to_string
500 (filter (fun x->x mod 2 = 0) (1 -- 1000) |> List.length)
@ -115,7 +115,7 @@ let filter p l =
500_000 (filter (fun x->x mod 2 = 0) (1 -- 1_000_000) |> List.length)
*)
let fold_right f l acc =
let fold_right ~f l ~x =
let rec direct i f l acc = match l with
| [] -> acc
| _ when i=0 -> safe f (List.rev l) acc
@ -128,30 +128,32 @@ let fold_right f l acc =
let acc = f x acc in
safe f l' acc
in
direct direct_depth_default_ f l acc
direct direct_depth_default_ f l x
(*$T
fold_right (+) (1 -- 1_000_000) 0 = \
fold_right ~f:(+) (1 -- 1_000_000) ~x:0 = \
List.fold_left (+) 0 (1 -- 1_000_000)
*)
(*$Q
(Q.list Q.small_int) (fun l -> \
l = fold_right (fun x y->x::y) l [])
l = fold_right ~f:(fun x y->x::y) l ~x:[])
*)
let rec fold_while f acc = function
| [] -> acc
| e::l -> let acc, cont = f acc e in
let rec fold_while ~f ~x = function
| [] -> x
| e::l -> let x, cont = f x e in
match cont with
| `Stop -> acc
| `Continue -> fold_while f acc l
| `Stop -> x
| `Continue -> fold_while ~f ~x l
(*$T
fold_while (fun acc b -> if b then acc+1, `Continue else acc, `Stop) 0 [true;true;false;true] = 2
fold_while \
~f:(fun acc b -> if b then acc+1, `Continue else acc, `Stop) \
~x:0 [true;true;false;true] = 2
*)
let fold_map f acc l =
let fold_map ~f ~x:acc l =
let rec aux f acc map_acc l = match l with
| [] -> acc, List.rev map_acc
| x :: l' ->
@ -170,7 +172,7 @@ let fold_map f acc l =
fold_map (fun acc x -> x::acc, x) [] l = (List.rev l, l))
*)
let fold_flat_map f acc l =
let fold_flat_map ~f ~x:acc l =
let rec aux f acc map_acc l = match l with
| [] -> acc, List.rev map_acc
| x :: l' ->
@ -206,24 +208,30 @@ let init len f =
init 1000 (fun x->x) = 0--999
*)
let rec compare f l1 l2 = match l1, l2 with
let compare ?(cmp=Pervasives.compare) l1 l2 =
let rec aux cmp l1 l2 = match l1, l2 with
| [], [] -> 0
| _, [] -> 1
| [], _ -> -1
| x1::l1', x2::l2' ->
let c = f x1 x2 in
if c <> 0 then c else compare f l1' l2'
let c = cmp x1 x2 in
if c <> 0 then c else aux cmp l1' l2'
in
aux cmp l1 l2
let rec equal f l1 l2 = match l1, l2 with
| [], [] -> true
| [], _ | _, [] -> false
| x1::l1', x2::l2' -> f x1 x2 && equal f l1' l2'
let equal ?(eq=(=)) l1 l2 =
let rec aux eq l1 l2 = match l1, l2 with
| [], [] -> true
| [], _ | _, [] -> false
| x1::l1', x2::l2' -> eq x1 x2 && aux eq l1' l2'
in
aux eq l1 l2
(*$T
equal CCInt.equal (1--1_000_000) (1--1_000_000)
equal ~eq:CCInt.equal (1--1_000_000) (1--1_000_000)
*)
let flat_map f l =
let flat_map ~f l =
let rec aux f l kont = match l with
| [] -> kont []
| x::l' ->
@ -243,23 +251,23 @@ let flat_map f l =
List.length (flat_map (fun x->[x]) (1--300_000)) = 300_000
*)
let flatten l = fold_right append l []
let flatten l = fold_right ~f:append l ~x:[]
(*$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
let product ~f l1 l2 =
flat_map ~f:(fun x -> map ~f:(fun y -> f x y) l2) l1
let fold_product f acc l1 l2 =
let fold_product ~f ~x l1 l2 =
List.fold_left
(fun acc x1 ->
List.fold_left
(fun acc x2 -> f acc x1 x2)
acc l2
) acc l1
) x l1
let diagonal l =
let rec gen acc l = match l with
@ -277,7 +285,7 @@ let diagonal l =
diagonal [1;2;3] |> List.sort Pervasives.compare = [1, 2; 1, 3; 2, 3]
*)
let partition_map f l =
let partition_map ~f l =
let rec iter f l1 l2 l = match l with
| [] -> List.rev l1, List.rev l2
| x :: tl ->
@ -302,13 +310,13 @@ let partition_map f l =
let return x = [x]
let (>>=) l f = flat_map f l
let (>>=) l f = flat_map ~f l
let (<$>) = map
let (<$>) f l = map ~f l
let pure = return
let (<*>) funs l = product (fun f x -> f x) funs l
let (<*>) funs l = product ~f:(fun f x -> f x) funs l
let sorted_merge ?(cmp=Pervasives.compare) l1 l2 =
let rec recurse cmp acc l1 l2 = match l1,l2 with
@ -338,7 +346,7 @@ let sort_uniq (type elt) ?(cmp=Pervasives.compare) l =
type t = elt
let compare = cmp
end) in
let set = fold_right S.add l S.empty in
let set = fold_right ~f:S.add l ~x:S.empty in
S.elements set
(*$T
@ -416,7 +424,7 @@ let sorted_merge_uniq ?(cmp=Pervasives.compare) l1 l2 =
uniq_succ l3 = l3)
*)
let take n l =
let take ~n l =
let rec direct i n l = match l with
| [] -> []
| _ when i=0 -> safe n [] l
@ -432,10 +440,10 @@ let take n l =
direct direct_depth_default_ n l
(*$T
take 2 [1;2;3;4;5] = [1;2]
take 10_000 (range 0 100_000) |> List.length = 10_000
take 10_000 (range 0 2_000) = range 0 2_000
take 300_000 (1 -- 400_000) = 1 -- 300_000
take ~n:2 [1;2;3;4;5] = [1;2]
take ~n:10_000 (range 0 100_000) |> List.length = 10_000
take ~n:10_000 (range 0 2_000) = range 0 2_000
take ~n:300_000 (1 -- 400_000) = 1 -- 300_000
*)
(*$Q
@ -445,12 +453,12 @@ let take n l =
List.length l1 <= i && ((List.length l1 = i) = (List.length l >= i)))
*)
let rec drop n l = match l with
let rec drop ~n l = match l with
| [] -> []
| _ when n=0 -> l
| _::l' -> drop (n-1) l'
| _::l' -> drop ~n:(n-1) l'
let take_drop n l = take n l, drop n l
let take_drop ~n l = take ~n l, drop ~n l
let split = take_drop
@ -461,7 +469,7 @@ let split = take_drop
l1 @ l2 = l )
*)
let take_while p l =
let take_while ~f l =
let rec direct i p l = match l with
| [] -> []
| _ when i=0 -> safe p [] l
@ -472,13 +480,13 @@ let take_while p l =
| x :: l' ->
if p x then safe p (x::acc) l' else List.rev acc
in
direct direct_depth_default_ p l
direct direct_depth_default_ f l
(*$T
take_while (fun x->x<10) (1 -- 20) = (1--9)
take_while (fun x->x <> 0) [0;1;2;3] = []
take_while (fun _ -> true) [] = []
take_while (fun _ -> true) (1--10) = (1--10)
take_while ~f:(fun x->x<10) (1 -- 20) = (1--9)
take_while ~f:(fun x->x <> 0) [0;1;2;3] = []
take_while ~f:(fun _ -> true) [] = []
take_while ~f:(fun _ -> true) (1--10) = (1--10)
*)
(*$Q
@ -487,25 +495,25 @@ let take_while p l =
List.for_all f l1)
*)
let rec drop_while p l = match l with
let rec drop_while ~f l = match l with
| [] -> []
| x :: l' -> if p x then drop_while p l' else l
| x :: l' -> if f x then drop_while ~f l' else l
(*$Q
Q.(pair (fun1 small_int bool) (list small_int)) (fun (f,l) -> \
take_while f l @ drop_while f l = l)
*)
let last n l =
let last ~n l =
let len = List.length l in
if len < n then l else drop (len-n) l
if len < n then l else drop ~n:(len-n) l
let rec find_pred p l = match l with
let rec find_pred ~f l = match l with
| [] -> None
| x :: _ when p x -> Some x
| _ :: tl -> find_pred p tl
| x :: _ when f x -> Some x
| _ :: tl -> find_pred ~f tl
let find_pred_exn p l = match find_pred p l with
let find_pred_exn ~f l = match find_pred ~f l with
| None -> raise Not_found
| Some x -> x
@ -516,7 +524,7 @@ let find_pred_exn p l = match find_pred p l with
find_pred (fun x -> x < 10) (1 -- 9) = Some 1
*)
let find_mapi f l =
let find_mapi ~f l =
let rec aux f i = function
| [] -> None
| x::l' ->
@ -525,16 +533,17 @@ let find_mapi f l =
| None -> aux f (i+1) l'
in aux f 0 l
let find_map f l = find_mapi (fun _ -> f) l
let find_map ~f l = find_mapi ~f:(fun _ -> f) l
let find = find_map
let findi = find_mapi
let find_idx p l = find_mapi (fun i x -> if p x then Some (i, x) else None) l
let find_idx ~f l =
find_mapi ~f:(fun i x -> if f x then Some (i, x) else None) l
(*$T
find (fun x -> if x=3 then Some "a" else None) [1;2;3;4] = Some "a"
find (fun x -> if x=3 then Some "a" else None) [1;2;4;5] = None
find ~f:(fun x -> if x=3 then Some "a" else None) [1;2;3;4] = Some "a"
find ~f:(fun x -> if x=3 then Some "a" else None) [1;2;4;5] = None
*)
let remove ?(eq=(=)) ~x l =
@ -550,7 +559,7 @@ let remove ?(eq=(=)) ~x l =
remove ~x:10 [1;2;3] = [1;2;3]
*)
let filter_map f l =
let filter_map ~f l =
let rec recurse acc l = match l with
| [] -> List.rev acc
| x::l' ->
@ -560,30 +569,30 @@ let filter_map f l =
(*$=
["2"; "4"] \
(filter_map (fun x -> if x mod 2 = 0 then Some (string_of_int x) else None) \
(filter_map ~f:(fun x -> if x mod 2 = 0 then Some (string_of_int x) else None) \
[1;2;3;4;5])
[ "2"; "4"; "6" ] \
(filter_map (fun x -> if x mod 2 = 0 then Some (string_of_int x) else None) \
(filter_map ~f:(fun x -> if x mod 2 = 0 then Some (string_of_int x) else None) \
[ 1; 2; 3; 4; 5; 6 ])
*)
module Set = struct
let mem ?(eq=(=)) x l =
let mem ?(eq=(=)) ~x l =
let rec search eq x l = match l with
| [] -> false
| y::l' -> eq x y || search eq x l'
in search eq x l
let add ?(eq=(=)) x l =
if mem ~eq x l then l else x::l
let add ?(eq=(=)) ~x l =
if mem ~eq ~x l then l else x::l
let remove ?(eq=(=)) x l =
let rec remove_one ~eq x acc l = match l with
let remove ?(eq=(=)) ~x l =
let rec remove_one eq x acc l = match l with
| [] -> assert false
| y :: tl when eq x y -> List.rev_append acc tl
| y :: tl -> remove_one ~eq x (y::acc) tl
| y :: tl -> remove_one eq x (y::acc) tl
in
if mem ~eq x l then remove_one ~eq x [] l else l
if mem ~eq ~x l then remove_one eq x [] l else l
(*$Q
Q.(pair int (list int)) (fun (x,l) -> \
@ -594,9 +603,9 @@ module Set = struct
not (Set.mem x l) || List.length (Set.remove x l) = List.length l - 1)
*)
let subset ?(eq=(=)) l1 l2 =
let subset ?(eq=(=)) l1 ~of_:l2 =
List.for_all
(fun t -> mem ~eq t l2)
(fun t -> mem ~eq ~x:t l2)
l1
let uniq ?(eq=(=)) l =
@ -613,7 +622,7 @@ module Set = struct
let union ?(eq=(=)) l1 l2 =
let rec union eq acc l1 l2 = match l1 with
| [] -> List.rev_append acc l2
| x::xs when mem ~eq x l2 -> union eq acc xs l2
| x::xs when mem ~eq ~x l2 -> union eq acc xs l2
| x::xs -> union eq (x::acc) xs l2
in union eq [] l1 l2
@ -624,7 +633,7 @@ module Set = struct
let inter ?(eq=(=)) l1 l2 =
let rec inter eq acc l1 l2 = match l1 with
| [] -> List.rev acc
| x::xs when mem ~eq x l2 -> inter eq (x::acc) xs l2
| x::xs when mem ~eq ~x l2 -> inter eq (x::acc) xs l2
| _::xs -> inter eq acc xs l2
in inter eq [] l1 l2
@ -634,40 +643,40 @@ module Set = struct
end
module Idx = struct
let mapi f l =
let mapi ~f l =
let r = ref 0 in
map
(fun x ->
map l
~f:(fun x ->
let y = f !r x in
incr r; y
) l
)
(*$T
Idx.mapi (fun i x -> i*x) [10;10;10] = [0;10;20]
*)
let iteri f l =
let iteri ~f l =
let rec aux f i l = match l with
| [] -> ()
| x::l' -> f i x; aux f (i+1) l'
in aux f 0 l
let foldi f acc l =
let foldi ~f ~x l =
let rec foldi f acc i l = match l with
| [] -> acc
| x::l' ->
let acc = f acc i x in
foldi f acc (i+1) l'
in
foldi f acc 0 l
foldi f x 0 l
let rec get_exn l i = match l with
let rec get_exn l ~k = match l with
| [] -> raise Not_found
| x::_ when i=0 -> x
| _::l' -> get_exn l' (i-1)
| x::_ when k=0 -> x
| _::l' -> get_exn l' ~k:(k-1)
let get l i =
try Some (get_exn l i)
let get l ~k =
try Some (get_exn l ~k)
with Not_found -> None
(*$T
@ -677,14 +686,14 @@ module Idx = struct
Idx.get [] 0 = None
*)
let set l0 i x =
let set l0 ~k ~v =
let rec aux l acc i = match l with
| [] -> l0
| _::l' when i=0 -> List.rev_append acc (x::l')
| _::l' when i=0 -> List.rev_append acc (v::l')
| y::l' ->
aux l' (y::acc) (i-1)
in
aux l0 [] i
aux l0 [] k
(*$T
Idx.set [1;2;3] 0 10 = [10;2;3]
@ -692,29 +701,29 @@ module Idx = struct
Idx.set [1;2;3] 1 10 = [1;10;3]
*)
let insert l i x =
let insert l ~k ~v =
let rec aux l acc i x = match l with
| [] -> List.rev_append acc [x]
| y::l' when i=0 -> List.rev_append acc (x::y::l')
| y::l' ->
aux l' (y::acc) (i-1) x
in
aux l [] i x
aux l [] k v
(*$T
Idx.insert [1;2;3] 0 10 = [10;1;2;3]
Idx.insert [1;2;3] 4 10 = [1;2;3;10]
Idx.insert [1;2;3] 1 10 = [1;10;2;3]
Idx.insert [1;2;3] ~k:0 ~v:10 = [10;1;2;3]
Idx.insert [1;2;3] ~k:4 ~v:10 = [1;2;3;10]
Idx.insert [1;2;3] ~k:1 ~v:10 = [1;10;2;3]
*)
let remove l0 i =
let remove l0 ~k =
let rec aux l acc i = match l with
| [] -> l0
| _::l' when i=0 -> List.rev_append acc l'
| y::l' ->
aux l' (y::acc) (i-1)
in
aux l0 [] i
aux l0 [] k
(*$T
Idx.remove [1;2;3;4] 0 = [2;3;4]
@ -755,48 +764,48 @@ let (--) = range
append (range 1000 501) (range 500 0) = range 1000 0
*)
let replicate i x =
let replicate ~n x =
let rec aux acc i =
if i = 0 then acc
else aux (x::acc) (i-1)
in aux [] i
in aux [] n
let repeat i l =
let repeat ~n l =
let l' = List.rev l in
let rec aux acc i =
if i = 0 then List.rev acc
else aux (List.rev_append l' acc) (i-1)
in aux [] i
in aux [] n
module Assoc = struct
type ('a, 'b) t = ('a*'b) list
let get_exn ?(eq=(=)) l x =
let get_exn ?(eq=(=)) l ~k =
let rec search eq l x = match l with
| [] -> raise Not_found
| (y,z)::l' ->
if eq x y then z else search eq l' x
in search eq l x
in search eq l k
let get ?eq l x =
try Some (get_exn ?eq l x)
let get ?eq l ~k =
try Some (get_exn ?eq l ~k)
with Not_found -> None
(*$T
Assoc.get [1, "1"; 2, "2"] 1 = Some "1"
Assoc.get [1, "1"; 2, "2"] 2 = Some "2"
Assoc.get [1, "1"; 2, "2"] 3 = None
Assoc.get [] 42 = None
Assoc.get [1, "1"; 2, "2"] ~k:1 = Some "1"
Assoc.get [1, "1"; 2, "2"] ~k:2 = Some "2"
Assoc.get [1, "1"; 2, "2"] ~k:3 = None
Assoc.get [] ~k:42 = None
*)
let set ?(eq=(=)) l x y =
let set ?(eq=(=)) l ~k ~v =
let rec search eq acc l x y = match l with
| [] -> (x,y)::acc
| (x',y')::l' ->
if eq x x'
then (x,y)::List.rev_append acc l'
else search eq ((x',y')::acc) l' x y
in search eq [] l x y
in search eq [] l k v
(*$T
Assoc.set [1,"1"; 2, "2"] 2 "two" |> List.sort Pervasives.compare \
@ -844,7 +853,7 @@ module Zipper = struct
| l, x::r -> x::l, r
| _, [] -> invalid_arg "zipper.right_exn"
let modify f z = match z with
let modify ~f z = match z with
| l, [] ->
begin match f None with
| None -> z
@ -868,7 +877,7 @@ module Zipper = struct
| _, x::_ -> x
| _, [] -> raise Not_found
let insert x (l,r) = l, x::r
let insert ~x (l,r) = l, x::r
let remove (l,r) = match r with
| [] -> l, []
@ -893,7 +902,7 @@ end
module Ref = struct
type 'a t = 'a list ref
let push l x = l := x :: !l
let push ~into:l x = l := x :: !l
let pop l = match !l with
| [] -> None
@ -911,13 +920,16 @@ module Ref = struct
let clear l = l := []
let lift f l = f !l
let lift ~f l = f !l
let push_list r l =
let push_list ~into:r l =
r := List.rev_append l !r
(*$T
let l = Ref.create() in Ref.push l 1; Ref.push_list l [2;3]; !l = [3;2;1]
let l = Ref.create() in \
Ref.push ~into:l 1; \
Ref.push_list ~into:l [2;3]; \
!l = [3;2;1]
*)
end
@ -931,7 +943,7 @@ end
module Traverse(M : MONAD) = struct
open M
let map_m f l =
let map_m ~f l =
let rec aux f acc l = match l with
| [] -> return (List.rev acc)
| x::tail ->
@ -939,23 +951,23 @@ module Traverse(M : MONAD) = struct
aux f (x' :: acc) tail
in aux f [] l
let rec map_m_par f l = match l with
let rec map_m_par ~f l = match l with
| [] -> M.return []
| x::tl ->
let x' = f x in
let tl' = map_m_par f tl in
let tl' = map_m_par ~f tl in
x' >>= fun x' ->
tl' >>= fun tl' ->
M.return (x'::tl')
let sequence_m l = map_m (fun x->x) l
let sequence_m l = map_m ~f:(fun x->x) l
let rec fold_m f acc l = match l with
let rec fold_m ~f ~x:acc l = match l with
| [] -> return acc
| x :: l' ->
f acc x
>>= fun acc' ->
fold_m f acc' l'
fold_m ~f ~x:acc' l'
end
(** {2 Conversions} *)
@ -990,7 +1002,7 @@ let random_choose l = match l with
let i = Random.State.int st len in
List.nth l i
let random_sequence l st = map (fun g -> g st) l
let random_sequence l st = map ~f:(fun g -> g st) l
let to_seq l k = List.iter k l
let of_seq seq =

View file

@ -33,7 +33,7 @@ val is_empty : _ t -> bool
(** [is_empty l] returns [true] iff [l = []]
@since 0.11 *)
val map : ('a -> 'b) -> 'a t -> 'b t
val map : f:('a -> 'b) -> 'a t -> 'b t
(** Safe version of map *)
val (>|=) : 'a t -> ('a -> 'b) -> 'b t
@ -54,23 +54,26 @@ val cons_maybe : 'a option -> 'a t -> 'a t
val (@) : 'a t -> 'a t -> 'a t
val filter : ('a -> bool) -> 'a t -> 'a t
val filter : f:('a -> bool) -> 'a t -> 'a t
(** Safe version of {!List.filter} *)
val fold_right : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b
val fold_right : f:('a -> 'b -> 'b) -> 'a t -> x:'b -> 'b
(** Safe version of [fold_right] *)
val fold_while : ('a -> 'b -> 'a * [`Stop | `Continue]) -> 'a -> 'b t -> 'a
val fold_while :
f:('a -> 'b -> 'a * [`Stop | `Continue]) -> x:'a -> 'b t -> 'a
(** Fold until a stop condition via [('a, `Stop)] is
indicated by the accumulator
@since 0.8 *)
val fold_map : ('acc -> 'a -> 'acc * 'b) -> 'acc -> 'a list -> 'acc * 'b list
val fold_map :
f:('acc -> 'a -> 'acc * 'b) -> x:'acc -> 'a list -> 'acc * 'b list
(** [fold_map f acc l] is a [fold_left]-like function, but it also maps the
list to another list.
@since 0.14 *)
val fold_flat_map : ('acc -> 'a -> 'acc * 'b list) -> 'acc -> 'a list -> 'acc * 'b list
val fold_flat_map :
f:('acc -> 'a -> 'acc * 'b list) -> x:'acc -> 'a list -> 'acc * 'b list
(** [fold_flat_map f acc l] is a [fold_left]-like function, but it also maps the
list to a list of lists that is then [flatten]'d..
@since 0.14 *)
@ -79,27 +82,28 @@ val init : int -> (int -> 'a) -> 'a t
(** Similar to {!Array.init}
@since 0.6 *)
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
val compare : ?cmp:('a -> 'a -> int) -> 'a t -> 'a t -> int
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
val equal : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t -> bool
val flat_map : ('a -> 'b t) -> 'a t -> 'b t
(** Map and flatten at the same time (safe). Evaluation order is not guaranteed. *)
val flat_map : f:('a -> 'b t) -> 'a t -> 'b t
(** Map and flatten at the same time (safe). Evaluation order
is not guaranteed. *)
val flatten : 'a t t -> 'a t
(** Safe flatten *)
val product : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
val product : f:('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
(** Cartesian product of the two lists, with the given combinator *)
val fold_product : ('c -> 'a -> 'b -> 'c) -> 'c -> 'a t -> 'b t -> 'c
val fold_product : f:('c -> 'a -> 'b -> 'c) -> x:'c -> 'a t -> 'b t -> 'c
(** Fold on the cartesian product *)
val diagonal : 'a t -> ('a * 'a) t
(** All pairs of distinct positions of the list. [list_diagonal l] will
return the list of [List.nth i l, List.nth j l] if [i < j]. *)
val partition_map : ('a -> [<`Left of 'b | `Right of 'c | `Drop]) ->
val partition_map : f:('a -> [<`Left of 'b | `Right of 'c | `Drop]) ->
'a list -> 'b list * 'c list
(** [partition_map f l] maps [f] on [l] and gather results in lists:
- if [f x = `Left y], adds [y] to the first list
@ -117,58 +121,58 @@ val return : 'a -> 'a t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
val take : int -> 'a t -> 'a t
val take : n:int -> 'a t -> 'a t
(** Take the [n] first elements, drop the rest *)
val drop : int -> 'a t -> 'a t
val drop : n:int -> 'a t -> 'a t
(** Drop the [n] first elements, keep the rest *)
val take_drop : int -> 'a t -> 'a t * 'a t
val take_drop : n:int -> 'a t -> 'a t * 'a t
(** [take_drop n l] returns [l1, l2] such that [l1 @ l2 = l] and
[length l1 = min (length l) n] *)
val take_while : ('a -> bool) -> 'a t -> 'a t
val take_while : f:('a -> bool) -> 'a t -> 'a t
(** @since 0.13 *)
val drop_while : ('a -> bool) -> 'a t -> 'a t
val drop_while : f:('a -> bool) -> 'a t -> 'a t
(** @since 0.13 *)
val split : int -> 'a t -> 'a t * 'a t
val split : n:int -> 'a t -> 'a t * 'a t
(** Synonym to {!take_drop}
@deprecated since 0.13: conflict with the {!List.split} standard function *)
val last : int -> 'a t -> 'a t
val last : n:int -> 'a t -> 'a t
(** [last n l] takes the last [n] elements of [l] (or less if
[l] doesn't have that many elements *)
val find_pred : ('a -> bool) -> 'a t -> 'a option
val find_pred : f:('a -> bool) -> 'a t -> 'a option
(** [find_pred p l] finds the first element of [l] that satisfies [p],
or returns [None] if no element satisfies [p]
@since 0.11 *)
val find_pred_exn : ('a -> bool) -> 'a t -> 'a
val find_pred_exn : f:('a -> bool) -> 'a t -> 'a
(** Unsafe version of {!find_pred}
@raise Not_found if no such element is found
@since 0.11 *)
val find_map : ('a -> 'b option) -> 'a t -> 'b option
val find_map : f:('a -> 'b option) -> 'a t -> 'b option
(** [find_map f l] traverses [l], applying [f] to each element. If for
some element [x], [f x = Some y], then [Some y] is returned. Otherwise
the call returns [None]
@since 0.11 *)
val find : ('a -> 'b option) -> 'a list -> 'b option
val find : f:('a -> 'b option) -> 'a list -> 'b option
(** @deprecated since 0.11 in favor of {!find_map}, for the name is too confusing *)
val find_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b option
val find_mapi : f:(int -> 'a -> 'b option) -> 'a t -> 'b option
(** Like {!find_map}, but also pass the index to the predicate function.
@since 0.11 *)
val findi : (int -> 'a -> 'b option) -> 'a t -> 'b option
val findi : f:(int -> 'a -> 'b option) -> 'a t -> 'b option
(** @deprecated since 0.11 in favor of {!find_mapi}, name is too confusing
@since 0.3.4 *)
val find_idx : ('a -> bool) -> 'a t -> (int * 'a) option
val find_idx : f:('a -> bool) -> 'a t -> (int * 'a) option
(** [find_idx p x] returns [Some (i,x)] where [x] is the [i]-th element of [l],
and [p x] holds. Otherwise returns [None] *)
@ -177,7 +181,7 @@ val remove : ?eq:('a -> 'a -> bool) -> x:'a -> 'a t -> 'a t
@param eq equality function
@since 0.11 *)
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
val filter_map : f:('a -> 'b option) -> 'a t -> 'b t
(** Map and remove elements at the same time *)
val sorted_merge : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
@ -206,28 +210,28 @@ val group_succ : ?eq:('a -> 'a -> bool) -> 'a list -> 'a list list
(** {2 Indices} *)
module Idx : sig
val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t
val mapi : f:(int -> 'a -> 'b) -> 'a t -> 'b t
val iteri : (int -> 'a -> unit) -> 'a t -> unit
val iteri : f:(int -> 'a -> unit) -> 'a t -> unit
val foldi : ('b -> int -> 'a -> 'b) -> 'b -> 'a t -> 'b
val foldi : f:('b -> int -> 'a -> 'b) -> x:'b -> 'a t -> 'b
(** Fold on list, with index *)
val get : 'a t -> int -> 'a option
val get : 'a t -> k:int -> 'a option
val get_exn : 'a t -> int -> 'a
val get_exn : 'a t -> k:int -> 'a
(** Get the i-th element, or
@raise Not_found if the index is invalid *)
val set : 'a t -> int -> 'a -> 'a t
(** Set i-th element (removes the old one), or does nothing if
val set : 'a t -> k:int -> v:'a -> 'a t
(** Set k-th element (removes the old one), or does nothing if
index is too high *)
val insert : 'a t -> int -> 'a -> 'a t
val insert : 'a t -> k:int -> v:'a -> 'a t
(** Insert at i-th position, between the two existing elements. If the
index is too high, append at the end of the list *)
val remove : 'a t -> int -> 'a t
val remove : 'a t -> k:int -> 'a t
(** Remove element at given index. Does nothing if the index is
too high. *)
end
@ -235,18 +239,18 @@ end
(** {2 Set Operators} *)
module Set : sig
val add : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t
val add : ?eq:('a -> 'a -> bool) -> x:'a -> 'a t -> 'a t
(** [add x set] adds [x] to [set] if it was not already present. Linear time.
@since 0.11 *)
val remove : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t
val remove : ?eq:('a -> 'a -> bool) -> x:'a -> 'a t -> 'a t
(** [remove x set] removes one occurrence of [x] from [set]. Linear time.
@since 0.11 *)
val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool
val mem : ?eq:('a -> 'a -> bool) -> x:'a -> 'a t -> bool
(** Membership to the list. Linear time *)
val subset : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t -> bool
val subset : ?eq:('a -> 'a -> bool) -> 'a t -> of_:'a t -> bool
(** Test for inclusion *)
val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t
@ -275,10 +279,10 @@ val range' : int -> int -> int t
val (--) : int -> int -> int t
(** Infix alias for [range] *)
val replicate : int -> 'a -> 'a t
val replicate : n:int -> 'a -> 'a t
(** Replicate the given element [n] times *)
val repeat : int -> 'a t -> 'a t
val repeat : n:int -> 'a t -> 'a t
(** Concatenate the list with itself [n] times *)
(** {2 Association Lists} *)
@ -286,14 +290,14 @@ val repeat : int -> 'a t -> 'a t
module Assoc : sig
type ('a, 'b) t = ('a*'b) list
val get : ?eq:('a->'a->bool) -> ('a,'b) t -> 'a -> 'b option
val get : ?eq:('a->'a->bool) -> ('a,'b) t -> k:'a -> 'b option
(** Find the element *)
val get_exn : ?eq:('a->'a->bool) -> ('a,'b) t -> 'a -> 'b
val get_exn : ?eq:('a->'a->bool) -> ('a,'b) t -> k:'a -> 'b
(** Same as [get]
@raise Not_found if the element is not present *)
val set : ?eq:('a->'a->bool) -> ('a,'b) t -> 'a -> 'b -> ('a,'b) t
val set : ?eq:('a->'a->bool) -> ('a,'b) t -> k:'a -> v:'b -> ('a,'b) t
(** Add the binding into the list (erase it if already present) *)
end
@ -343,11 +347,11 @@ module Zipper : sig
@raise Invalid_argument if the zipper is already at rightmost pos
@since 0.14 *)
val modify : ('a option -> 'a option) -> 'a t -> 'a t
val modify : f:('a option -> 'a option) -> 'a t -> 'a t
(** Modify the current element, if any, by returning a new element, or
returning [None] if the element is to be deleted *)
val insert : 'a -> 'a t -> 'a t
val insert : x:'a -> 'a t -> 'a t
(** Insert an element at the current position. If an element was focused,
[insert x l] adds [x] just before it, and focuses on [x]
@since 0.14 *)
@ -396,7 +400,7 @@ end
module Ref : sig
type 'a t = 'a list ref
val push : 'a t -> 'a -> unit
val push : into:'a t -> 'a -> unit
val pop : 'a t -> 'a option
@ -410,10 +414,10 @@ module Ref : sig
val clear : _ t -> unit
(** Remove all elements *)
val lift : ('a list -> 'b) -> 'a t -> 'b
val lift : f:('a list -> 'b) -> 'a t -> 'b
(** Apply a list function to the content *)
val push_list : 'a t -> 'a list -> unit
val push_list : into:'a t -> 'a list -> unit
(** Add elements of the list at the beginning of the list ref. Elements
at the end of the list will be at the beginning of the list ref *)
end
@ -428,11 +432,11 @@ end
module Traverse(M : MONAD) : sig
val sequence_m : 'a M.t t -> 'a t M.t
val fold_m : ('b -> 'a -> 'b M.t) -> 'b -> 'a t -> 'b M.t
val fold_m : f:('b -> 'a -> 'b M.t) -> x:'b -> 'a t -> 'b M.t
val map_m : ('a -> 'b M.t) -> 'a t -> 'b t M.t
val map_m : f:('a -> 'b M.t) -> 'a t -> 'b t M.t
val map_m_par : ('a -> 'b M.t) -> 'a t -> 'b t M.t
val map_m_par : f:('a -> 'b M.t) -> 'a t -> 'b t M.t
(** Same as {!map_m} but [map_m_par f (x::l)] evaluates [f x] and
[f l] "in parallel" before combining their result (for instance
in Lwt). *)

View file

@ -417,7 +417,7 @@ let compare ?(cmp=Pervasives.compare) a b =
(*$Q
Q.(pair (list int) (list int)) (fun (l1,l2) -> \
CCOrd.equiv (compare (of_list l1) (of_list l2)) \
(CCList.compare Pervasives.compare l1 l2))
(CCList.compare l1 l2))
*)
type 'a printer = Format.formatter -> 'a -> unit

View file

@ -478,7 +478,8 @@ let scc ?(tbl=mk_table 128) ~graph seq = SCC.explore ~tbl ~graph seq
(* example from https://en.wikipedia.org/wiki/Strongly_connected_component *)
(*$R
let set_eq ?(eq=(=)) l1 l2 = CCList.Set.subset ~eq l1 l2 && CCList.Set.subset ~eq l2 l1 in
let set_eq ?(eq=(=)) l1 l2 =
CCList.Set.subset ~eq l1 ~of_:l2 && CCList.Set.subset ~eq l2 ~of_:l1 in
let graph = of_list
[ "a", "b"
; "b", "e"

View file

@ -604,7 +604,7 @@ module Make(Key : KEY)
aux acc t
(*$T
let l = CCList.(1 -- 10 |> map (fun x->x,x)) in \
let l = CCList.(1 -- 10 |> map ~f:(fun x->x,x)) in \
M.of_list l \
|> M.fold ~f:(fun acc x y -> (x,y)::acc) ~x:[] \
|> List.sort Pervasives.compare = l
@ -720,7 +720,7 @@ module Make(Key : KEY)
end
(*$R
let m = M.of_list CCList.( (501 -- 1000) @ (500 -- 1) |> map (fun i->i,i)) in
let m = M.of_list CCList.( (501 -- 1000) @ (500 -- 1) |> map ~f:(fun i->i,i)) in
assert_equal ~printer:CCInt.to_string 1000 (M.cardinal m);
assert_bool "check all get"
(Sequence.for_all (fun i -> i = M.get_exn i m) Sequence.(1 -- 1000));

View file

@ -357,7 +357,7 @@ module MakeFull(K : KEY) : S with type key = K.t = struct
with Not_found -> None
(*$T
let m = CCList.(0 -- 1000 |> map (fun i->i,i) |> M.of_list) in \
let m = CCList.(0 -- 1000 |> map ~f:(fun i->i,i) |> M.of_list) in \
List.for_all (fun i -> M.nth_exn i m = (i,i)) CCList.(0--1000)
*)
@ -488,7 +488,7 @@ module MakeFull(K : KEY) : S with type key = K.t = struct
let m = M.merge (fun k -> CCOpt.map2 (+)) m1 m2 in
assert_bool "balanced" (M.balanced m);
assert_equal
~cmp:(CCList.equal (CCPair.equal CCInt.equal CCInt.equal))
~cmp:(CCList.equal ?eq:None)
~printer:CCFormat.(to_string (list (pair int int)))
[1, 2; 4, 8]
(M.to_list m |> List.sort Pervasives.compare)