caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <xavier.leroy@inria.fr>
To: Francois Pottier <francois.pottier@inria.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Re: generic programming
Date: Fri, 5 Jul 2002 11:25:51 +0200	[thread overview]
Message-ID: <20020705112551.B16273@pauillac.inria.fr> (raw)
In-Reply-To: <20020705104249.B14853@pauillac.inria.fr>; from francois.pottier@inria.fr on Fri, Jul 05, 2002 at 10:42:49AM +0200

> On Fri, Jul 05, 2002 at 01:18:17AM +1000, John Max Skaller wrote:
> > 
> > Only two data structures in Ocaml readily admit iterators: lists and arrays.
> > Other data structures like hastables, sets, maps, etc, have control
> > inverted iterators, which are much weaker (that is, you have to provide
> > a callback which is given each value in turn

I guessed this is what John had in mind :-)

Actually, if you're desperate, you can derive a C++-style iterator
from a higher-order functional iterator (of the kind provided by the
OCaml standard library) by "inverting the control", using threads or
call/cc.  See example below.

Of course, I'm not advocating that anyone should use this trick in actual
programs.  Like François, I find functional iterators so much cleaner
and better structured.  Moreover, the extra "power" of imperative iterators
isn't obvious to me.  For instance, one could argue that imperative
iterators allow iterating in parallel over several data structures.
This is true, but how useful is iterating over two hashtables in
parallel, knowing that you will *not* scan matching keys at the same time?

- Xavier Leroy

exception End_of_iteration

let hashtbl_enumerator (h, chan) =
  Hashtbl.iter
    (fun key data -> Event.sync (Event.send chan (Some(key, data))))
    h;
  Event.sync (Event.send chan None)

class ['a,'b] hashtbl_iterator (h : ('a, 'b) Hashtbl.t) =
    object
      val chan = Event.new_channel ()
      initializer (ignore (Thread.create hashtbl_enumerator (h, chan)))
      method next_element =
        match Event.sync (Event.receive chan) with
          None -> raise End_of_iteration
        | Some e -> e
    end

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2002-07-05  9:25 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-03  2:49 [Caml-list] " Oleg
2002-07-03  8:37 ` [Caml-list] " Ketanu
2002-07-03 17:29   ` Chris Hecker
2002-07-03 20:07     ` Oleg
2002-07-03 20:34       ` Alessandro Baretta
2002-07-04 15:33         ` John Max Skaller
     [not found]           ` <3D249B27.5080807@baretta.com>
     [not found]             ` <3D25D27B.2020005@ozemail.com.au>
2002-07-07 20:42               ` Alessandro Baretta
2002-07-08  0:59                 ` John Max Skaller
2002-07-08  7:29                   ` Alessandro Baretta
2002-10-15  0:10         ` Eray Ozkural
2002-07-03 21:55     ` Peter Wood
2002-07-04  2:02       ` james woodyatt
2002-07-04 15:18     ` John Max Skaller
2002-07-05  8:42       ` Francois Pottier
2002-07-05  9:25         ` Xavier Leroy [this message]
2002-07-05  9:57           ` Chris Hecker
2002-07-05 13:54             ` Xavier Leroy
2002-07-05 17:59               ` Chris Hecker
2002-07-05 20:31                 ` John Max Skaller
2002-07-05 19:33               ` John Max Skaller
2002-07-05 19:31             ` John Max Skaller
2002-07-05  8:33     ` Francois Pottier
2002-07-05 23:05       ` Dave Berry
2002-07-08  9:54         ` Francois Pottier
2002-07-08 15:49           ` John Max Skaller
2002-08-02 14:49         ` [Caml-list] Streams Diego Olivier Fernandez Pons
2002-08-02 15:29           ` Alain Frisch
2002-08-03 14:19             ` Diego Olivier Fernandez Pons
2002-07-03  8:42 ` [Caml-list] generic programming Johan Baltié
     [not found]   ` <002301c22270$fb4ca160$2be213c3@youngkouzdra>
     [not found]     ` <20020703092753.M39371@wanadoo.fr>
2002-07-05 10:38       ` Anton Moscal
2002-07-03  9:10 ` Jun P.FURUSE

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020705112551.B16273@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=francois.pottier@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).