From 67a40a488271c2cd991bf889529bd40576ad14f6 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 26 Dec 2016 00:51:45 +0100 Subject: [PATCH] distinction `diagonal,diagonal_l` --- src/sequence.ml | 15 ++++++++++++--- src/sequence.mli | 9 +++++++-- src/sequenceLabels.mli | 9 +++++++-- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/sequence.ml b/src/sequence.ml index 1e38e3b..425bf8d 100644 --- a/src/sequence.ml +++ b/src/sequence.ml @@ -463,14 +463,23 @@ let product outer inner k = let product2 outer inner k = 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 -> 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 = diff --git a/src/sequence.mli b/src/sequence.mli index bfc8ac1..ce7d276 100644 --- a/src/sequence.mli +++ b/src/sequence.mli @@ -251,9 +251,14 @@ val product : 'a t -> 'b t -> ('a * 'b) t as required (several times), possibly by calling {!persistent} on it 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 - 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 *) val product2 : 'a t -> 'b t -> ('a, 'b) t2 diff --git a/src/sequenceLabels.mli b/src/sequenceLabels.mli index 6ef97f3..dd80e73 100644 --- a/src/sequenceLabels.mli +++ b/src/sequenceLabels.mli @@ -222,9 +222,14 @@ val product : 'a t -> 'b t -> ('a * 'b) t as required (several times), possibly by calling {!persistent} on it 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 - 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 *) val product2 : 'a t -> 'b t -> ('a, 'b) t2