Module Lwt_stream

module Lwt_stream : sig..end

Data streams


type 'a t

Type of a stream holding values of type 'a


Naming convention: in this module all function taking a function which is applied to all element of the streams are suffixed by:

  • _s when the function returns a thread and calls are serialised
  • _p when the function returns a thread and calls are parallelised



Construction


val from : (unit -> 'a option Lwt.t) -> 'a t

from f creates an stream from the given input function. f is called each time more input is needed, and the stream ends when f returns None.

val create : unit -> 'a t * ('a option -> unit)

create () returns a new stream and a push function

val of_list : 'a list -> 'a t

of_list l creates a stream returning all elements of l

val of_array : 'a array -> 'a t

of_array a creates a stream returning all elements of a

val of_string : string -> char t

of_string str creates a stream returning all characters of str

val clone : 'a t -> 'a t

clone st clone the given stream. Operations on each stream will not affect the other.

For example:

# let st1 = Lwt_stream.of_list [1; 2; 3];;
        val st1 : int Lwt_stream.t = <abstr>
        # let st2 = Lwt_stream.clone st1;;
        val st2 : int Lwt_stream.t = <abstr>
        # lwt x = Lwt_stream.next st1;;
        val x : int = 1
        # lwt y = Lwt_stream.next st2;;
        val y : int = 1



Destruction


val to_list : 'a t -> 'a list Lwt.t

Returns the list of elements of the given stream

val to_string : char t -> string Lwt.t

Returns the word composed of all characters of the given stream


Data retreival


exception Empty

Exception raised when trying to retreive data from an empty stream.

val peek : 'a t -> 'a option Lwt.t

peek st returns the first element of the stream, if any, without removing it.

val npeek : int -> 'a t -> 'a list Lwt.t

npeek n st returns at most the first n elements of st, without removing them.

val get : 'a t -> 'a option Lwt.t