mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
delete containers.iter and merge parts of it into containers-data
This commit is contained in:
parent
a2d07e4028
commit
46e40c9165
7 changed files with 0 additions and 827 deletions
|
|
@ -1,541 +0,0 @@
|
||||||
|
|
||||||
(* This file is free software, part of containers. See file "license" for more details. *)
|
|
||||||
|
|
||||||
(** {1 Continuation List} *)
|
|
||||||
|
|
||||||
type 'a sequence = ('a -> unit) -> unit
|
|
||||||
type 'a gen = unit -> 'a option
|
|
||||||
type 'a equal = 'a -> 'a -> bool
|
|
||||||
type 'a ord = 'a -> 'a -> int
|
|
||||||
type 'a printer = Format.formatter -> 'a -> unit
|
|
||||||
|
|
||||||
type + 'a t = unit ->
|
|
||||||
[ `Nil
|
|
||||||
| `Cons of 'a * 'a t
|
|
||||||
]
|
|
||||||
|
|
||||||
let nil () = `Nil
|
|
||||||
let cons a b () = `Cons (a,b)
|
|
||||||
let empty = nil
|
|
||||||
|
|
||||||
let singleton x () = `Cons (x, nil)
|
|
||||||
|
|
||||||
let rec _forever x () = `Cons (x, _forever x)
|
|
||||||
|
|
||||||
let rec _repeat n x () =
|
|
||||||
if n<=0 then `Nil else `Cons (x, _repeat (n-1) x)
|
|
||||||
|
|
||||||
let repeat ?n x = match n with
|
|
||||||
| None -> _forever x
|
|
||||||
| Some n -> _repeat n x
|
|
||||||
|
|
||||||
(*$T
|
|
||||||
repeat ~n:4 0 |> to_list = [0;0;0;0]
|
|
||||||
repeat ~n:0 1 |> to_list = []
|
|
||||||
repeat 1 |> take 20 |> to_list = (repeat ~n:20 1 |> to_list)
|
|
||||||
*)
|
|
||||||
|
|
||||||
let is_empty l = match l () with
|
|
||||||
| `Nil -> true
|
|
||||||
| `Cons _ -> false
|
|
||||||
|
|
||||||
let head_exn l = match l() with | `Nil -> raise Not_found | `Cons (x, _) -> x
|
|
||||||
let head l = match l() with `Nil -> None | `Cons (x, _) -> Some x
|
|
||||||
let tail_exn l = match l() with | `Nil -> raise Not_found | `Cons (_, l) -> l
|
|
||||||
let tail l = match l() with | `Nil -> None | `Cons (_, l) -> Some l
|
|
||||||
|
|
||||||
let rec equal eq l1 l2 = match l1(), l2() with
|
|
||||||
| `Nil, `Nil -> true
|
|
||||||
| `Nil, _
|
|
||||||
| _, `Nil -> false
|
|
||||||
| `Cons (x1,l1'), `Cons (x2,l2') ->
|
|
||||||
eq x1 x2 && equal eq l1' l2'
|
|
||||||
|
|
||||||
let rec compare cmp l1 l2 = match l1(), l2() with
|
|
||||||
| `Nil, `Nil -> 0
|
|
||||||
| `Nil, _ -> -1
|
|
||||||
| _, `Nil -> 1
|
|
||||||
| `Cons (x1,l1'), `Cons (x2,l2') ->
|
|
||||||
let c = cmp x1 x2 in
|
|
||||||
if c = 0 then compare cmp l1' l2' else c
|
|
||||||
|
|
||||||
let rec fold f acc res = match res () with
|
|
||||||
| `Nil -> acc
|
|
||||||
| `Cons (s, cont) -> fold f (f acc s) cont
|
|
||||||
|
|
||||||
let rec iter f l = match l () with
|
|
||||||
| `Nil -> ()
|
|
||||||
| `Cons (x, l') -> f x; iter f l'
|
|
||||||
|
|
||||||
let iteri f l =
|
|
||||||
let rec aux f l i = match l() with
|
|
||||||
| `Nil -> ()
|
|
||||||
| `Cons (x, l') ->
|
|
||||||
f i x;
|
|
||||||
aux f l' (i+1)
|
|
||||||
in
|
|
||||||
aux f l 0
|
|
||||||
|
|
||||||
let length l = fold (fun acc _ -> acc+1) 0 l
|
|
||||||
|
|
||||||
let rec take n (l:'a t) () =
|
|
||||||
if n=0 then `Nil
|
|
||||||
else match l () with
|
|
||||||
| `Nil -> `Nil
|
|
||||||
| `Cons (x,l') -> `Cons (x, take (n-1) l')
|
|
||||||
|
|
||||||
let rec take_while p l () = match l () with
|
|
||||||
| `Nil -> `Nil
|
|
||||||
| `Cons (x,l') ->
|
|
||||||
if p x then `Cons (x, take_while p l') else `Nil
|
|
||||||
|
|
||||||
(*$T
|
|
||||||
of_list [1;2;3;4] |> take_while (fun x->x < 4) |> to_list = [1;2;3]
|
|
||||||
*)
|
|
||||||
|
|
||||||
let rec drop n (l:'a t) () = match l () with
|
|
||||||
| l' when n=0 -> l'
|
|
||||||
| `Nil -> `Nil
|
|
||||||
| `Cons (_,l') -> drop (n-1) l' ()
|
|
||||||
|
|
||||||
let rec drop_while p l () = match l() with
|
|
||||||
| `Nil -> `Nil
|
|
||||||
| `Cons (x,l') when p x -> drop_while p l' ()
|
|
||||||
| `Cons _ as res -> res
|
|
||||||
|
|
||||||
(*$Q
|
|
||||||
(Q.pair (Q.list Q.small_int) Q.small_int) (fun (l,n) -> \
|
|
||||||
let s = of_list l in let s1, s2 = take n s, drop n s in \
|
|
||||||
append s1 s2 |> to_list = l )
|
|
||||||
*)
|
|
||||||
|
|
||||||
let rec map f l () = match l () with
|
|
||||||
| `Nil -> `Nil
|
|
||||||
| `Cons (x, l') -> `Cons (f x, map f l')
|
|
||||||
|
|
||||||
(*$T
|
|
||||||
(map ((+) 1) (1 -- 5) |> to_list) = (2 -- 6 |> to_list)
|
|
||||||
*)
|
|
||||||
|
|
||||||
let mapi f l =
|
|
||||||
let rec aux f l i () = match l() with
|
|
||||||
| `Nil -> `Nil
|
|
||||||
| `Cons (x, tl) ->
|
|
||||||
`Cons (f i x, aux f tl (i+1))
|
|
||||||
in
|
|
||||||
aux f l 0
|
|
||||||
|
|
||||||
(*$T
|
|
||||||
mapi (fun i x -> i,x) (1 -- 3) |> to_list = [0, 1; 1, 2; 2, 3]
|
|
||||||
*)
|
|
||||||
|
|
||||||
let rec fmap f (l:'a t) () = match l() with
|
|
||||||
| `Nil -> `Nil
|
|
||||||
| `Cons (x, l') ->
|
|
||||||
begin match f x with
|
|
||||||
| None -> fmap f l' ()
|
|
||||||
| Some y -> `Cons (y, fmap f l')
|
|
||||||
end
|
|
||||||
|
|
||||||
(*$T
|
|
||||||
fmap (fun x -> if x mod 2=0 then Some (x*3) else None) (1--10) |> to_list \
|
|
||||||
= [6;12;18;24;30]
|
|
||||||
*)
|
|
||||||
|
|
||||||
let rec filter p l () = match l () with
|
|
||||||
| `Nil -> `Nil
|
|
||||||
| `Cons (x, l') ->
|
|
||||||
if p x
|
|
||||||
then `Cons (x, filter p l')
|
|
||||||
else filter p l' ()
|
|
||||||
|
|
||||||
let rec append l1 l2 () = match l1 () with
|
|
||||||
| `Nil -> l2 ()
|
|
||||||
| `Cons (x, l1') -> `Cons (x, append l1' l2)
|
|
||||||
|
|
||||||
let rec cycle l () = append l (cycle l) ()
|
|
||||||
|
|
||||||
(*$T
|
|
||||||
cycle (of_list [1;2]) |> take 5 |> to_list = [1;2;1;2;1]
|
|
||||||
cycle (of_list [1; ~-1]) |> take 100_000 |> fold (+) 0 = 0
|
|
||||||
*)
|
|
||||||
|
|
||||||
let rec unfold f acc () = match f acc with
|
|
||||||
| None -> `Nil
|
|
||||||
| Some (x, acc') -> `Cons (x, unfold f acc')
|
|
||||||
|
|
||||||
(*$T
|
|
||||||
let f = function 10 -> None | x -> Some (x, x+1) in \
|
|
||||||
unfold f 0 |> to_list = [0;1;2;3;4;5;6;7;8;9]
|
|
||||||
*)
|
|
||||||
|
|
||||||
let rec flat_map f l () = match l () with
|
|
||||||
| `Nil -> `Nil
|
|
||||||
| `Cons (x, l') ->
|
|
||||||
_flat_map_app f (f x) l' ()
|
|
||||||
and _flat_map_app f l l' () = match l () with
|
|
||||||
| `Nil -> flat_map f l' ()
|
|
||||||
| `Cons (x, tl) ->
|
|
||||||
`Cons (x, _flat_map_app f tl l')
|
|
||||||
|
|
||||||
let product_with f l1 l2 =
|
|
||||||
let rec _next_left h1 tl1 h2 tl2 () =
|
|
||||||
match tl1() with
|
|
||||||
| `Nil -> _next_right ~die:true h1 tl1 h2 tl2 ()
|
|
||||||
| `Cons (x, tl1') ->
|
|
||||||
_map_list_left x h2
|
|
||||||
(_next_right ~die:false (x::h1) tl1' h2 tl2)
|
|
||||||
()
|
|
||||||
and _next_right ~die h1 tl1 h2 tl2 () =
|
|
||||||
match tl2() with
|
|
||||||
| `Nil when die -> `Nil
|
|
||||||
| `Nil -> _next_left h1 tl1 h2 tl2 ()
|
|
||||||
| `Cons (y, tl2') ->
|
|
||||||
_map_list_right h1 y
|
|
||||||
(_next_left h1 tl1 (y::h2) tl2')
|
|
||||||
()
|
|
||||||
and _map_list_left x l kont () = match l with
|
|
||||||
| [] -> kont()
|
|
||||||
| y::l' -> `Cons (f x y, _map_list_left x l' kont)
|
|
||||||
and _map_list_right l y kont () = match l with
|
|
||||||
| [] -> kont()
|
|
||||||
| x::l' -> `Cons (f x y, _map_list_right l' y kont)
|
|
||||||
in
|
|
||||||
_next_left [] l1 [] l2
|
|
||||||
|
|
||||||
let product l1 l2 =
|
|
||||||
product_with (fun x y -> x,y) l1 l2
|
|
||||||
|
|
||||||
let rec group eq l () = match l() with
|
|
||||||
| `Nil -> `Nil
|
|
||||||
| `Cons (x, l') ->
|
|
||||||
`Cons (cons x (take_while (eq x) l'), group eq (drop_while (eq x) l'))
|
|
||||||
|
|
||||||
(*$T
|
|
||||||
of_list [1;1;1;2;2;3;3;1] |> group (=) |> map to_list |> to_list = \
|
|
||||||
[[1;1;1]; [2;2]; [3;3]; [1]]
|
|
||||||
*)
|
|
||||||
|
|
||||||
let rec _uniq eq prev l () = match prev, l() with
|
|
||||||
| _, `Nil -> `Nil
|
|
||||||
| None, `Cons (x, l') ->
|
|
||||||
`Cons (x, _uniq eq (Some x) l')
|
|
||||||
| Some y, `Cons (x, l') ->
|
|
||||||
if eq x y
|
|
||||||
then _uniq eq prev l' ()
|
|
||||||
else `Cons (x, _uniq eq (Some x) l')
|
|
||||||
|
|
||||||
let uniq eq l = _uniq eq None l
|
|
||||||
|
|
||||||
let rec filter_map f l () = match l() with
|
|
||||||
| `Nil -> `Nil
|
|
||||||
| `Cons (x, l') ->
|
|
||||||
begin match f x with
|
|
||||||
| None -> filter_map f l' ()
|
|
||||||
| Some y -> `Cons (y, filter_map f l')
|
|
||||||
end
|
|
||||||
|
|
||||||
let flatten l = flat_map (fun x->x) l
|
|
||||||
|
|
||||||
let range i j =
|
|
||||||
let rec aux i j () =
|
|
||||||
if i=j then `Cons(i, nil)
|
|
||||||
else if i<j then `Cons (i, aux (i+1) j)
|
|
||||||
else `Cons (i, aux (i-1) j)
|
|
||||||
in aux i j
|
|
||||||
|
|
||||||
(*$T
|
|
||||||
range 0 5 |> to_list = [0;1;2;3;4;5]
|
|
||||||
range 0 0 |> to_list = [0]
|
|
||||||
range 5 2 |> to_list = [5;4;3;2]
|
|
||||||
*)
|
|
||||||
|
|
||||||
let (--) = range
|
|
||||||
|
|
||||||
let (--^) i j =
|
|
||||||
if i=j then empty
|
|
||||||
else if i<j then range i (j-1)
|
|
||||||
else range i (j+1)
|
|
||||||
|
|
||||||
(*$T
|
|
||||||
1 --^ 5 |> to_list = [1;2;3;4]
|
|
||||||
5 --^ 1 |> to_list = [5;4;3;2]
|
|
||||||
1 --^ 2 |> to_list = [1]
|
|
||||||
0 --^ 0 |> to_list = []
|
|
||||||
*)
|
|
||||||
|
|
||||||
let rec fold2 f acc l1 l2 = match l1(), l2() with
|
|
||||||
| `Nil, _
|
|
||||||
| _, `Nil -> acc
|
|
||||||
| `Cons(x1,l1'), `Cons(x2,l2') ->
|
|
||||||
fold2 f (f acc x1 x2) l1' l2'
|
|
||||||
|
|
||||||
let rec map2 f l1 l2 () = match l1(), l2() with
|
|
||||||
| `Nil, _
|
|
||||||
| _, `Nil -> `Nil
|
|
||||||
| `Cons(x1,l1'), `Cons(x2,l2') ->
|
|
||||||
`Cons (f x1 x2, map2 f l1' l2')
|
|
||||||
|
|
||||||
let rec iter2 f l1 l2 = match l1(), l2() with
|
|
||||||
| `Nil, _
|
|
||||||
| _, `Nil -> ()
|
|
||||||
| `Cons(x1,l1'), `Cons(x2,l2') ->
|
|
||||||
f x1 x2; iter2 f l1' l2'
|
|
||||||
|
|
||||||
let rec for_all2 f l1 l2 = match l1(), l2() with
|
|
||||||
| `Nil, _
|
|
||||||
| _, `Nil -> true
|
|
||||||
| `Cons(x1,l1'), `Cons(x2,l2') ->
|
|
||||||
f x1 x2 && for_all2 f l1' l2'
|
|
||||||
|
|
||||||
let rec exists2 f l1 l2 = match l1(), l2() with
|
|
||||||
| `Nil, _
|
|
||||||
| _, `Nil -> false
|
|
||||||
| `Cons(x1,l1'), `Cons(x2,l2') ->
|
|
||||||
f x1 x2 || exists2 f l1' l2'
|
|
||||||
|
|
||||||
let rec merge cmp l1 l2 () = match l1(), l2() with
|
|
||||||
| `Nil, tl2 -> tl2
|
|
||||||
| tl1, `Nil -> tl1
|
|
||||||
| `Cons(x1,l1'), `Cons(x2,l2') ->
|
|
||||||
if cmp x1 x2 < 0
|
|
||||||
then `Cons (x1, merge cmp l1' l2)
|
|
||||||
else `Cons (x2, merge cmp l1 l2')
|
|
||||||
|
|
||||||
let rec zip a b () = match a(), b() with
|
|
||||||
| `Nil, _
|
|
||||||
| _, `Nil -> `Nil
|
|
||||||
| `Cons (x, a'), `Cons (y, b') -> `Cons ((x,y), zip a' b')
|
|
||||||
|
|
||||||
let unzip l =
|
|
||||||
let rec first l () = match l() with
|
|
||||||
| `Nil -> `Nil
|
|
||||||
| `Cons ((x,_), tl) -> `Cons (x, first tl)
|
|
||||||
and second l () = match l() with
|
|
||||||
| `Nil -> `Nil
|
|
||||||
| `Cons ((_, y), tl) -> `Cons (y, second tl)
|
|
||||||
in
|
|
||||||
first l, second l
|
|
||||||
|
|
||||||
(*$Q
|
|
||||||
Q.(list (pair int int)) (fun l -> \
|
|
||||||
let l = CCKList.of_list l in let a, b = unzip l in equal (=) l (zip a b))
|
|
||||||
*)
|
|
||||||
|
|
||||||
(** {2 Implementations} *)
|
|
||||||
|
|
||||||
let return x () = `Cons (x, nil)
|
|
||||||
let pure = return
|
|
||||||
let (>>=) xs f = flat_map f xs
|
|
||||||
let (>|=) xs f = map f xs
|
|
||||||
|
|
||||||
let (<*>) fs xs = product_with (fun f x -> f x) fs xs
|
|
||||||
|
|
||||||
(** {2 Conversions} *)
|
|
||||||
|
|
||||||
let rec _to_rev_list acc l = match l() with
|
|
||||||
| `Nil -> acc
|
|
||||||
| `Cons (x,l') -> _to_rev_list (x::acc) l'
|
|
||||||
|
|
||||||
let to_rev_list l = _to_rev_list [] l
|
|
||||||
|
|
||||||
let to_list l =
|
|
||||||
let rec direct i (l:'a t) = match l () with
|
|
||||||
| `Nil -> []
|
|
||||||
| _ when i=0 -> List.rev (_to_rev_list [] l)
|
|
||||||
| `Cons (x, f) -> x :: direct (i-1) f
|
|
||||||
in
|
|
||||||
direct 200 l
|
|
||||||
|
|
||||||
let of_list l =
|
|
||||||
let rec aux l () = match l with
|
|
||||||
| [] -> `Nil
|
|
||||||
| x::l' -> `Cons (x, aux l')
|
|
||||||
in aux l
|
|
||||||
|
|
||||||
let of_array a =
|
|
||||||
let rec aux a i () =
|
|
||||||
if i=Array.length a then `Nil
|
|
||||||
else `Cons (a.(i), aux a (i+1))
|
|
||||||
in
|
|
||||||
aux a 0
|
|
||||||
|
|
||||||
let to_array l =
|
|
||||||
match l() with
|
|
||||||
| `Nil -> [| |]
|
|
||||||
| `Cons (x, _) ->
|
|
||||||
let n = length l in
|
|
||||||
let a = Array.make n x in (* need first elem to create [a] *)
|
|
||||||
iteri
|
|
||||||
(fun i x -> a.(i) <- x)
|
|
||||||
l;
|
|
||||||
a
|
|
||||||
|
|
||||||
(*$Q
|
|
||||||
Q.(array int) (fun a -> of_array a |> to_array = a)
|
|
||||||
*)
|
|
||||||
|
|
||||||
(*$T
|
|
||||||
of_array [| 1; 2; 3 |] |> to_list = [1;2;3]
|
|
||||||
of_list [1;2;3] |> to_array = [| 1; 2; 3; |]
|
|
||||||
*)
|
|
||||||
|
|
||||||
let rec to_seq res k = match res () with
|
|
||||||
| `Nil -> ()
|
|
||||||
| `Cons (s, f) -> k s; to_seq f k
|
|
||||||
|
|
||||||
let to_gen l =
|
|
||||||
let l = ref l in
|
|
||||||
fun () ->
|
|
||||||
match !l () with
|
|
||||||
| `Nil -> None
|
|
||||||
| `Cons (x,l') ->
|
|
||||||
l := l';
|
|
||||||
Some x
|
|
||||||
|
|
||||||
type 'a of_gen_state =
|
|
||||||
| Of_gen_thunk of 'a gen
|
|
||||||
| Of_gen_saved of [`Nil | `Cons of 'a * 'a t]
|
|
||||||
|
|
||||||
let of_gen g =
|
|
||||||
let rec consume r () = match !r with
|
|
||||||
| Of_gen_saved cons -> cons
|
|
||||||
| Of_gen_thunk g ->
|
|
||||||
begin match g() with
|
|
||||||
| None ->
|
|
||||||
r := Of_gen_saved `Nil;
|
|
||||||
`Nil
|
|
||||||
| Some x ->
|
|
||||||
let tl = consume (ref (Of_gen_thunk g)) in
|
|
||||||
let l = `Cons (x, tl) in
|
|
||||||
r := Of_gen_saved l;
|
|
||||||
l
|
|
||||||
end
|
|
||||||
in
|
|
||||||
consume (ref (Of_gen_thunk g))
|
|
||||||
|
|
||||||
(*$R
|
|
||||||
let g = let n = ref 0 in fun () -> Some (incr n; !n) in
|
|
||||||
let l = of_gen g in
|
|
||||||
assert_equal [1;2;3;4;5;6;7;8;9;10] (take 10 l |> to_list);
|
|
||||||
assert_equal [1;2;3;4;5;6;7;8;9;10] (take 10 l |> to_list);
|
|
||||||
assert_equal [11;12] (drop 10 l |> take 2 |> to_list);
|
|
||||||
*)
|
|
||||||
|
|
||||||
let sort ~cmp l =
|
|
||||||
let l = to_list l in
|
|
||||||
of_list (List.sort cmp l)
|
|
||||||
|
|
||||||
let sort_uniq ~cmp l =
|
|
||||||
let l = to_list l in
|
|
||||||
uniq (fun x y -> cmp x y = 0) (of_list (List.sort cmp l))
|
|
||||||
|
|
||||||
type 'a memoize =
|
|
||||||
| MemoThunk
|
|
||||||
| MemoSave of [`Nil | `Cons of 'a * 'a t]
|
|
||||||
|
|
||||||
let rec memoize f =
|
|
||||||
let r = ref MemoThunk in
|
|
||||||
fun () -> match !r with
|
|
||||||
| MemoSave l -> l
|
|
||||||
| MemoThunk ->
|
|
||||||
let l = match f() with
|
|
||||||
| `Nil -> `Nil
|
|
||||||
| `Cons (x, tail) -> `Cons (x, memoize tail)
|
|
||||||
in
|
|
||||||
r := MemoSave l;
|
|
||||||
l
|
|
||||||
|
|
||||||
(*$R
|
|
||||||
let printer = Q.Print.(list int) in
|
|
||||||
let gen () =
|
|
||||||
let rec l = let r = ref 0 in fun () -> incr r; `Cons (!r, l) in l
|
|
||||||
in
|
|
||||||
let l1 = gen () in
|
|
||||||
assert_equal ~printer [1;2;3;4] (take 4 l1 |> to_list);
|
|
||||||
assert_equal ~printer [5;6;7;8] (take 4 l1 |> to_list);
|
|
||||||
let l2 = gen () |> memoize in
|
|
||||||
assert_equal ~printer [1;2;3;4] (take 4 l2 |> to_list);
|
|
||||||
assert_equal ~printer [1;2;3;4] (take 4 l2 |> to_list);
|
|
||||||
*)
|
|
||||||
|
|
||||||
|
|
||||||
(** {2 Fair Combinations} *)
|
|
||||||
|
|
||||||
let rec interleave a b () = match a() with
|
|
||||||
| `Nil -> b ()
|
|
||||||
| `Cons (x, tail) -> `Cons (x, interleave b tail)
|
|
||||||
|
|
||||||
let rec fair_flat_map f a () = match a() with
|
|
||||||
| `Nil -> `Nil
|
|
||||||
| `Cons (x, tail) ->
|
|
||||||
let y = f x in
|
|
||||||
interleave y (fair_flat_map f tail) ()
|
|
||||||
|
|
||||||
let rec fair_app f a () = match f() with
|
|
||||||
| `Nil -> `Nil
|
|
||||||
| `Cons (f1, fs) ->
|
|
||||||
interleave (map f1 a) (fair_app fs a) ()
|
|
||||||
|
|
||||||
let (>>-) a f = fair_flat_map f a
|
|
||||||
let (<.>) f a = fair_app f a
|
|
||||||
|
|
||||||
(*$T
|
|
||||||
interleave (of_list [1;3;5]) (of_list [2;4;6]) |> to_list = [1;2;3;4;5;6]
|
|
||||||
fair_app (of_list [(+)1; ( * ) 3]) (of_list [1; 10]) \
|
|
||||||
|> to_list |> List.sort Stdlib.compare = [2; 3; 11; 30]
|
|
||||||
*)
|
|
||||||
|
|
||||||
(** {2 Infix} *)
|
|
||||||
|
|
||||||
module Infix = struct
|
|
||||||
let (>>=) = (>>=)
|
|
||||||
let (>|=) = (>|=)
|
|
||||||
let (<*>) = (<*>)
|
|
||||||
let (>>-) = (>>-)
|
|
||||||
let (<.>) = (<.>)
|
|
||||||
let (--) = (--)
|
|
||||||
let (--^) = (--^)
|
|
||||||
end
|
|
||||||
|
|
||||||
(** {2 Monadic Operations} *)
|
|
||||||
module type MONAD = sig
|
|
||||||
type 'a t
|
|
||||||
val return : 'a -> 'a t
|
|
||||||
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
|
|
||||||
end
|
|
||||||
|
|
||||||
module Traverse(M : MONAD) = struct
|
|
||||||
open M
|
|
||||||
|
|
||||||
let map_m f l =
|
|
||||||
let rec aux acc l = match l () with
|
|
||||||
| `Nil -> return (of_list (List.rev acc))
|
|
||||||
| `Cons (x,l') ->
|
|
||||||
f x >>= fun x' ->
|
|
||||||
aux (x' :: acc) l'
|
|
||||||
in
|
|
||||||
aux [] l
|
|
||||||
|
|
||||||
let sequence_m l = map_m (fun x->x) l
|
|
||||||
|
|
||||||
let rec fold_m f acc l = match l() with
|
|
||||||
| `Nil -> return acc
|
|
||||||
| `Cons (x,l') ->
|
|
||||||
f acc x >>= fun acc' -> fold_m f acc' l'
|
|
||||||
end
|
|
||||||
|
|
||||||
(** {2 IO} *)
|
|
||||||
|
|
||||||
let pp ?(sep=",") pp_item fmt l =
|
|
||||||
let rec pp fmt l = match l() with
|
|
||||||
| `Nil -> ()
|
|
||||||
| `Cons (x,l') ->
|
|
||||||
Format.pp_print_string fmt sep;
|
|
||||||
Format.pp_print_cut fmt ();
|
|
||||||
pp_item fmt x;
|
|
||||||
pp fmt l'
|
|
||||||
in
|
|
||||||
match l() with
|
|
||||||
| `Nil -> ()
|
|
||||||
| `Cons (x,l') -> pp_item fmt x; pp fmt l'
|
|
||||||
|
|
@ -1,279 +0,0 @@
|
||||||
|
|
||||||
(* This file is free software, part of containers. See file "license" for more details. *)
|
|
||||||
|
|
||||||
(** {1 Continuation List}
|
|
||||||
|
|
||||||
|
|
||||||
@deprecated since 2.7, you should use the standard {b Seq} instead.
|
|
||||||
See {{: https://github.com/c-cube/oseq/} oseq} for similar combinators.
|
|
||||||
|
|
||||||
*)
|
|
||||||
|
|
||||||
[@@@ocaml.deprecated "use Seq instead"]
|
|
||||||
|
|
||||||
type 'a sequence = ('a -> unit) -> unit
|
|
||||||
type 'a gen = unit -> 'a option
|
|
||||||
type 'a equal = 'a -> 'a -> bool
|
|
||||||
type 'a ord = 'a -> 'a -> int
|
|
||||||
type 'a printer = Format.formatter -> 'a -> unit
|
|
||||||
|
|
||||||
(** {2 Basics} *)
|
|
||||||
|
|
||||||
type + 'a t = unit ->
|
|
||||||
[ `Nil
|
|
||||||
| `Cons of 'a * 'a t
|
|
||||||
]
|
|
||||||
|
|
||||||
val nil : 'a t
|
|
||||||
|
|
||||||
val empty : 'a t
|
|
||||||
|
|
||||||
val cons : 'a -> 'a t -> 'a t
|
|
||||||
|
|
||||||
val singleton : 'a -> 'a t
|
|
||||||
|
|
||||||
val repeat : ?n:int -> 'a -> 'a t
|
|
||||||
(** [repeat ~n x] repeats [x] [n] times then stops. If [n] is omitted,
|
|
||||||
then [x] is repeated forever.
|
|
||||||
@since 0.3.3 *)
|
|
||||||
|
|
||||||
val cycle : 'a t -> 'a t
|
|
||||||
(** Cycle through the iterator infinitely. The iterator shouldn't be empty.
|
|
||||||
@since 0.3.3 *)
|
|
||||||
|
|
||||||
val unfold : ('b -> ('a * 'b) option) -> 'b -> 'a t
|
|
||||||
(** [unfold f acc] calls [f acc] and:
|
|
||||||
- if [f acc = Some (x, acc')], yield [x], continue with [unfold f acc'].
|
|
||||||
- if [f acc = None], stops.
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
val is_empty : 'a t -> bool
|
|
||||||
|
|
||||||
val head : 'a t -> 'a option
|
|
||||||
(** Head of the list.
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
val head_exn : 'a t -> 'a
|
|
||||||
(** Unsafe version of {!head}.
|
|
||||||
@raise Not_found if the list is empty.
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
val tail : 'a t -> 'a t option
|
|
||||||
(** Tail of the list.
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
val tail_exn : 'a t -> 'a t
|
|
||||||
(** Unsafe version of {!tail}.
|
|
||||||
@raise Not_found if the list is empty.
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
val equal : 'a equal -> 'a t equal
|
|
||||||
(** Equality step by step. Eager. *)
|
|
||||||
|
|
||||||
val compare : 'a ord -> 'a t ord
|
|
||||||
(** Lexicographic comparison. Eager. *)
|
|
||||||
|
|
||||||
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
|
|
||||||
(** Fold on values. *)
|
|
||||||
|
|
||||||
val iter : ('a -> unit) -> 'a t -> unit
|
|
||||||
|
|
||||||
val iteri : (int -> 'a -> unit) -> 'a t -> unit
|
|
||||||
(** Iterate with index (starts at 0).
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
val length : _ t -> int
|
|
||||||
(** Number of elements in the list.
|
|
||||||
Will not terminate if the list if infinite:
|
|
||||||
use (for instance) {!take} to make the list finite if necessary. *)
|
|
||||||
|
|
||||||
val take : int -> 'a t -> 'a t
|
|
||||||
|
|
||||||
val take_while : ('a -> bool) -> 'a t -> 'a t
|
|
||||||
|
|
||||||
val drop : int -> 'a t -> 'a t
|
|
||||||
|
|
||||||
val drop_while : ('a -> bool) -> 'a t -> 'a t
|
|
||||||
|
|
||||||
val map : ('a -> 'b) -> 'a t -> 'b t
|
|
||||||
|
|
||||||
val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t
|
|
||||||
(** Map with index (starts at 0).
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
val fmap : ('a -> 'b option) -> 'a t -> 'b t
|
|
||||||
|
|
||||||
val filter : ('a -> bool) -> 'a t -> 'a t
|
|
||||||
|
|
||||||
val append : 'a t -> 'a t -> 'a t
|
|
||||||
|
|
||||||
val product_with : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
|
|
||||||
(** Fair product of two (possibly infinite) lists into a new list. Lazy.
|
|
||||||
The first parameter is used to combine each pair of elements.
|
|
||||||
@since 0.3.3 *)
|
|
||||||
|
|
||||||
val product : 'a t -> 'b t -> ('a * 'b) t
|
|
||||||
(** Specialization of {!product_with} producing tuples.
|
|
||||||
@since 0.3.3 *)
|
|
||||||
|
|
||||||
val group : 'a equal -> 'a t -> 'a t t
|
|
||||||
(** [group eq l] groups together consecutive elements that satisfy [eq]. Lazy.
|
|
||||||
For instance [group (=) [1;1;1;2;2;3;3;1]] yields
|
|
||||||
[[1;1;1]; [2;2]; [3;3]; [1]].
|
|
||||||
@since 0.3.3 *)
|
|
||||||
|
|
||||||
val uniq : 'a equal -> 'a t -> 'a t
|
|
||||||
(** [uniq eq l] returns [l] but removes consecutive duplicates. Lazy.
|
|
||||||
In other words, if several values that are equal follow one another,
|
|
||||||
only the first of them is kept.
|
|
||||||
@since 0.3.3 *)
|
|
||||||
|
|
||||||
val flat_map : ('a -> 'b t) -> 'a t -> 'b t
|
|
||||||
|
|
||||||
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
|
|
||||||
|
|
||||||
val flatten : 'a t t -> 'a t
|
|
||||||
|
|
||||||
val range : int -> int -> int t
|
|
||||||
|
|
||||||
val (--) : int -> int -> int t
|
|
||||||
(** [a -- b] is the range of integers containing
|
|
||||||
[a] and [b] (therefore, never empty). *)
|
|
||||||
|
|
||||||
val (--^) : int -> int -> int t
|
|
||||||
(** [a -- b] is the integer range from [a] to [b], where [b] is excluded.
|
|
||||||
@since 0.17 *)
|
|
||||||
|
|
||||||
(** {2 Operations on two Collections} *)
|
|
||||||
|
|
||||||
val fold2 : ('acc -> 'a -> 'b -> 'acc) -> 'acc -> 'a t -> 'b t -> 'acc
|
|
||||||
(** Fold on two collections at once. Stop at soon as one of them ends. *)
|
|
||||||
|
|
||||||
val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
|
|
||||||
(** Map on two collections at once. Stop as soon as one of the
|
|
||||||
arguments is exhausted. *)
|
|
||||||
|
|
||||||
val iter2 : ('a -> 'b -> unit) -> 'a t -> 'b t -> unit
|
|
||||||
(** Iterate on two collections at once. Stop as soon as one of them ends. *)
|
|
||||||
|
|
||||||
val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
|
|
||||||
val exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
|
|
||||||
|
|
||||||
val merge : 'a ord -> 'a t -> 'a t -> 'a t
|
|
||||||
(** Merge two sorted iterators into a sorted iterator. *)
|
|
||||||
|
|
||||||
val zip : 'a t -> 'b t -> ('a * 'b) t
|
|
||||||
(** Combine elements pairwise. Stop as soon as one of the lists stops.
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
val unzip : ('a * 'b) t -> 'a t * 'b t
|
|
||||||
(** Split each tuple in the list.
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
(** {2 Misc} *)
|
|
||||||
|
|
||||||
val sort : cmp:'a ord -> 'a t -> 'a t
|
|
||||||
(** Eager sort. Require the iterator to be finite. [O(n ln(n))] time
|
|
||||||
and space.
|
|
||||||
@since 0.3.3 *)
|
|
||||||
|
|
||||||
val sort_uniq : cmp:'a ord -> 'a t -> 'a t
|
|
||||||
(** Eager sort that removes duplicate values. Require the iterator to be
|
|
||||||
finite. [O(n ln(n))] time and space.
|
|
||||||
@since 0.3.3 *)
|
|
||||||
|
|
||||||
val memoize : 'a t -> 'a t
|
|
||||||
(** Avoid recomputations by caching intermediate results.
|
|
||||||
@since 0.14 *)
|
|
||||||
|
|
||||||
(** {2 Fair Combinations} *)
|
|
||||||
|
|
||||||
val interleave : 'a t -> 'a t -> 'a t
|
|
||||||
(** Fair interleaving of both streams.
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
val fair_flat_map : ('a -> 'b t) -> 'a t -> 'b t
|
|
||||||
(** Fair version of {!flat_map}.
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
val fair_app : ('a -> 'b) t -> 'a t -> 'b t
|
|
||||||
(** Fair version of {!(<*>)}.
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
(** {2 Implementations}
|
|
||||||
@since 0.3.3 *)
|
|
||||||
|
|
||||||
val return : 'a -> 'a t
|
|
||||||
val pure : 'a -> 'a t
|
|
||||||
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
|
|
||||||
val (>|=) : 'a t -> ('a -> 'b) -> 'b t
|
|
||||||
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
|
|
||||||
|
|
||||||
val (>>-) : 'a t -> ('a -> 'b t) -> 'b t
|
|
||||||
(** Infix version of {! fair_flat_map}.
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
val (<.>) : ('a -> 'b) t -> 'a t -> 'b t
|
|
||||||
(** Infix version of {!fair_app}.
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
(** {2 Infix operators}
|
|
||||||
|
|
||||||
@since 0.17 *)
|
|
||||||
|
|
||||||
module Infix : sig
|
|
||||||
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
|
|
||||||
val (>|=) : 'a t -> ('a -> 'b) -> 'b t
|
|
||||||
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
|
|
||||||
val (>>-) : 'a t -> ('a -> 'b t) -> 'b t
|
|
||||||
val (<.>) : ('a -> 'b) t -> 'a t -> 'b t
|
|
||||||
val (--) : int -> int -> int t
|
|
||||||
val (--^) : int -> int -> int t
|
|
||||||
end
|
|
||||||
|
|
||||||
(** {2 Monadic Operations} *)
|
|
||||||
module type MONAD = sig
|
|
||||||
type 'a t
|
|
||||||
val return : 'a -> 'a t
|
|
||||||
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
|
|
||||||
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 map_m : ('a -> 'b M.t) -> 'a t -> 'b t M.t
|
|
||||||
end
|
|
||||||
|
|
||||||
(** {2 Conversions} *)
|
|
||||||
|
|
||||||
val of_list : 'a list -> 'a t
|
|
||||||
|
|
||||||
val to_list : 'a t -> 'a list
|
|
||||||
(** Gather all values into a list. *)
|
|
||||||
|
|
||||||
val of_array : 'a array -> 'a t
|
|
||||||
(** Iterate on the array.
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
val to_array : 'a t -> 'a array
|
|
||||||
(** Convert into array. Iterate twice.
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
val to_rev_list : 'a t -> 'a list
|
|
||||||
(** Convert to a list, in reverse order. More efficient than {!to_list}. *)
|
|
||||||
|
|
||||||
val to_seq : 'a t -> 'a sequence
|
|
||||||
|
|
||||||
val to_gen : 'a t -> 'a gen
|
|
||||||
|
|
||||||
val of_gen : 'a gen -> 'a t
|
|
||||||
(** [of_gen g] consumes the generator and caches intermediate results.
|
|
||||||
@since 0.13 *)
|
|
||||||
|
|
||||||
(** {2 IO} *)
|
|
||||||
|
|
||||||
val pp : ?sep:string -> 'a printer -> 'a t printer
|
|
||||||
(** Print the list with the given separator (default ",").
|
|
||||||
Do not print opening/closing delimiters. *)
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
|
|
||||||
(library
|
|
||||||
(name containers_iter)
|
|
||||||
(public_name containers.iter)
|
|
||||||
(wrapped false)
|
|
||||||
(flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string)
|
|
||||||
(ocamlopt_flags :standard (:include ../flambda.flags)))
|
|
||||||
Loading…
Add table
Reference in a new issue