Documentation
containers.advanced lib
CCMonadIO
Module
IO MonadA simple abstraction over blocking IO, with strict evaluation. This is in no way an alternative to Lwt/Async if you need concurrency.
Examples:
obtain the list of lines of a file: let l = CCIO.((with_in "/tmp/some_file" >>>= read_lines) |> run_exn);;
transfer one file into another: # let a = CCIO.(
with_in "input" >>>= fun ic ->
with_out ~flags:[Open_creat] "output" >>>= fun oc ->
Seq.chunks 512 ic
|> Seq.output oc
) ;;
# run a;;
A value of type 'a with_finalizer
is similar to a value 'a t
but also contains a finalizer that must be run to cleanup. See (>>>=)
to get rid of it.
type 'a or_error = [
| `Ok of 'a
| `Error of string
]
val (>>=) : 'a t -> ('a -> 'b t ) -> 'b t
Wait for the result of an action, then use a function to build a new action and execute it
val repeat : int -> 'a t -> 'a list t
Repeat an IO action as many times as required
val repeat' : int -> 'a t -> unit t
Same as repeat
, but ignores the result
val map : ('a -> 'b ) -> 'a t -> 'b t
val (>|=) : 'a t -> ('a -> 'b ) -> 'b t
val bind : ?finalize :unit t -> ('a -> 'b t ) -> 'a t -> 'b t
bind f a
runs the action a
and applies f
to its result to obtain a new action. It then behaves exactly like this new action.
val (<*>) : ('a -> 'b ) t -> 'a t -> 'b t
val lift : ('a -> 'b ) -> 'a t -> 'b t
val lift2 : ('a -> 'b -> 'c ) -> 'a t -> 'b t -> 'c t
val lift3 : ('a -> 'b -> 'c -> 'd ) -> 'a t -> 'b t -> 'c t -> 'd t
val sequence : 'a t list -> 'a list t
Runs operations one by one and gather their results
val sequence_map : ('a -> 'b t ) -> 'a list -> 'b list t
val fail : string -> 'a t
fail msg
fails with the given message. Running the IO value will return an `Error
variant
FinalizersSame as (>>=)
, but taking the finalizer into account. Once this IO value is done executing, the finalizer is executed and the resource, fred.
Runningexception IO_error of string
Unsafe version of run
. It assumes non-failure.
val register_printer : (exn -> string option ) -> unit
register_printer p
register p
as a possible failure printer. If run a
raises an exception e
, p e
is evaluated. If p e = Some msg
then the error message will be msg
, otherwise other printers will be tried
Standard Wrappersval with_in :
?mode :int ->
?flags :Pervasives .open_flag list ->
string ->
Pervasives .in_channel with_finalizer
Open an input file with the given optional flag list. It yields a in_channel
with a finalizer attached. See (>>>=)
to use it.
val read : Pervasives .in_channel -> Bytes .t -> int -> int -> int t
Read a chunk into the given string
val read_line : Pervasives .in_channel -> string option t
Read a line from the channel. Returns None
if the input is terminated.
val read_lines : Pervasives .in_channel -> string list t
val read_all : Pervasives .in_channel -> string t
Read the whole channel into a buffer, then converted into a string
Outputval with_out :
?mode :int ->
?flags :Pervasives .open_flag list ->
string ->
Pervasives .out_channel with_finalizer
Same as with_in
but for an output channel
val with_out_a :
?mode :int ->
?flags :Pervasives .open_flag list ->
string ->
Pervasives .out_channel with_finalizer
Similar to with_out
but with the Open_append
and Open_creat
flags activated
val write : Pervasives .out_channel -> string -> int -> int -> unit t
val write_str : Pervasives .out_channel -> string -> unit t
val write_buf : Pervasives .out_channel -> Buffer .t -> unit t
val write_line : Pervasives .out_channel -> string -> unit t
val flush : Pervasives .out_channel -> unit t
StreamsIterators on chunks of bytes, or lines, or any other value using combinators. Those iterators are usable only once, because their source might be usable only once (think of a socket)
File and file namesHow to list recursively files in a directory:
CCIO.(
File.read_dir ~recurse:true (File.make "/tmp")
>>= Seq.output ~sep:"\n" stdout
) |> CCIO.run_exn ;;
See File.walk
if you also need to list directories.
module File : sig ... end