Version 1.3.0
This is a preliminary version of the documentation. Help us to improve it by filling tickets. We are looking for native english speakers to proof read the documentation. Contact us!
module Lwt_stream:Data streamssig..end
type 'a t 'a_s when the function returns a thread and calls are serialised_p when the function returns a thread and calls are parallelisedval from : (unit -> 'a option Lwt.t) -> 'a tfrom 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 push_stream : unit ->
([ `Data of 'a | `End_of_stream | `Exn of exn ] -> unit) * 'a tpush_stream () returns a new stream and a push functionval of_list : 'a list -> 'a tof_list l creates a stream returns all elements of lval of_string : string -> char tof_string str creates a stream returning all characters of strval of_event : 'a React.event -> 'a tof_event e creates a stream containing all event of eval clone : 'a t -> 'a tclone 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_main.run (Lwt_stream.next st1);; \- : int = 1 # Lwt_main.run (Lwt_stream.next st2);; \- : int = 1 val to_list : 'a t -> 'a list Lwt.tval to_string : char t -> string Lwt.texception Empty
val peek : 'a t -> 'a option Lwt.tpeek st returns the first element of the stream, if any, without removing it.val npeek : int -> 'a t -> 'a list Lwt.tnpeek n st returns at most the first n elements of st, without removing them.val get : 'a t -> 'a option Lwt.tget st remove and returns the first element of the stream, if any.val nget : int -> 'a t -> 'a list Lwt.tnget n st remove and returns at most the first n elements of st.val get_while : ('a -> bool) -> 'a t -> 'a list Lwt.tval get_while_s : ('a -> bool Lwt.t) -> 'a t -> 'a list Lwt.tget_while f st returns the longest prefix of st where all elements satisfy f.val next : 'a t -> 'a Lwt.tnext enum remove and returns the next element of the stream, of fail with Lwt_stream.Empty if the stream is empty.val junk : 'a t -> unit Lwt.tjunk st remove the first element of st.val njunk : int -> 'a t -> unit Lwt.tnjunk n st removes at most the first n elements of the stream.val junk_while : ('a -> bool) -> 'a t -> unit Lwt.tval junk_while_s : ('a -> bool Lwt.t) -> 'a t -> unit Lwt.tjunk_while f st removes all elements at the beginning of the streams which satisfy f.val junk_old : 'a t -> unit Lwt.tjunk_old st removes all elements that are ready to be read without yeilding from st. For example the read_password function of Lwt_read_line use that to junk key previously typed by the user.
val get_available : 'a t -> 'a listget_available l returns all available elements of l without blockingval get_available_up_to : int -> 'a t -> 'a listget_available_up_to l n returns up to n elements of l without blockingval is_empty : 'a t -> bool Lwt.tis_empty enum returns wether the given stream is emptyFor example:
# let st1 = Lwt_stream.of_list [1; 2; 3];; val st1 : int Lwt_stream.t = <abstr> # let st2 = Lwt_stream.map string_of_int st1;; val st2 : string Lwt_stream.t = <abstr> # Lwt_main.run (Lwt_stream.next st1);; \- : int = 1 # Lwt_main.run (Lwt_stream.next st2);; \- : string = "2" val choose : 'a t list -> 'a tchoose l creates an stream from a list of streams. The resulting stream will returns elements returned by any stream of l in an unspecified order.val map : ('a -> 'b) -> 'a t -> 'b tval map_s : ('a -> 'b Lwt.t) -> 'a t -> 'b tmap f st maps the value returned by st with fval filter : ('a -> bool) -> 'a t -> 'a tval filter_s : ('a -> bool Lwt.t) -> 'a t -> 'a tfilter f st keeps only value x such that f x is trueval filter_map : ('a -> 'b option) -> 'a t -> 'b tval filter_map_s : ('a -> 'b option Lwt.t) -> 'a t -> 'b tfilter_map f st filter and map st at the same timeval map_list : ('a -> 'b list) -> 'a t -> 'b tval map_list_s : ('a -> 'b list Lwt.t) -> 'a t -> 'b tmap_list f st applies f on each element of st and flattens the lists returnedval fold : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b Lwt.tval fold_s : ('a -> 'b -> 'b Lwt.t) -> 'a t -> 'b -> 'b Lwt.tfold f s x fold_like function for streams.val iter : ('a -> unit) -> 'a t -> unit Lwt.tval iter_p : ('a -> unit Lwt.t) -> 'a t -> unit Lwt.tval iter_s : ('a -> unit Lwt.t) -> 'a t -> unit Lwt.titer f s iterates over all elements of the streamval find : ('a -> bool) -> 'a t -> 'a option Lwt.tval find_s : ('a -> bool Lwt.t) -> 'a t -> 'a option Lwt.tfind f s find an element in a stream.val find_map : ('a -> 'b option) -> 'a t -> 'b option Lwt.tval find_map_s : ('a -> 'b option Lwt.t) -> 'a t -> 'b option Lwt.tfind f s find and map at the same time.val combine : 'a t -> 'b t -> ('a * 'b) tcombine s1 s2 combine two streams. The stream will ends when the first stream ends.val append : 'a t -> 'a t -> 'a tappend s1 s2 returns a stream which returns all elements of s1, then all elements of s2val concat : 'a t t -> 'a tconcat st returns the concatenation of all streams of st.val flatten : 'a list t -> 'a tflatten st = map_list (fun l -> l) stval parse : 'a t -> ('a t -> 'b Lwt.t) -> 'b Lwt.tparse st f parses st with f. If f raise an exception, st is restored to its previous state.val hexdump : char t -> string thexdump byte_stream returns a stream which is the same as the output of hexdump -C. Basically, here is a simple implementation of hexdump -C:
open Lwt open Lwt_io let () = Lwt_main.run (write_lines stdout (Lwt_stream.hexdump (read_lines stdin)))