caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Lwt idioms: iterating
@ 2013-01-23  5:02 Ivan Gotovchits
  2013-01-23 10:15 ` Raphael Proust
  0 siblings, 1 reply; 2+ messages in thread
From: Ivan Gotovchits @ 2013-01-23  5:02 UTC (permalink / raw)
  To: caml-list


What is a best practice to implement a common iterative operations with
Lwt?

For example, I have a data structure incapsulating some
collection. Interface of the structure includes some iterators, i.e.

  val iter (e -> unit) -> t -> unit

  
Now, I would like to output each element of type e using one of the
functions from Lwt_io module. 

I see two solutions and neither I like:

1. Use Buffer.t to accumulate text while iterating, then output it.

2. Use Lwt_stream.t for the same purpose.

Both solutions a rather imperative and too ad-hoc (not scales to other
iterators and tasks). 

Are there any idiomatic solutions for such common tasks in Lwt?

-- 
         (__) 
         (oo) 
   /------\/ 
  / |    ||   
 *  /\---/\ 
    ~~   ~~   
...."Have you mooed today?"...

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Lwt idioms: iterating
  2013-01-23  5:02 [Caml-list] Lwt idioms: iterating Ivan Gotovchits
@ 2013-01-23 10:15 ` Raphael Proust
  0 siblings, 0 replies; 2+ messages in thread
From: Raphael Proust @ 2013-01-23 10:15 UTC (permalink / raw)
  To: Ivan Gotovchits; +Cc: caml-list

On Wed, Jan 23, 2013 at 5:02 AM, Ivan Gotovchits <ivg@ieee.org> wrote:
>
> What is a best practice to implement a common iterative operations with
> Lwt?
>
> For example, I have a data structure incapsulating some
> collection. Interface of the structure includes some iterators, i.e.
>
>   val iter (e -> unit) -> t -> unit
>
>
> Now, I would like to output each element of type e using one of the
> functions from Lwt_io module.
>
> I see two solutions and neither I like:
>
> 1. Use Buffer.t to accumulate text while iterating, then output it.
>
> 2. Use Lwt_stream.t for the same purpose.

3. Just write it directly and trust the Lwt scheduler to flush the write buffer.

The page https://ocsigen.org/lwt/api/Lwt_io explains that writes are
automatically flushed ``when there is nothing else to do''

~~~~~~~
If you are familiar with buffered channels you may be familiar too
with the flush operation. Note that byte channles of this
modules are automatically flushed when there is nothing else to do
(i.e. before the program goes into idle), so this means that you
no longer have to write:

eprintf "log message\n";
      flush stderr;

to have you messages displayed.
~~~~~~~

>
> Both solutions a rather imperative and too ad-hoc (not scales to other
> iterators and tasks).
>
> Are there any idiomatic solutions for such common tasks in Lwt?
>
> --
>          (__)
>          (oo)
>    /------\/
>   / |    ||
>  *  /\---/\
>     ~~   ~~
> ...."Have you mooed today?"...
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs



-- 
______________
Raphaël Proust

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-01-23 10:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-23  5:02 [Caml-list] Lwt idioms: iterating Ivan Gotovchits
2013-01-23 10:15 ` Raphael Proust

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).