distinction diagonal,diagonal_l

This commit is contained in:
Simon Cruanes 2016-12-26 00:51:45 +01:00
parent 4748655708
commit 67a40a4882
3 changed files with 26 additions and 7 deletions

View file

@ -463,14 +463,23 @@ let product outer inner k =
let product2 outer inner k = let product2 outer inner k =
outer (fun x -> inner (fun y -> k x y)) outer (fun x -> inner (fun y -> k x y))
let rec diagonal l yield = match l with let rec diagonal_l l yield = match l with
| [] -> () | [] -> ()
| x::tail -> | x::tail ->
List.iter (fun y -> yield (x,y)) tail; List.iter (fun y -> yield (x,y)) tail;
diagonal tail yield diagonal_l tail yield
(*$= (*$=
[0,1; 0,2; 1,2] (diagonal [0;1;2] |> to_list) [0,1; 0,2; 1,2] (diagonal_l [0;1;2] |> to_list)
*)
let diagonal seq =
let l = ref [] in
seq (fun x -> l := x :: !l);
diagonal_l (List.rev !l)
(*$=
[0,1; 0,2; 1,2] (of_list [0;1;2] |> diagonal |> to_list)
*) *)
let join ~join_row s1 s2 k = let join ~join_row s1 s2 k =

View file

@ -251,9 +251,14 @@ val product : 'a t -> 'b t -> ('a * 'b) t
as required (several times), possibly by calling {!persistent} on it as required (several times), possibly by calling {!persistent} on it
beforehand. *) beforehand. *)
val diagonal : 'a list -> ('a * 'a) t val diagonal_l : 'a list -> ('a * 'a) t
(** All pairs of distinct positions of the list. [diagonal l] will (** All pairs of distinct positions of the list. [diagonal l] will
return the list of [List.nth i l, List.nth j l] if [i < j]. return the sequence of all [List.nth i l, List.nth j l] if [i < j].
@since NEXT_RELEASE *)
val diagonal : 'a t -> ('a * 'a) t
(** All pairs of distinct positions of the sequence.
Iterates only once on the sequence, which must be finite.
@since NEXT_RELEASE *) @since NEXT_RELEASE *)
val product2 : 'a t -> 'b t -> ('a, 'b) t2 val product2 : 'a t -> 'b t -> ('a, 'b) t2

View file

@ -222,9 +222,14 @@ val product : 'a t -> 'b t -> ('a * 'b) t
as required (several times), possibly by calling {!persistent} on it as required (several times), possibly by calling {!persistent} on it
beforehand. *) beforehand. *)
val diagonal : 'a list -> ('a * 'a) t val diagonal_l : 'a list -> ('a * 'a) t
(** All pairs of distinct positions of the list. [diagonal l] will (** All pairs of distinct positions of the list. [diagonal l] will
return the list of [List.nth i l, List.nth j l] if [i < j]. return the sequence of all [List.nth i l, List.nth j l] if [i < j].
@since NEXT_RELEASE *)
val diagonal : 'a t -> ('a * 'a) t
(** All pairs of distinct positions of the sequence.
Iterates only once on the sequence, which must be finite.
@since NEXT_RELEASE *) @since NEXT_RELEASE *)
val product2 : 'a t -> 'b t -> ('a, 'b) t2 val product2 : 'a t -> 'b t -> ('a, 'b) t2