oseq-0.5.1/0000755000175000017500000000000014702443054011161 5ustar kylekyleoseq-0.5.1/src/0000755000175000017500000000000014534102210011735 5ustar kylekyleoseq-0.5.1/src/OSeq.ml0000644000175000017500000006250414534102210013145 0ustar kylekyleopen Seq type 'a seq = 'a Seq.t (* alias *) type 'a iter = ('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 let empty () = Nil let is_empty l = match l () with | Nil -> true | Cons _ -> false let return x () = Cons (x, empty) let cons a b () = Cons (a, b) let head_exn g = match g () with | Cons (x, _) -> x | Nil -> invalid_arg "OSeq.head_exn" let tail_exn g : _ t = match g () with | Cons (_, l) -> l | Nil -> invalid_arg "OSeq.tail_exn" let rec ( -- ) i j () = if i = j then Cons (i, empty) else if i < j then Cons (i, i + 1 -- j) else Cons (i, i - 1 -- j) let ( --^ ) i j = if i = j then empty else if i < j then i -- (j - 1) else i -- (j + 1) let rec map f l () = match l () with | Nil -> Nil | Cons (x, tail) -> Cons (f x, map f tail) let rec fold_map f acc l () = match l () with | Nil -> Nil | Cons (x, tl) -> let acc = f acc x in Cons (acc, fold_map f acc tl) let rec repeatedly f () = Cons (f (), repeatedly f) let rec repeat x () = Cons (x, repeat x) let init n f = let rec aux r () = if r >= n then Nil else ( let x = f r in Cons (x, aux (r + 1)) ) in aux 0 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 let rec filter_map f (l : 'a t) () = match l () with | Nil -> Nil | Cons (x, l') -> (match f x with | None -> filter_map f l' () | Some y -> Cons (y, filter_map f l')) let filter f l = let rec aux f l () = match l () with | Nil -> Nil | Cons (x, tl) when f x -> Cons (x, aux f tl) | Cons (_, tl) -> aux f tl () in aux f l let rec append a b () = match a () with | Nil -> b () | Cons (x, tl) -> Cons (x, append tl b) let rec cycle l () = append l (cycle l) () let iterate f x = let rec aux f x () = let y = f x in Cons (x, aux f y) in aux f x let rec fold f acc l = match l () with | Nil -> acc | Cons (x, tl) -> fold f (f acc x) tl let fold_left = fold let foldi f acc l = let rec foldi f i acc l = match l () with | Nil -> acc | Cons (x, tl) -> foldi f (succ i) (f i acc x) tl in foldi f 0 acc l let reduce f g = match g () with | Nil -> invalid_arg "reduce" | Cons (x, tl) -> fold f x tl 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 unfold f acc () = match f acc with | None -> Nil | Some (x, acc') -> Cons (x, unfold f acc') let rec flat_map f l () = match l () with | Nil -> Nil | Cons (x, tl) -> fm_app_ f (f x) tl () and fm_app_ f l l' () = match l () with | Nil -> flat_map f l' () | Cons (x, tl) -> Cons (x, fm_app_ f tl l') let take_nth n g = let rec aux i g () = match g () with | Nil -> Nil | Cons (_, tl) when i > 0 -> aux (i - 1) tl () | Cons (x, tl) -> assert (i = 0); Cons (x, aux (n - 1) tl) in aux 0 g let rec nth i l = match l () with | Nil -> raise Not_found | Cons (x, _) when i = 0 -> x | Cons (_, tl) -> nth (i - 1) tl let mem eq x gen = let rec mem eq x gen = match gen () with | Nil -> false | Cons (y, tl) -> eq x y || mem eq x tl in mem eq x gen let rec for_all p gen = match gen () with | Nil -> true | Cons (x, tl) -> p x && for_all p tl let rec exists p gen = match gen () with | Nil -> false | Cons (x, tl) -> p x || exists p tl let min ~lt gen = match gen () with | Cons (x, tl) -> fold (fun min x -> if lt x min then x else min) x tl | Nil -> invalid_arg "min" let max ~lt gen = match gen () with | Cons (x, tl) -> fold (fun max x -> if lt max x then x else max) x tl | Nil -> invalid_arg "max" let equal eq gen1 gen2 = let rec check gen1 gen2 = match gen1 (), gen2 () with | Nil, Nil -> true | Cons (x1, tl1), Cons (x2, tl2) when eq x1 x2 -> check tl1 tl2 | _ -> false in check gen1 gen2 (* [partition p l] returns the elements that satisfy [p], and the elements that do not satisfy [p] *) let partition p gen = filter p gen, filter (fun x -> not (p x)) gen let zip_index gen = let rec aux r gen () = match gen () with | Nil -> Nil | Cons (x, tl) -> Cons ((r, x), aux (r + 1) tl) in aux 0 gen 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 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 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 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 let compare cmp gen1 gen2 : int = let rec aux gen1 gen2 = match gen1 (), gen2 () with | Nil, Nil -> 0 | Cons (x1, tl1), Cons (x2, tl2) -> let c = cmp x1 x2 in if c <> 0 then c else aux tl1 tl2 | Cons _, Nil -> 1 | Nil, Cons _ -> -1 in aux gen1 gen2 let rec find p e = match e () with | Nil -> None | Cons (x, _) when p x -> Some x | Cons (_, tl) -> find p tl let rec find_map f e = match e () with | Nil -> None | Cons (x, tl) -> (match f x with | None -> find_map f tl | Some _ as res -> res) let sum e = fold ( + ) 0 e (** {2 Fair Combinations} *) let rec interleave a b () = match a () with | Nil -> b () | Cons (x, tail) -> Cons (x, interleave b tail) let rec flat_map_interleave f a () = match a () with | Nil -> Nil | Cons (x, tail) -> let y = f x in interleave y (flat_map_interleave f tail) () let rec app_interleave f a () = match f () with | Nil -> Nil | Cons (f1, fs) -> interleave (map f1 a) (app_interleave fs a) () let rec flatten l () = match l () with | Nil -> Nil | Cons (x, tl) -> flat_app_ x tl () and flat_app_ l l' () = match l () with | Nil -> flatten l' () | Cons (x, tl) -> Cons (x, flat_app_ tl 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 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 let rec fold_while f acc gen = match gen () with | Nil -> acc | Cons (x, tl) -> let acc, cont = f acc x in (match cont with | `Stop -> acc | `Continue -> fold_while f acc tl) let scan f acc g : _ t = let rec aux f acc g () = match g () with | Nil -> Cons (acc, empty) | Cons (x, tl) -> let acc' = f acc x in Cons (acc, aux f acc' tl) in aux f acc g let unfold_scan f acc g = let rec aux f acc g () = match g () with | Nil -> Nil | Cons (x, tl) -> let acc, res = f acc x in Cons (res, aux f acc tl) in aux f acc g let product_with f l1 l2 = (* take next element from [l1] *) let rec loop l1 () = match l1 () with | Nil -> Nil | Cons (x1, tl1) -> let seq = interleave (map (fun x2 -> f x1 x2) l2) (loop tl1) in seq () in loop l1 let product l1 l2 = product_with (fun x y -> x, y) l1 l2 let app fs xs = product_with (fun f x -> f x) fs xs module Infix = struct let[@inline] ( >>= ) xs f = flat_map f xs let[@inline] ( >|= ) xs f = map f xs let[@inline] ( >>| ) xs f = map f xs let ( <*> ) = app let ( -- ) = ( -- ) let ( --^ ) = ( --^ ) let[@inline] ( let+ ) x f = map f x let[@inline] ( let* ) x f = flat_map f x let ( and+ ) = product let ( and* ) = product end include Infix let product3 l1 l2 l3 = (fun x1 x2 x3 -> x1, x2, x3) |> return <*> l1 <*> l2 <*> l3 let product4 l1 l2 l3 l4 = (fun x1 x2 x3 x4 -> x1, x2, x3, x4) |> return <*> l1 <*> l2 <*> l3 <*> l4 let product5 l1 l2 l3 l4 l5 = (fun x1 x2 x3 x4 x5 -> x1, x2, x3, x4, x5) |> return <*> l1 <*> l2 <*> l3 <*> l4 <*> l5 let product6 l1 l2 l3 l4 l5 l6 = (fun x1 x2 x3 x4 x5 x6 -> x1, x2, x3, x4, x5, x6) |> return <*> l1 <*> l2 <*> l3 <*> l4 <*> l5 <*> l6 let product7 l1 l2 l3 l4 l5 l6 l7 = (fun x1 x2 x3 x4 x5 x6 x7 -> x1, x2, x3, x4, x5, x6, x7) |> return <*> l1 <*> l2 <*> l3 <*> l4 <*> l5 <*> l6 <*> l7 let rec cartesian_product l () = match l () with | Nil -> Cons ([], empty) | Cons (l1, tail) -> let tail = cartesian_product tail in product_with (fun x tl -> x :: tl) l1 tail () (* cartesian product of lists of lists *) let map_product_l f l = let l = map f l in cartesian_product l 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')) let rec uniq_rec_ eq prev l () = match prev, l () with | _, Nil -> Nil | None, Cons (x, l') -> Cons (x, uniq_rec_ eq (Some x) l') | Some y, Cons (x, l') -> if eq x y then uniq_rec_ eq prev l' () else Cons (x, uniq_rec_ eq (Some x) l') let uniq eq l = uniq_rec_ eq None l let chunks n e = let rec aux e () = match e () with | Nil -> Nil | Cons (x, tl) -> let a = Array.make n x in fill a 1 tl and fill a i e = (* fill the array. [i]: current index to fill *) if i = n then Cons (a, aux e) else ( match e () with | Nil -> Cons (Array.sub a 0 i, empty) (* last array is not full *) | Cons (x, tl) -> a.(i) <- x; fill a (i + 1) tl ) in aux e (* Put [x] between elements of [enum] *) let intersperse x g = let rec aux_with_sep g () = match g () with | Nil -> Nil | Cons (y, g') -> Cons (x, cons y (aux_with_sep g')) in fun () -> match g () with | Nil -> Nil | Cons (x, g) -> Cons (x, aux_with_sep g) (* functional queue *) module F_queue = struct type 'a t = { hd: 'a list; tl: 'a list } (** Queue containing elements of type 'a *) let empty = { hd = []; tl = [] } (* invariant: if hd=[], then tl=[] *) let make_ hd tl = match hd with | [] -> { hd = List.rev tl; tl = [] } | _ :: _ -> { hd; tl } let list_is_empty = function | [] -> true | _ :: _ -> false let is_empty q = list_is_empty q.hd let push x q = make_ q.hd (x :: q.tl) let pop_exn q = match q.hd with | [] -> assert (list_is_empty q.tl); invalid_arg "F_queue.pop_exn" | x :: hd' -> let q' = make_ hd' q.tl in x, q' end type 'a merge_op = Merge_from of 'a t | Merge_start of 'a t t let merge gens : _ t = (* recursive function to get next element @param q the already produced generators @param tl the generators still untouched *) let rec next (q : 'a merge_op F_queue.t) () = if F_queue.is_empty q then Nil else ( match F_queue.pop_exn q with | Merge_from g, q' -> yield_from g q' | Merge_start gens, q' -> (match gens () with | Nil -> next q' () | Cons (g, gens') -> let q' = F_queue.push (Merge_start gens') q' in yield_from g q') ) and yield_from g q = match g () with | Nil -> next q () | Cons (x, g') -> Cons (x, next (F_queue.push (Merge_from g') q)) in let q = F_queue.push (Merge_start gens) F_queue.empty in next q let intersection cmp gen1 gen2 : _ t = let rec next x1 x2 () = match x1, x2 with | Cons (y1, tl1), Cons (y2, tl2) -> let c = cmp y1 y2 in if c = 0 (* equal elements, yield! *) then Cons (y1, fun () -> next (tl1 ()) (tl2 ()) ()) else if c < 0 (* drop y1 *) then next (tl1 ()) x2 () else (* drop y2 *) next x1 (tl2 ()) () | _ -> Nil in fun () -> next (gen1 ()) (gen2 ()) () let rec zip_with f a b () = match a (), b () with | Cons (xa, tla), Cons (xb, tlb) -> Cons (f xa xb, zip_with f tla tlb) | _ -> Nil let sorted_merge cmp gen1 gen2 : _ t = let rec next x1 x2 () = match x1, x2 with | Nil, Nil -> Nil | Cons (y1, tl1), Cons (y2, tl2) -> if cmp y1 y2 <= 0 then Cons (y1, next (tl1 ()) x2) else Cons (y2, next x1 (tl2 ())) | Cons _, Nil -> x1 | Nil, Cons _ -> x2 in fun () -> next (gen1 ()) (gen2 ()) () let round_robin ?(n = 2) gen : _ t list = let rec start i = if i = n then [] else ( let g = take_nth n (drop i gen) in g :: start (i + 1) ) in start 0 (** {2 Combinatorics} *) (* state of the permutation machine. One machine manages one element [x], and depends on a deeper machine [g] that generates permutations of the list minus this element (down to the empty list). The machine can do two things: - insert the element in the current list of [g], at any position - obtain the next list of [g] *) let permutations l = let rec aux n l = match l with | [] -> assert (n = 0); return [] | x :: tail -> aux (n - 1) tail >>= fun tail -> insert_ x [] tail (* insert [x] in [tail[i…n]] *) and insert_ x left right : _ t = match right with | [] -> return (List.rev (x :: left)) | y :: right' -> cons (List.rev_append left (x :: right)) (insert_ x (y :: left) right') in aux (List.length l) l let combinations n g = assert (n >= 0); let rec make_state n l () = match n, l () with | 0, _ -> Cons ([], empty) | _, Nil -> Nil | _, Cons (x, tail) -> let m1 = make_state (n - 1) tail in let m2 = make_state n tail in add x m1 m2 () and add x m1 m2 () = match m1 () with | Nil -> m2 () | Cons (l, m1') -> Cons (x :: l, add x m1' m2) in make_state n g let power_set g : _ t = let rec make_state l () = match l with | [] -> Cons ([], empty) | x :: tail -> let m = make_state tail in add x m () and add x m () = match m () with | Nil -> Nil | Cons (l, m') -> Cons (x :: l, cons l (add x m')) in let l = fold (fun acc x -> x :: acc) [] g in make_state l (** {2 Conversions} *) let rec to_rev_list_rec_ acc l = match l () with | Nil -> acc | Cons (x, l') -> to_rev_list_rec_ (x :: acc) l' let to_rev_list l = to_rev_list_rec_ [] l let to_list l = let rec direct i (l : 'a t) = match l () with | Nil -> [] | _ when i = 0 -> List.rev (to_rev_list_rec_ [] 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 ?(start = 0) ?len a = let len = match len with | Some l -> l | None -> Array.length a - start in let rec aux a i () = if i = len then Nil else Cons (a.(i), aux a (i + 1)) in aux a start 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 let to_buffer buf g = iter (Buffer.add_char buf) g let of_string ?(start = 0) ?len s = let len = match len with | None -> String.length s - start | Some n -> assert (n + start < String.length s); n in let rec aux i () = if i >= start + len then Nil else ( let x = s.[i] in Cons (x, aux (i + 1)) ) in aux 0 let to_string s = let buf = Buffer.create 16 in to_buffer buf s; Buffer.contents buf let concat_string ~sep s = match s () with | Nil -> "" | Cons (x, tl) -> let sep_len = String.length sep in let len = fold (fun len s -> String.length s + sep_len + len) (String.length x) tl in let bytes = Bytes.make len '\000' in let (_ : int) = fold (fun off s -> let slen = String.length s in assert (off + slen <= len); Bytes.unsafe_blit (Bytes.unsafe_of_string s) 0 bytes off slen; if off + slen < len then ( (* not the last chunk *) Bytes.unsafe_blit (Bytes.unsafe_of_string sep) 0 bytes (off + slen) sep_len; off + slen + sep_len ) else off + slen) 0 s in Bytes.unsafe_to_string bytes let rec to_iter res k = match res () with | Nil -> () | Cons (s, f) -> k s; to_iter 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 'a node let of_gen g = let rec consume r () = match !r with | Of_gen_saved cons -> cons | Of_gen_thunk g -> (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) in consume (ref (Of_gen_thunk g)) let rec of_gen_transient f () = match f () with | None -> Nil | Some x -> Cons (x, of_gen_transient f) 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)) let lines g : _ t = let rec aux g buf () = match g () with | Nil -> (* only return a non-empty line *) if Buffer.length buf = 0 then Nil else ( let s = Buffer.contents buf in Buffer.clear buf; Cons (s, empty) ) | Cons (c, tl) -> if c = '\n' then ( let s = Buffer.contents buf in Buffer.clear buf; Cons (s, aux tl buf) ) else ( Buffer.add_char buf c; aux tl buf () ) in aux g (Buffer.create 16) let unlines g : _ t = let rec aux g st () = match st with | `Stop -> Nil | `Next -> (match g () with | Nil -> Nil | Cons ("", tl) -> Cons ('\n', aux tl st) (* empty line *) | Cons (s, tl) -> Cons (s.[0], aux tl (`Consume (s, 1)))) | `Consume (s, i) when i = String.length s -> Cons ('\n', aux g `Next) | `Consume (s, i) -> Cons (s.[i], aux g (`Consume (s, i + 1))) in aux g `Next type 'a memoize = MemoThunk | MemoSave of 'a node | MemoExn of exn let rec memoize f = let r = ref MemoThunk in fun () -> match !r with | MemoSave l -> l | MemoExn e -> raise e | MemoThunk -> (try let l = match f () with | Nil -> Nil | Cons (x, tail) -> Cons (x, memoize tail) in r := MemoSave l; l with e -> r := MemoExn e; raise e) module Generator = struct type 'a t = | Skip | Yield of 'a | Delay of (unit -> 'a t) | Append of 'a t * 'a t let empty = Skip let yield x = Yield x let ( >>= ) x f = Append (x, Delay f) let delay f = Delay f let run (x : 'a t) : 'a seq = let rec aux l () = match l with | [] -> Nil | Skip :: tl -> aux tl () | Yield x :: tl -> Cons (x, aux tl) | Delay f :: tl -> aux (f () :: tl) () | Append (x1, x2) :: tl -> aux (x1 :: x2 :: tl) () in aux [ x ] end module type HashedType = Hashtbl.HashedType let group_by_fold (type k) (module K : HashedType with type t = k) ~project ~fold ~init seq = let module Tbl = Hashtbl.Make (K) in (* compute group table *) let tbl = lazy (let tbl = Tbl.create 32 in iter (fun x -> let key = project x in let acc = try Tbl.find tbl key with Not_found -> init in let acc = fold acc x in Tbl.replace tbl key acc) seq; Tbl.to_seq tbl) in (* delay start *) fun () -> (Lazy.force tbl) () let group_by key ~project seq = group_by_fold key ~project ~fold:(fun l x -> x :: l) ~init:[] seq let group_count key seq = group_by_fold key ~project:(fun x -> x) ~fold:(fun n _x -> n + 1) ~init:0 seq let join_by (type k) (module Key : HashedType with type t = k) ~project_left ~project_right ~merge seq1 seq2 : _ t = let module Tbl = Hashtbl.Make (Key) in let tbl_left = Tbl.create 16 in let tbl_right = Tbl.create 16 in let seq1 = ref seq1 in let seq2 = ref seq2 in let get_l tbl k = try Tbl.find tbl k with Not_found -> [] in let next_left = ref true in let q = Queue.create () in let rec gen () = match Queue.take q with | x -> Some x | exception Queue.Empty -> if !next_left then ( next_left := false; match !seq1 () with | Nil -> () | Cons (x, tl1) -> seq1 := tl1; let key = project_left x in Tbl.replace tbl_left key (x :: get_l tbl_left key); (* join [x] with the RHS items that have the same key *) let ys = get_l tbl_right key in List.iter (fun y -> match merge key x y with | None -> () | Some r -> Queue.push r q) ys ) else ( next_left := true; match !seq2 () with | Nil -> () | Cons (y, tl2) -> seq2 := tl2; let key = project_right y in Tbl.replace tbl_right key (y :: get_l tbl_right key); (* join [y] with the LHS items that have the same key *) let xs = get_l tbl_left key in List.iter (fun x -> match merge key x y with | None -> () | Some r -> Queue.push r q) xs ); gen () in memoize (of_gen_transient gen) let join_by_fold (type k) (module Key : HashedType with type t = k) ~project_left ~project_right ~init ~merge seq1 seq2 : _ t = let module Tbl = Hashtbl.Make (Key) in let tbl_left = Tbl.create 16 in let get_l tbl k = try Tbl.find tbl k with Not_found -> [] in (* index all of [seq1] by key *) iter (fun x -> let key = project_left x in Tbl.replace tbl_left key (x :: get_l tbl_left key)) seq1; let tbl = Tbl.create 16 in (* do product by iterating on [seq2] *) iter (fun y -> let key = project_right y in let xs = get_l tbl_left key in match xs with | [] -> () | _ -> let acc = try Tbl.find tbl key with Not_found -> init in let acc = List.fold_left (fun acc x -> merge key x y acc) acc xs in Tbl.replace tbl key acc) seq2; Tbl.to_seq tbl |> map snd module IO = struct let with_file_in ?(mode = 0o644) ?(flags = []) filename f = let ic = open_in_gen flags mode filename in try let x = f ic in close_in_noerr ic; x with e -> close_in_noerr ic; raise e let with_in ?mode ?flags filename f = with_file_in ?mode ?flags filename (fun ic -> f @@ of_gen @@ fun () -> try Some (input_char ic) with End_of_file -> None) let with_lines ?mode ?flags filename f = with_file_in ?mode ?flags filename (fun ic -> f @@ of_gen @@ fun () -> try Some (input_line ic) with End_of_file -> None) let with_file_out ?(mode = 0o644) ?(flags = [ Open_creat; Open_wronly ]) filename f = let oc = open_out_gen flags mode filename in try let x = f oc in close_out oc; x with e -> close_out_noerr oc; raise e let write_str ?mode ?flags ?(sep = "") filename g = with_file_out ?mode ?flags filename (fun oc -> iteri (fun i s -> if i > 0 then output_string oc sep; output_string oc s) g) let write ?mode ?flags filename g = with_file_out ?mode ?flags filename (fun oc -> iter (fun c -> output_char oc c) g) let write_lines ?mode ?flags filename g = with_file_out ?mode ?flags filename (fun oc -> iter (fun s -> output_string oc s; output_char oc '\n') g) end 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 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' include Seq oseq-0.5.1/src/mkshims.ml0000644000175000017500000000116214534102210013742 0ustar kylekyle let code_before_407 = {| let rec seq_of_list_ l () = match l with | [] -> Seq.Nil | x :: tl -> Seq.Cons (x, seq_of_list_ tl) module Tbl_make(H:Hashtbl.HashedType) = struct include Hashtbl.Make(H) let to_seq tbl = let l = fold (fun k v l -> (k,v) :: l) tbl [] in seq_of_list_ l end|} let code_after_407 = {|module Tbl_make = Hashtbl.Make |} let () = let major, minor = Scanf.sscanf Sys.ocaml_version "%u.%u" (fun major minor -> major, minor) in let after_4_7 = (major, minor) >= (4, 7) in if after_4_7 then ( print_string code_after_407 ) else ( print_string code_before_407 ) oseq-0.5.1/src/OSeq.mli0000644000175000017500000005240514534102210013315 0ustar kylekyle(** OSeq: Functional Iterators *) type 'a t = 'a Seq.t type 'a seq = 'a Seq.t (* alias *) type 'a iter = ('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 val empty : 'a t (** Empty iterator, with no elements *) val return : 'a -> 'a t (** One-element iterator *) val cons : 'a -> 'a t -> 'a t val repeat : 'a -> 'a t (** Repeat same element endlessly *) val head_exn : 'a t -> 'a (** Returns first element, or fails. @raise Invalid_argument on an empty sequence @since 0.4 *) val tail_exn : 'a t -> 'a t (** Returns list without its first element, or fails. @raise Invalid_argument on an empty sequence @since 0.4 *) val cycle : 'a t -> 'a t (** Cycle through the iterator infinitely. The iterator shouldn't be empty. {[# OSeq.(cycle (1--3) |> take 10 |> to_list);; - : int list = [1; 2; 3; 1; 2; 3; 1; 2; 3; 1] ]} *) val iterate : ('a -> 'a) -> 'a -> 'a t (** [iterate x f] is [[x; f x; f (f x); f (f (f x)); ...]]. {[# OSeq.(iterate 0 succ |> take 10 |> to_list);; - : int list = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9] ]} *) val unfold : ('b -> ('a * 'b) option) -> 'b -> 'a t (** Dual of {!fold}, with a deconstructing operation. It keeps on unfolding the ['b] value into a new ['b], and a ['a] which is yielded, until [None] is returned. {[# OSeq.(unfold (fun x -> if x<5 then Some (string_of_int x, x+1) else None) 0 |> to_list);; - : string list = ["0"; "1"; "2"; "3"; "4"] ]} *) val repeatedly : (unit -> 'a) -> 'a t (** Call the same function an infinite number of times (useful for instance if the function is a random iterator). *) val init : int -> (int -> 'a) -> 'a t (** Calls the function, starting from 0, on increasing indices, up to [n-1]. [n] must be non-negative. For instance [init 4 (fun x->x)] will yield 0, 1, 2, and 3. *) (** {2 Basic combinators} *) val is_empty : _ t -> bool (** Check whether the iterator is empty. Pops an element, if any *) val fold : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b (** Fold on the iterator, tail-recursively. *) val fold_left : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b (** Alias to {!fold} *) val foldi : (int -> 'b -> 'a -> 'b) -> 'b -> 'a t -> 'b (** Fold on the iterator, tail-recursively. @since 0.3 *) val reduce : ('a -> 'a -> 'a) -> 'a t -> 'a (** Fold on non-empty iterators. @raise Invalid_argument on an empty iterator *) val scan : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b t (** Like {!fold}, but keeping successive values of the accumulator. {[ # OSeq.(scan (+) 0 (1--5) |> to_list);; - : int list = [0; 1; 3; 6; 10; 15] ]} *) val unfold_scan : ('b -> 'a -> 'b * 'c) -> 'b -> 'a t -> 'c t (** A mix of {!unfold} and {!scan}. The current state is combined with the current element to produce a new state, and an output value of type 'c. *) val iter : ('a -> unit) -> 'a t -> unit (** Iterate on the iterator . *) val iteri : (int -> 'a -> unit) -> 'a t -> unit (** Iterate on elements with their index in the iterator, from 0. *) val length : _ t -> int (** Length of an iterator (linear time). *) val map : ('a -> 'b) -> 'a t -> 'b t (** Lazy map. No iteration is performed now, the function will be called when the result is traversed. *) val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t (** Lazy map with indexing starting from 0. No iteration is performed now, the function will be called when the result is traversed. *) val app : ('a -> 'b) t -> 'a t -> 'b t (** Applicative *) val fold_map : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b t (** Lazy fold and map. No iteration is performed now, the function will be called when the result is traversed. The result is an iterator over the successive states of the fold. The final accumulator is discarded. Unlike {!scan}, fold_map does not return the first accumulator. *) val append : 'a t -> 'a t -> 'a t (** Append the two iterators; the result contains the elements of the first, then the elements of the second iterator. *) val flatten : 'a t t -> 'a t (** Flatten the iterator of iterators *) val flat_map : ('a -> 'b t) -> 'a t -> 'b t (** Monadic bind; each element is transformed to a sub-iterator which is then iterated on, before the next element is processed, and so on. *) val app_interleave : ('a -> 'b) t -> 'a t -> 'b t (** Same as {!app} but interleaves the values of the function and the argument iterators. See {!interleave} for more details. @since 0.4 *) val flat_map_interleave : ('a -> 'b t) -> 'a t -> 'b t (** [flat_map_interleave f seq] is similar to [flat_map f seq], except that each sub-sequence is interleaved rather than concatenated in order. See {!interleave} for more details. @since 0.4 *) val mem : ('a -> 'a -> bool) -> 'a -> 'a t -> bool (** Is the given element, member of the iterator? *) val take : int -> 'a t -> 'a t (** Take at most n elements *) val drop : int -> 'a t -> 'a t (** Drop n elements *) val nth : int -> 'a t -> 'a (** n-th element, or Not_found @raise Not_found if the iterator contains less than [n] arguments *) val take_nth : int -> 'a t -> 'a t (** [take_nth n g] returns every element of [g] whose index is a multiple of [n]. For instance [take_nth 2 (1--10) |> to_list] will return [[1;3;5;7;9]] *) val filter : ('a -> bool) -> 'a t -> 'a t (** Filter out elements that do not satisfy the predicate. *) val take_while : ('a -> bool) -> 'a t -> 'a t (** Take elements while they satisfy the predicate. *) val fold_while : ('a -> 'b -> 'a * [ `Stop | `Continue ]) -> 'a -> 'b t -> 'a (** Fold elements until (['a, `Stop]) is indicated by the accumulator. *) val drop_while : ('a -> bool) -> 'a t -> 'a t (** Drop elements while they satisfy the predicate. *) val filter_map : ('a -> 'b option) -> 'a t -> 'b t (** Maps some elements to 'b, drop the other ones *) val zip_index : 'a t -> (int * 'a) t (** Zip elements with their index in the iterator *) val unzip : ('a * 'b) t -> 'a t * 'b t (** Unzip into two iterators, splitting each pair *) val partition : ('a -> bool) -> 'a t -> 'a t * 'a t (** [partition p l] returns the elements that satisfy [p], and the elements that do not satisfy [p] *) val for_all : ('a -> bool) -> 'a t -> bool (** Is the predicate true for all elements? *) val exists : ('a -> bool) -> 'a t -> bool (** Is the predicate true for at least one element? *) val min : lt:('a -> 'a -> bool) -> 'a t -> 'a (** Minimum element, according to the given comparison function. @raise Invalid_argument if the iterator is empty *) val max : lt:('a -> 'a -> bool) -> 'a t -> 'a (** Maximum element, see {!min} @raise Invalid_argument if the iterator is empty *) val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool (** Equality of iterators. *) val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int (** Lexicographic comparison of iterators. If a iterator is a prefix of the other one, it is considered smaller. *) val find : ('a -> bool) -> 'a t -> 'a option (** [find p e] returns the first element of [e] to satisfy [p], or None. *) val find_map : ('a -> 'b option) -> 'a t -> 'b option (** [find_map f e] returns the result of [f] on the first element of [e] for which it returns [Some _], or [None] otherwise. @since 0.3 *) val sum : int t -> int (** Sum of all elements *) (** {2 Multiple iterators} *) val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t (** Map on the two iterators. Stops once one of them is exhausted.*) val iter2 : ('a -> 'b -> unit) -> 'a t -> 'b t -> unit (** Iterate on the two iterators. Stops once one of them is exhausted.*) val fold2 : ('acc -> 'a -> 'b -> 'acc) -> 'acc -> 'a t -> 'b t -> 'acc (** Fold the common prefix of the two iterators *) val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool (** Succeeds if all pairs of elements satisfy the predicate. Ignores elements of an iterator if the other runs dry. *) val exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool (** Succeeds if some pair of elements satisfy the predicate. Ignores elements of an iterator if the other runs dry. *) val zip_with : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t (** Combine common part of the gens (stops when one is exhausted) *) val zip : 'a t -> 'b t -> ('a * 'b) t (** Zip together the common part of the gens *) (** {2 Complex combinators} *) val merge : 'a t t -> 'a t (** Pick elements fairly in each sub-iterator. The merge of gens [e1, e2, ... ] picks elements in [e1], [e2], in [e3], [e1], [e2] .... Once an iterator is empty, it is skipped; when they are all empty, and none remains in the input, their merge is also empty. For instance, [merge [1;3;5] [2;4;6]] will be, in disorder, [1;2;3;4;5;6]. *) val intersection : ('a -> 'a -> int) -> 'a t -> 'a t -> 'a t (** Intersection of two sorted iterators. Only elements that occur in both inputs appear in the output *) val sorted_merge : ('a -> 'a -> int) -> 'a t -> 'a t -> 'a t (** Merge two sorted iterators into a sorted iterator *) val round_robin : ?n:int -> 'a t -> 'a t list (** Split the iterator into [n] iterators in a fair way. Elements with [index = k mod n] with go to the k-th iterator. [n] default value is 2. *) val interleave : 'a t -> 'a t -> 'a t (** [interleave a b] yields an element of [a], then an element of [b], and so on. When one of the iterators is exhausted, this behaves like the other iterator. *) val intersperse : 'a -> 'a t -> 'a t (** Put the separator element between all elements of the given iterator *) val product : 'a t -> 'b t -> ('a * 'b) t (** Cartesian product, in no predictable order. Works even if some of the arguments are infinite. *) val product3 : 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) t (** Cartesian product of three iterators, see product. @since 0.2 *) val product4 : 'a t -> 'b t -> 'c t -> 'd t -> ('a * 'b * 'c * 'd) t (** Cartesian product of four iterators, see product. @since 0.2 *) val product5 : 'a t -> 'b t -> 'c t -> 'd t -> 'e t -> ('a * 'b * 'c * 'd * 'e) t (** Cartesian product of five iterators, see product. @since 0.2 *) val product6 : 'a t -> 'b t -> 'c t -> 'd t -> 'e t -> 'f t -> ('a * 'b * 'c * 'd * 'e * 'f) t (** Cartesian product of six iterators, see product. @since 0.2 *) val product7 : 'a t -> 'b t -> 'c t -> 'd t -> 'e t -> 'f t -> 'g t -> ('a * 'b * 'c * 'd * 'e * 'f * 'g) t (** Cartesian product of seven iterators, see product. @since 0.2 *) val cartesian_product : 'a t t -> 'a list t (** Produce the cartesian product of this sequence of sequences, by returning all the ways of picking one element per sequence. {b NOTE} the order of the returned sequence is unspecified. This assumes each sub-sequence is finite, and that the main sequence is also finite. For example: {[ # cartesian_product [[1;2];[3];[4;5;6]] |> sort = [[1;3;4];[1;3;5];[1;3;6];[2;3;4];[2;3;5];[2;3;6]];; # cartesian_product [[1;2];[];[4;5;6]] = [];; # cartesian_product [[1;2];[3];[4];[5];[6]] |> sort = [[1;3;4;5;6];[2;3;4;5;6]];; ]} invariant: [cartesian_product l = map_product_l id l]. @since 0.2 *) val map_product_l : ('a -> 'b t) -> 'a t -> 'b list t (** [map_product_l f l] maps each element of [l] to a list of objects of type ['b] using [f]. We obtain [[l1;l2;...;ln]] where [length l=n] and [li : 'b list]. Then, it returns all the ways of picking exactly one element per [li]. @since 0.2 *) val group : ('a -> 'a -> bool) -> 'a t -> 'a t t (** Group equal consecutive elements together. *) val uniq : ('a -> 'a -> bool) -> 'a t -> 'a t (** Remove consecutive duplicate elements. Basically this is like [fun e -> map List.hd (group e)]. *) val sort : ('a -> 'a -> int) -> 'a t -> 'a t (** Sort according to the given comparison function. The iterator must be finite. *) val sort_uniq : ('a -> 'a -> int) -> 'a t -> 'a t (** Sort and remove duplicates. The iterator must be finite. *) val chunks : int -> 'a t -> 'a array t (** [chunks n e] returns a iterator of arrays of length [n], composed of successive elements of [e]. The last array may be smaller than [n] *) val permutations : 'a list -> 'a list t (** Permutations of the list. *) val combinations : int -> 'a t -> 'a list t (** Combinations of given length. The ordering of the elements within each combination is unspecified. Example (ignoring ordering): [combinations 2 (1--3) |> to_list = [[1;2]; [1;3]; [2;3]]] *) val power_set : 'a t -> 'a list t (** All subsets of the iterator (in no particular order). The ordering of the elements within each subset is unspecified. *) (** {2 Relational combinators} *) module type HashedType = Hashtbl.HashedType (** A type that can be compared and hashed. invariant: for any [x] and [y], if [equal x y] then [hash x=hash y] must hold. @since 0.4 *) val group_by : (module HashedType with type t = 'key) -> project:('a -> 'key) -> 'a t -> ('key * 'a list) t (** Group together elements that project onto the same key, ignoring their order of appearance. The order of each resulting list is unspecified. This function needs to consume the whole input before it can emit anything. @since 0.4 *) val group_by_fold : (module HashedType with type t = 'key) -> project:('a -> 'key) -> fold:('b -> 'a -> 'b) -> init:'b -> 'a t -> ('key * 'b) t (** Group together elements that project onto the same key, folding them into some aggregate of type ['b] as they are met. This is the most general version of the "group_by" functions. This function needs to consume the whole input before it can emit anything. @since 0.4 *) val group_count : (module HashedType with type t = 'a) -> 'a t -> ('a * int) t (** Map each distinct element to its number of occurrences in the whole seq. Similar to [group_by_fold hash_key ~project:(fun x->x) ~fold:(fun a _->a+1) ~init:0 seq]. This function needs to consume the whole input before it can emit anything. @since 0.4 *) val join_by : (module HashedType with type t = 'key) -> project_left:('a -> 'key) -> project_right:('b -> 'key) -> merge:('key -> 'a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c t (** [join_by ~project_left ~project_right ~merge a b] takes every pair of elements [x] from [a] and [y] from [b], and if they map onto the same key [k] by [project_left] and [project_right] respectively, and if [merge k x y = Some res], then it yields [res]. If [merge k x y] returns [None], the combination of values is discarded. This function works with infinite inputs, it does not have to consume the whole input before yielding elements. @since 0.4 *) val join_by_fold : (module HashedType with type t = 'key) -> project_left:('a -> 'key) -> project_right:('b -> 'key) -> init:'c -> merge:('key -> 'a -> 'b -> 'c -> 'c) -> 'a t -> 'b t -> 'c t (** [join_by_fold ~project_left ~project_right ~init ~merge a b] takes every pair of elements [x] from [a] and [y] from [b], and if they map onto the same key [k] by [project_left] and [project_right] respectively, it fold [x] and [y] into the accumulator for this key (which starts at [init]). This function consumes both inputs entirely before it emits anything. @since 0.4 *) (** {2 Basic conversion functions} *) val of_list : 'a list -> 'a t (** Enumerate elements of the list *) val to_list : 'a t -> 'a list (** non tail-call trasnformation to list, in the same order *) val to_rev_list : 'a t -> 'a list (** Tail call conversion to list, in reverse order (more efficient) *) val to_array : 'a t -> 'a array (** Convert the iterator to an array (not very efficient). The iterator must be memoized, as it's traversed twice. *) val of_array : ?start:int -> ?len:int -> 'a array -> 'a t (** Iterate on (a slice of) the given array *) val of_gen : 'a gen -> 'a t (** Build a functional iterator from a mutable, imperative generator. The result is properly memoized and can be iterated on several times, as a normal functional value. *) val of_gen_transient : 'a gen -> 'a t (** Build a functional iterator from a mutable, imperative generator. Note that the resulting iterator is not going to be really functional because the underlying generator can be consumed only once. Use {!memoize} to recover the proper semantics, or use {!of_gen} directly. *) val to_gen : 'a t -> 'a gen (** Build a mutable iterator that traverses this functional iterator. @since 0.4 *) val of_string : ?start:int -> ?len:int -> string -> char t (** Iterate on bytes of the string *) val to_string : char t -> string (** Convert into a string *) val to_buffer : Buffer.t -> char t -> unit (** Traverse the iterator and writes its content to the buffer *) val to_iter : 'a t -> 'a iter (** Iterate on the whole sequence. @since 0.4 *) val concat_string : sep:string -> string t -> string (** [concat_string ~sep s] concatenates all strings of [i], separated with [sep]. The iterator must be memoized. @since 0.3 *) val lines : char t -> string t (** Group together chars belonging to the same line *) val unlines : string t -> char t (** Explode lines into their chars, adding a ['\n'] after each one *) module Infix : sig val ( -- ) : int -> int -> int t (** Integer range, inclusive *) val ( --^ ) : int -> int -> int t (** Integer range, exclusive in the right bound *) val ( >>= ) : 'a t -> ('a -> 'b t) -> 'b t (** Monadic bind operator *) val ( >>| ) : 'a t -> ('a -> 'b) -> 'b t (** Infix map operator *) val ( >|= ) : 'a t -> ('a -> 'b) -> 'b t (** Infix map operator *) val ( <*> ) : ('a -> 'b) t -> 'a t -> 'b t val ( let+ ) : 'a t -> ('a -> 'b) -> 'b t (** Alias for {!map} @since 0.5 *) val ( and+ ) : 'a t -> 'b t -> ('a * 'b) t (** Alias for {!product} @since 0.5 *) val ( let* ) : 'a t -> ('a -> 'b t) -> 'b t (** Alias for {!flat_map} @since 0.5 *) val ( and* ) : 'a t -> 'b t -> ('a * 'b) t (** Alias for {!product} @since 0.5 *) end include module type of Infix val pp : ?sep:string -> 'a printer -> 'a t printer (** Pretty print the content of the iterator on a formatter. *) val memoize : 'a t -> 'a t (** Store content of the transient iterator in memory, to be able to iterate on it several times later. *) (** {2 Easy interface to Produce Iterators} *) (** This interface is designed to make it easy to build complex streams of values in a way that resembles Python's generators (using "yield"). {[ let naturals : int OSeq.t = OSeq.Generator.( let rec aux n = yield n >>= fun () -> aux (n+1) in run (aux 0) ) ]} {[ type 'a tree = E | N of 'a tree * 'a * 'a tree let traverse (t:'a tree) : 'a OSeq.t = let open OSeq.Generator in let rec trav = function | E -> empty | N (l,v,r) -> trav l >>= fun () -> yield v >>= fun () -> trav r in run (trav t) ]} *) module Generator : sig type 'a t (** Type for writing generators (of type ['a OSeq.Generator.t]) that can be used to construct an iterator of type ['a OSeq.t] *) val empty : 'a t (** Empty generator, yields no value *) val yield : 'a -> 'a t (** Yield one value *) val ( >>= ) : 'a t -> (unit -> 'a t) -> 'a t (** [gen1 >>= fun () -> gen2] first yields all values from [gen1], and then all values from [gen2] *) val delay : (unit -> 'a t) -> 'a t (** Delayed generator, will evaluate the function when needed *) val run : 'a t -> 'a seq (** Iterator over the values yielded by the generator *) end (** {2 Basic IO} *) module IO : sig val with_in : ?mode:int -> ?flags:open_flag list -> string -> (char t -> 'a) -> 'a (** [with_in filename f] opens [filename] and calls [f g], where [g] is a generator of characters from the file. The generator is only valid within the scope in which [f] is called. *) val with_lines : ?mode:int -> ?flags:open_flag list -> string -> (string t -> 'a) -> 'a (** [with_lines filename f] opens file [filename] and calls [f g], where [g] is a generator that iterates on the lines from the file. Do not use the generator outside of the scope of [f] *) val write_str : ?mode:int -> ?flags:open_flag list -> ?sep:string -> string -> string t -> unit (** [write_to filename g] writes all strings from [g] into the given file. It takes care of opening and closing the file. Does not add [sep] after the last string. @param mode default [0o644] @param flags used by [open_out_gen]. Default: [[Open_creat;Open_wronly]]. @param sep separator between each string (e.g. newline) *) val write : ?mode:int -> ?flags:open_flag list -> string -> char t -> unit (** Same as {!write_str} but with individual characters *) val write_lines : ?mode:int -> ?flags:open_flag list -> string -> string t -> unit (** [write_lines file g] is similar to [write_str file g ~sep:"\n"] but also adds ['\n'] at the end of the file *) 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 include module type of Seq with type 'a node = 'a Seq.node and type 'a t := 'a t oseq-0.5.1/src/mkflags.ml0000644000175000017500000000062614534102210013717 0ustar kylekyle let () = let major, minor = Scanf.sscanf Sys.ocaml_version "%u.%u" (fun major minor -> major, minor) in let after_4_3 = (major, minor) >= (4, 3) in let flags_file = open_out "flambda.flags" in if after_4_3 then ( output_string flags_file "(-O3 -unbox-closures -unbox-closures-factor 20 -color always)\n"; ) else ( output_string flags_file "()\n"; ); close_out flags_file oseq-0.5.1/src/dune0000644000175000017500000000045314534102210012615 0ustar kylekyle(rule (targets flambda.flags) (deps (file mkflags.ml)) (mode fallback) (action (run ocaml ./mkflags.ml))) (library (name oseq) (public_name oseq) (wrapped false) (modules OSeq) (flags :standard -safe-string -warn-error -a+8) (ocamlopt_flags :standard (:include flambda.flags))) oseq-0.5.1/README.md0000644000175000017500000000060614534102210012427 0ustar kylekyle# OSeq [![build status](https://github.com/c-cube/oseq/workflows/build/badge.svg)](https://github.com/c-cube/oseq/actions/) Simple list of suspensions, as a composable lazy iterator that behaves like a value. The type of sequences, `'a OSeq.t`, is compatible with the new standard type of iterators `'a Seq.t`. ## Documentation https://c-cube.github.io/oseq/ ## License BSD license. oseq-0.5.1/dune-project0000644000175000017500000000005614534102210013471 0ustar kylekyle(lang dune 1.0) (name oseq) (version b44a72b) oseq-0.5.1/oseq.opam0000644000175000017500000000155414534102210013000 0ustar kylekyleversion: "0.5.1" opam-version: "2.0" name: "oseq" author: "Simon Cruanes" maintainer: "simon.cruanes.2007@m4x.org" license: "BSD-2-clause" build: [ ["dune" "build" "-p" name "-j" jobs] ["dune" "build" "@doc" "-p" name "-j" jobs] {with-doc} ["dune" "runtest" "-p" name "-j" jobs] {with-test} ] depends: [ "dune" { >= "1.0" } "qcheck" {with-test} "gen" {with-test} "containers" {with-test} "odoc" {with-doc} "ocaml" { >= "4.08.0" } ] tags: [ "sequence" "iterator" "seq" "pure" "list" ] homepage: "https://github.com/c-cube/oseq/" doc: "https://c-cube.github.io/oseq/" bug-reports: "https://github.com/c-cube/oseq/issues" dev-repo: "git+https://github.com/c-cube/oseq.git" synopsis: "Simple list of suspensions, as a composable lazy iterator that behaves like a value" description: "Extends the new standard library's `Seq` module with many useful combinators."oseq-0.5.1/.ocamlformat0000644000175000017500000000046614534102210013461 0ustar kylekyleversion = 0.24.1 profile=conventional margin=80 if-then-else=k-r parens-ite=true parens-tuple=multi-line-only sequence-style=terminator type-decl=compact break-cases=toplevel cases-exp-indent=2 field-space=tight-decl leading-nested-match-parens=true module-item-spacing=compact quiet=true ocaml-version=4.08.0 oseq-0.5.1/tests/0000755000175000017500000000000014534102210012310 5ustar kylekyleoseq-0.5.1/tests/unit/0000755000175000017500000000000014534102210013267 5ustar kylekyleoseq-0.5.1/tests/unit/t_oseq.ml0000644000175000017500000003730614534102210015124 0ustar kylekylemodule Q = QCheck open OSeq let spf = Printf.sprintf let pp_ilist = Q.Print.(list int) let plist f l = "[" ^ String.concat ";" (List.map f l) ^ "]" let ppair f1 f2 (x, y) = Printf.sprintf "(%s,%s)" (f1 x) (f2 y) let pint i = string_of_int i let popt p x = Q.Print.option p x let pilist l = plist pint l let pistrlist l = plist (ppair pint (spf "%S")) l let pilistlist l = plist (plist pint) l let pi2list l = plist (ppair pint pint) l let pstrlist l = plist (Printf.sprintf "%S") l let lsort l = List.sort Stdlib.compare l let ofll l = l |> of_list |> map of_list let cmp_lii_unord l1 l2 : bool = List.sort Stdlib.compare l1 = List.sort Stdlib.compare l2 (* list of qcheck tests *) let qchecks = ref [] let ounits = ref [] let add_qcheck line gen prop = let test = Q.Test.make gen prop ~name:(spf "qcheck %d" line) in qchecks := test :: !qchecks let add_ounit f = ounits := f :: !ounits (* compat test, ensure Seq.t and OSeq.t are the same *) let () = add_ounit @@ fun () -> ignore (Seq.empty : int OSeq.t); ignore (OSeq.empty : int Seq.t) let () = add_ounit @@ fun () -> let seq = empty in OUnit.assert_bool "empty" (is_empty seq); OUnit.assert_bool "empty" (try iter (fun _ -> raise Exit) seq; true with Exit -> false) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilist [ 0; 1; 2; 3; 4; 5 ] (0 -- 5 |> to_list); OUnit.assert_equal ~printer:pilist [ 0 ] (0 -- 0 |> to_list); OUnit.assert_equal ~printer:pilist [ 5; 4; 3; 2 ] (5 -- 2 |> to_list) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilist [ 1; 2; 3; 4 ] (1 --^ 5 |> to_list); OUnit.assert_equal ~printer:pilist [ 5; 4; 3; 2 ] (5 --^ 1 |> to_list); OUnit.assert_equal ~printer:pilist [ 1 ] (1 --^ 2 |> to_list); OUnit.assert_equal ~printer:pilist [] (0 --^ 0 |> to_list) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilist (repeat 0 |> take 4 |> to_list) [ 0; 0; 0; 0 ]; OUnit.assert_equal ~printer:pilist (repeat 1 |> take 0 |> to_list) [] let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilist [ 0; 1; 2; 3; 4 ] (init 5 (fun i -> i) |> to_list) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pi2list [ 0, 1; 1, 2; 2, 3 ] (mapi (fun i x -> i, x) (1 -- 3) |> to_list) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilist [ 6; 12; 18; 24; 30 ] (filter_map (fun x -> if x mod 2 = 0 then Some (x * 3) else None) (1 -- 10) |> to_list) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilist [ 0; 1; 2; 3; 4 ] (iterate (( + ) 1) 0 |> take 5 |> to_list) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pistrlist [ 1, "b"; 0, "a" ] (foldi (fun i acc x -> (i, x) :: acc) [] (of_list [ "a"; "b" ])) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilist [ 1; 2; 1; 2; 1 ] (cycle (of_list [ 1; 2 ]) |> take 5 |> to_list); OUnit.assert_equal ~printer:pint 0 (cycle (of_list [ 1; ~-1 ]) |> take 100_000 |> fold ( + ) 0) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilist [ 0; 1; 2; 3; 4; 5; 6; 7; 8; 9 ] (let f = function | 10 -> None | x -> Some (x, x + 1) in unfold f 0 |> to_list) let () = add_qcheck __LINE__ Q.(pair (fun1 Observable.int (small_list int)) (small_list int)) (fun (f, l) -> of_list l |> flat_map (fun x -> of_list (Q.Fn.apply f x)) |> to_list = CCList.flat_map (Q.Fn.apply f) l) let () = add_qcheck __LINE__ Q.(pair (fun1 Observable.int (small_list int)) (small_list int)) (fun (f, l) -> of_list l |> flat_map (fun x -> of_list (Q.Fn.apply f x)) |> to_list = (of_list l |> map (Q.Fn.apply f) |> map of_list |> flatten |> to_list)) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pint 4 (nth 4 (0 -- 10)); OUnit.assert_equal ~printer:pint 8 (nth 8 (0 -- 10)) let () = add_ounit @@ fun () -> OUnit.assert_bool "t" (try ignore (nth 11 (1 -- 10)); false with Not_found -> true) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pint ~-2 (min ~lt:( < ) (of_list [ 1; 4; 6; 0; 11; -2 ])); OUnit.assert_bool "t" (try ignore (min ~lt:( < ) empty); false with Invalid_argument _ -> true) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pint 11 (max ~lt:( < ) (of_list [ 1; 4; 6; 0; 11; -2 ])); OUnit.assert_bool "t" (try ignore (max ~lt:( < ) empty); false with Invalid_argument _ -> true) let () = add_qcheck __LINE__ (Q.pair (Q.list Q.small_int) (Q.list Q.small_int)) (fun (l1, l2) -> equal Stdlib.( = ) (of_list l1) (of_list l2) = (l1 = l2)) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:(ppair pilist pilist) ([ 2; 4; 6; 8; 10 ], [ 1; 3; 5; 7; 9 ]) ( partition (fun x -> x mod 2 = 0) (1 -- 10) |> fun (x, y) -> to_list x, to_list y ) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pi2list [ 0, 1; 1, 2; 2, 3; 3, 4; 4, 5 ] (zip_index (1 -- 5) |> to_list) let () = add_qcheck __LINE__ Q.(list (pair int int)) (fun l -> let l = of_list l in let a, b = unzip l in equal ( = ) l (zip a b)) let () = add_qcheck __LINE__ (Q.pair (Q.list Q.small_int) (Q.list Q.small_int)) (fun (l1, l2) -> let sign x = if x < 0 then -1 else if x = 0 then 0 else 1 in sign (compare Stdlib.compare (of_list l1) (of_list l2)) = sign (Stdlib.compare l1 l2)) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:(popt pint) (find (fun x -> x >= 5) (1 -- 10)) (Some 5); OUnit.assert_equal ~printer:(popt pint) (find (fun x -> x > 5) (1 -- 4)) None let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:(popt pint) (find_map (fun x -> if x >= 5 then Some (-x) else None) (1 -- 10)) (Some (-5)); OUnit.assert_equal ~printer:(popt pint) (find_map (fun x -> if x > 5 then Some (-x) else None) (1 -- 4)) None; OUnit.assert_equal ~printer:(popt pint) (find_map (fun _ -> None) (1 -- 10)) None let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pint (sum (1 -- 10)) 55 let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilist (of_list [ 1; 2; 3; 4 ] |> take_while (fun x -> x < 4) |> to_list) [ 1; 2; 3 ] let () = add_qcheck __LINE__ (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 () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pint 2 (fold_while (fun acc b -> if b then acc + 1, `Continue else acc, `Stop) 0 (of_list [ true; true; false; true ])) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:(plist pilist) [ []; [ 2 ]; [ 3; 2 ]; [ 4; 3; 2 ]; [ 5; 4; 3; 2 ]; [ 6; 5; 4; 3; 2 ] ] (scan (fun acc x -> (x + 1) :: acc) [] (1 -- 5) |> to_list) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilist [ 0; 1; 3; 6; 10 ] (unfold_scan (fun acc x -> x + acc, acc) 0 (1 -- 5) |> to_list) let () = add_qcheck __LINE__ Q.(pair (small_list int) (small_list int)) (fun (l1, l2) -> lsort (List.flatten @@ List.map (fun x -> List.map (fun y -> x, y) l2) l1) = lsort (product (of_list l1) (of_list l2) |> to_list)) let () = add_ounit @@ fun () -> let l = product (of_list [ true; false ]) (1 -- max_int) |> take 10 |> to_list in OUnit.assert_bool "a bit fair" (List.exists (fun (b, _) -> b = false) l) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:(plist pilist) ~cmp:cmp_lii_unord [ [ 1; 3; 4 ]; [ 1; 3; 5 ]; [ 1; 3; 6 ]; [ 2; 3; 4 ]; [ 2; 3; 5 ]; [ 2; 3; 6 ]; ] (to_list @@ cartesian_product @@ ofll [ [ 1; 2 ]; [ 3 ]; [ 4; 5; 6 ] ]); OUnit.assert_equal ~printer:(plist pilist) ~cmp:cmp_lii_unord [] (to_list @@ cartesian_product @@ ofll [ [ 1; 2 ]; []; [ 4; 5; 6 ] ]); OUnit.assert_equal ~printer:(plist pilist) ~cmp:cmp_lii_unord [ [] ] (to_list @@ cartesian_product empty); OUnit.assert_equal ~printer:(plist pilist) ~cmp:cmp_lii_unord [ [ 1; 3; 4; 5; 6 ]; [ 2; 3; 4; 5; 6 ] ] (to_list @@ cartesian_product @@ ofll [ [ 1; 2 ]; [ 3 ]; [ 4 ]; [ 5 ]; [ 6 ] ]) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:(plist pilist) [ [ 1; 1; 1 ]; [ 2; 2 ]; [ 3; 3 ]; [ 1 ] ] (of_list [ 1; 1; 1; 2; 2; 3; 3; 1 ] |> group ( = ) |> map to_list |> to_list) let () = add_qcheck __LINE__ Q.(small_list int) (fun l -> of_list l |> group ( = ) |> flatten |> to_list = l) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:(plist pilist) (List.map to_list [ 0 -- 24; 25 -- 49; 50 -- 74; 75 -- 99; 100 -- 100 ]) (chunks 25 (0 -- 100) |> map Array.to_list |> to_list) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilist [] (intersperse 0 empty |> to_list); OUnit.assert_equal ~printer:pilist [ 1 ] (intersperse 0 (return 1) |> to_list); OUnit.assert_equal ~printer:pilist [ 1; 0; 2; 0; 3; 0; 4; 0; 5 ] (intersperse 0 (1 -- 5) |> to_list) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilist [ 1; 2; 3; 4; 5; 6; 7; 8; 9 ] (merge (of_list [ of_list [ 1; 3; 5 ]; of_list [ 2; 4; 6 ]; of_list [ 7; 8; 9 ] ]) |> to_list |> List.sort Stdlib.compare); OUnit.assert_equal ~printer:pilist [ 1; 2; 3; 4; 5; 6 ] (merge (of_list [ of_list [ 1; 3; 6 ]; of_list [ 2; 5 ]; of_list [ 4 ] ]) |> to_list) let () = add_ounit @@ fun () -> OUnit.assert_bool "t" @@ mem ( = ) (3, 5) @@ take 20_000 @@ merge @@ map (fun i -> iterate succ 0 |> map (fun j -> i, j)) @@ iterate succ 0 let () = add_ounit @@ fun () -> let e = of_list [ 1 -- 3; 4 -- 6; 7 -- 9 ] in let e' = merge e in OUnit.assert_equal [ 1; 2; 3; 4; 5; 6; 7; 8; 9 ] (to_list e' |> List.sort Stdlib.compare) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilist [ 1; 2; 4; 8 ] (intersection Stdlib.compare (of_list [ 1; 1; 2; 3; 4; 8 ]) (of_list [ 1; 2; 4; 5; 6; 7; 8; 9 ]) |> to_list) let () = add_qcheck __LINE__ (Q.list Q.small_int) (fun l -> zip_with (fun x y -> x, y) (of_list l) (of_list l) |> unzip |> fst |> to_list = l) let () = add_ounit @@ fun () -> let e = zip_with ( + ) (repeat 1) (4 -- 7) in OUnit.assert_equal [ 5; 6; 7; 8 ] (to_list e) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilist [ 1; 2; 2; 2; 3; 4; 5; 5; 6; 10; 11; 100 ] (sorted_merge Stdlib.compare (of_list [ 1; 2; 2; 3; 5; 10; 100 ]) (of_list [ 2; 4; 5; 6; 11 ]) |> to_list) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilistlist [ [ 1; 4; 7; 10 ]; [ 2; 5; 8; 11 ]; [ 3; 6; 9; 12 ] ] (round_robin ~n:3 (1 -- 12) |> List.map to_list) let () = add_ounit @@ fun () -> let e = round_robin ~n:2 (1 -- 10) in match e with | [ a; b ] -> OUnit.assert_equal ~printer:pilist [ 1; 3; 5; 7; 9 ] (to_list a); OUnit.assert_equal ~printer:pilist [ 2; 4; 6; 8; 10 ] (to_list b) | _ -> OUnit.assert_failure "wrong list lenght" let () = add_ounit @@ fun () -> let e = round_robin ~n:3 (1 -- 999) in let l = List.map length e in OUnit.assert_equal ~printer:pilist [ 333; 333; 333 ] l let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilistlist [ [ 1; 2; 3 ]; [ 1; 3; 2 ]; [ 2; 1; 3 ]; [ 2; 3; 1 ]; [ 3; 1; 2 ]; [ 3; 2; 1 ]; ] (permutations CCList.(1 -- 3) |> to_list |> List.sort Stdlib.compare); OUnit.assert_equal ~printer:pilistlist [ [] ] (permutations [] |> to_list); OUnit.assert_equal ~printer:pilistlist [ [ 1 ] ] (permutations [ 1 ] |> to_list) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilistlist [ [ 1; 2 ]; [ 1; 3 ]; [ 1; 4 ]; [ 2; 3 ]; [ 2; 4 ]; [ 3; 4 ] ] (combinations 2 (1 -- 4) |> map (List.sort Stdlib.compare) |> to_list |> List.sort Stdlib.compare); OUnit.assert_equal ~printer:pilistlist [ [] ] (combinations 0 (1 -- 4) |> to_list); OUnit.assert_equal ~printer:pilistlist [ [ 1 ] ] (combinations 1 (return 1) |> to_list) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilistlist [ []; [ 1 ]; [ 1; 2 ]; [ 1; 2; 3 ]; [ 1; 3 ]; [ 2 ]; [ 2; 3 ]; [ 3 ] ] (power_set (1 -- 3) |> map (List.sort Stdlib.compare) |> to_list |> List.sort Stdlib.compare); OUnit.assert_equal ~printer:pilistlist [ [] ] (power_set empty |> to_list); OUnit.assert_equal ~printer:pilistlist [ []; [ 1 ] ] (power_set (return 1) |> map (List.sort Stdlib.compare) |> to_list |> List.sort Stdlib.compare) let () = add_qcheck __LINE__ Q.(array int) (fun a -> of_array a |> to_array = a) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pilist (of_array [| 1; 2; 3 |] |> to_list) [ 1; 2; 3 ]; OUnit.assert_equal ~printer:(Q.Print.array pint) (of_list [ 1; 2; 3 ] |> to_array) [| 1; 2; 3 |] let () = add_qcheck __LINE__ Q.(pair (list string) string) (fun (s, sep) -> String.concat sep s = concat_string ~sep (of_list s)) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:Q.Print.string (concat_string ~sep:"" (of_list [ "a"; "b"; "coucou" ])) "abcoucou"; OUnit.assert_equal ~printer:Q.Print.string (concat_string ~sep:"random" (return "a")) "a"; OUnit.assert_equal ~printer:Q.Print.string (concat_string ~sep:"," (of_list [ "a"; "b"; "c"; ""; ""; "d" ])) "a,b,c,,,d"; OUnit.assert_equal ~printer:Q.Print.string (concat_string ~sep:"random" empty) "" let () = add_ounit @@ fun () -> let g = let n = ref 0 in fun () -> Some (incr n; !n) in let l = of_gen g in OUnit.assert_equal [ 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 ] (take 10 l |> to_list); OUnit.assert_equal [ 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 ] (take 10 l |> to_list); OUnit.assert_equal [ 11; 12 ] (drop 10 l |> take 2 |> to_list) let () = add_ounit @@ fun () -> OUnit.assert_equal ~printer:pstrlist [ "abc"; "de"; "" ] (lines (of_string "abc\nde\n\n") |> to_list) let () = add_qcheck __LINE__ Q.printable_string (fun s -> of_string s |> lines |> unlines |> to_string |> String.trim = String.trim s) let () = add_ounit @@ fun () -> let naturals = Generator.( let rec aux n = yield n >>= fun () -> aux (n + 1) in run (aux 0)) in let naturals' = unfold (fun n -> Some (n, n + 1)) 0 in OUnit.assert_equal ~printer:Q.Print.(list int) (take 100 naturals' |> to_list) (take 100 naturals |> to_list) let () = add_qcheck __LINE__ Q.(small_list int) (fun l -> let seq = of_list l in let seq2 = let open Generator in let rec aux seq = match seq () with | Nil -> empty | Cons (x, tl) -> yield x >>= fun () -> aux tl in run (aux seq) in equal Stdlib.( = ) seq seq2) module IntK = struct type t = int let equal = ( = ) let hash x = x land max_int end let () = add_ounit @@ fun () -> [ 1; 2; 3; 3; 2; 2; 3; 4 ] |> of_list |> group_by (module IntK) ~project:(fun x -> x) |> map snd |> sort Stdlib.compare |> to_list |> OUnit.assert_equal [ [ 1 ]; [ 2; 2; 2 ]; [ 3; 3; 3 ]; [ 4 ] ] (* test for compat with seq *) module Foo : module type of Seq = OSeq let () = Printf.printf "running unit tests…\n%!"; List.iter (fun t -> t ()) !ounits; Printf.printf "ran %d unit tests\n%!" (List.length !ounits); Printf.printf "running qcheck tests…\n%!"; let errcode = QCheck_base_runner.run_tests ~colors:false !qchecks in if errcode <> 0 then exit errcode oseq-0.5.1/tests/unit/dune0000644000175000017500000000013614534102210014145 0ustar kylekyle (tests (names t_oseq) (libraries oseq containers qcheck-core qcheck-core.runner ounit2)) oseq-0.5.1/CHANGELOG.md0000644000175000017500000000104314534102210012755 0ustar kylekyle## 0.5.1 - do not use qtest anymore ## 0.5 - use ocamlformat - add let-operators - require OCaml 4.08 - breaking: remove most labels, ensure compatibility with extended Seq ## 0.4.1 - fix error in tests related to `-nolabels` ## 0.4 - use `include Seq` so we're forward compatible with OCaml - add relational operators: group_by, group_by_fold, join_by, join_by_fold - add `{flat_map,app}_interleave`, `to_iter`, `to_gen` - add head_exn/tail_exn - fix: make `product` more fair by using interleave - fix: handle exceptions in OSeq.memoize oseq-0.5.1/.gitignore0000644000175000017500000000007614534102210013141 0ustar kylekyle.*.swp _build *.native *.docdir *.html man/ *.install .merlin oseq-0.5.1/Makefile0000644000175000017500000000142414534102210012607 0ustar kylekyle all: build test build: @dune build @install test: @dune runtest --no-buffer --force clean: @dune clean doc: @dune build @doc benchs: @dune build $(addprefix bench/, $(BENCH_TARGETS)) VERSION=$(shell awk '/^version:/ {print $$2}' oseq.opam) update_next_tag: @echo "update version to $(VERSION)..." sed -i "s/NEXT_VERSION/$(VERSION)/g" src/*.ml src/*.mli sed -i "s/NEXT_RELEASE/$(VERSION)/g" src/*.ml src/*.mli reindent: @which ocp-indent || ( echo "require ocp-indent" ; exit 1 ) @find src '(' -name '*.ml' -or -name '*.mli' ')' -type f -print0 | xargs -0 echo "reindenting: " @find src '(' -name '*.ml' -or -name '*.mli' ')' -type f -print0 | xargs -0 ocp-indent -i WATCH?=@install watch: @dune build $(WATCH) -w .PHONY: benchs tests examples update_next_tag watch oseq-0.5.1/.github/0000755000175000017500000000000014534102210012506 5ustar kylekyleoseq-0.5.1/.github/workflows/0000755000175000017500000000000014534102210014543 5ustar kylekyleoseq-0.5.1/.github/workflows/gh-pages.yml0000644000175000017500000000161614534102210016765 0ustar kylekylename: github pages on: push: branches: - master # Set a branch name to trigger deployment jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@main - name: Cache opam id: cache-opam uses: actions/cache@v2 with: path: ~/.opam key: opam-ubuntu-latest-4.12.0 - uses: avsm/setup-ocaml@v1 with: ocaml-version: '4.12.0' - name: Pin run: opam pin -n . - name: Depext run: opam depext -yt oseq - name: Deps run: opam install -d . --deps-only - name: Build run: opam exec -- dune build @doc - name: Deploy uses: peaceiris/actions-gh-pages@v3 with: github_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: ./_build/default/_doc/_html/ destination_dir: dev enable_jekyll: true oseq-0.5.1/.github/workflows/main.yml0000644000175000017500000000126514534102210016216 0ustar kylekylename: build on: push: branches: - master pull_request: branches: - master jobs: run: name: Build strategy: matrix: os: - macos-latest - ubuntu-latest - windows-latest ocaml-compiler: - 4.08.x - 4.13.x runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v2 - uses: ocaml/setup-ocaml@v2 with: ocaml-compiler: ${{ matrix.ocaml-compiler }} - run: opam pin -n . - run: opam depext -yt oseq - run: opam install -t . --deps-only - run: opam exec -- dune build - run: opam exec -- dune runtest if: ${{ matrix.os == 'ubuntu-latest'}}