1. james woodyatt
  2. oni

Commits

jhwoodyatt  committed a1599b3

Add nil value and flatten function.

  • Participants
  • Parent commits 0cb9aa0
  • Branches default

Comments (0)

Files changed (2)

File cf/cf_seq.ml

View file
 
 exception Empty
 
+let nil = Lazy.lazy_from_val Z
+
 let head s =
     match Lazy.force s with
     | P (hd, _) -> hd
         | Z -> Lazy.force s2
     end
 
+let rec flatten z =
+    lazy begin
+        match Lazy.force z with
+        | Z -> Z
+        | P (hd, tl) ->
+            match Lazy.force hd with
+            | P (hd2, tl2) -> P (hd2, flatten (lazy (P (tl2, tl))))
+            | Z -> Lazy.force (flatten tl)
+    end
+
 let rec limit n s =
     if n < 0 then invalid_arg "Cf_seq.limit: n < 0";
     lazy begin

File cf/cf_seq.mli

View file
 
 (** {6 Functions} *)
 
+(** An empty sequence. *)
+val nil: 'a t
+
 (** Returns the first element in the sequence.  Raises [Empty] if the sequence
     has no elements, i.e. if the sequence is [Z].
 *)
 *)
 val concat: 'a t -> 'a t -> 'a t
 
+(** [flatten a] returns the sequence of all the elements in the sequence of
+    sequences by concatenating them.
+*)
+val flatten: 'a t t -> 'a t
+
 (** [limit n s] returns the sequence of all the elements in [s], up to [n]
     elements in number and no more.  Raises [Invalid_argument] if [n < 0].
 *)