module CCMonadIO:sig
..end
A simple abstraction over blocking IO, with strict evaluation. This is in
no way an alternative to Lwt/Async if you need concurrency.
Since 0.3.3
let l = CCIO.((with_in "/tmp/some_file" >>>= read_lines) |> run_exn);;
# 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;;
type 'a
t
type'a
io ='a t
type 'a
with_finalizer
'a with_finalizer
is similar to a value 'a t
but
also contains a finalizer that must be run to cleanup.
See CCMonadIO.(>>>=)
to get rid of it.type'a
or_error =[ `Error of string | `Ok of 'a ]
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
val return : 'a -> 'a t
val repeat : int -> 'a t -> 'a list t
val repeat' : int -> 'a t -> unit t
CCMonadIO.repeat
, but ignores the resultval 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.finalize
: an optional action that is always run after evaluating
the whole actionval pure : 'a -> 'a t
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
val lift : ('a -> 'b) -> 'a t -> 'b t
CCMonadIO.map
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
val sequence_map : ('a -> 'b t) -> 'a list -> 'b list t
CCMonadIO.sequence
val fail : string -> 'a t
fail msg
fails with the given message. Running the IO value will
return an `Error
variantval (>>>=) : 'a with_finalizer -> ('a -> 'b t) -> 'b t
CCMonadIO.(>>=)
, but taking the finalizer into account. Once this
IO value is done executing, the finalizer is executed and the resource,
fred.val run : 'a t -> 'a or_error
`Ok x
when x
is the successful result of the
computation, or some `Error "message"
exception IO_error of string
val run_exn : 'a t -> 'a
CCMonadIO.run
. It assumes non-failure.IO_error
if the execution didn't go wellval 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 triedval with_in : ?mode:int ->
?flags:Pervasives.open_flag list ->
string -> Pervasives.in_channel with_finalizer
in_channel
with a finalizer attached. See CCMonadIO.(>>>=)
to
use it.val read : Pervasives.in_channel -> Bytes.t -> int -> int -> int t
val read_line : Pervasives.in_channel -> string option t
None
if the input is terminated.val read_lines : Pervasives.in_channel -> string list t
val read_all : Pervasives.in_channel -> string t
val with_out : ?mode:int ->
?flags:Pervasives.open_flag list ->
string -> Pervasives.out_channel with_finalizer
CCMonadIO.with_in
but for an output channelval with_out_a : ?mode:int ->
?flags:Pervasives.open_flag list ->
string -> Pervasives.out_channel with_finalizer
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
Iterators 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)
module Seq:sig
..end
How 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 CCMonadIO.File.walk
if you also need to list directories.
module File:sig
..end
module Raw:sig
..end