caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Peng Zang <peng.zang@gmail.com>
To: caml-list@yquem.inria.fr
Cc: "Ludovic Coquelle" <lcoquelle@gmail.com>
Subject: Re: [Caml-list] Getting an element of a hashtable: simple ... or is it?
Date: Fri, 8 Aug 2008 12:01:55 -0400	[thread overview]
Message-ID: <200808081201.57404.peng.zang@gmail.com> (raw)
In-Reply-To: <d6c7b34d0808080846v512be854w2ef4962e814b57f5@mail.gmail.com>

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You can do something similar using options:

let getone h = 
  let res = ref None in
  try
    Hashtbl.iter (fun k v -> res := Some (k,v); raise Exit) h;
    !res
  with Exit -> !res
;;

Peng
  

On Friday 08 August 2008 11:46:41 am Ludovic Coquelle wrote:
> If the type of hashtbl key is known (if the hashtbl module has been
> created by the functor),
> the same code as initial can avoid a reference and use exception
> propagation mechanism:
>
> type key = MyHashtbl.key
> exception One of key
> let get_one h = try (MyHashtbl.iter (fun k _ -> raise (One k)) h;
> raise Not_found) with One x -> x
>
> Can't we have polymorphic exception? like
> 'a exception One of 'a
>
> On Wed, Aug 6, 2008 at 5:47 AM, Brighten Godfrey <pbg@cs.berkeley.edu> 
wrote:
> > On Aug 5, 2008, at 5:25 AM, blue storm wrote:
> >> With Extlib you can use :
> >> let get_one hashtbl = Enum.peek (Hashtbl.enum hashtbl)
> >> val get_one : ('a, 'b) Hashtbl.t -> ('a * 'b) option
> >
> > Ah, thanks.
> >
> > On Aug 5, 2008, at 6:21 AM, Peng Zang wrote:
> >> I think this is pretty standard.  At least, I see it in ExtLib and I do
> >> it on
> >> a regular basis.  In fact I have a function to do this for me so I don't
> >> have
> >> to do it over and over again.  Eg.
> >>
> >>  let get_one ht = mkGetOne Hashtbl.iter ht
> >
> > OK -- so you're saying ExtLib also implements it by breaking out of the
> > loop with an exception.  Interesting.
> >
> > On Aug 5, 2008, at 2:02 PM, Chris Kauffman wrote:
> >> I'm curious what sort of scenario calls for retrieving any single
> >> element of a hash table (which is potentially empty?). It seems most
> >> of the cases I deal with involve simply storing or iterating over all
> >> the elements.
> >
> > Yes, nearly all cases are like that for me too.  But in this case, I want
> > to decompose a graph into its connected components, roughly according to
> > the following pseudocode:
> >
> >    unprocessed_nodes : (node_t, unit) Hashtbl.t = all nodes
> >    while unprocessed_nodes not empty do
> >        let one_node = choose any one node from unprocessed_nodes
> >        let cc = find_connected_component_containing one_node
> >        Do some sort of processing on cc. Then:
> >        for each node v in cc
> >            remove v from unprocessed_nodes
> >        done
> >
> > Thanks,
> > ~Brighten Godfrey
> >
> > _______________________________________________
> > Caml-list mailing list. Subscription management:
> > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> > Archives: http://caml.inria.fr
> > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> > Bug reports: http://caml.inria.fr/bin/caml-bugs
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)

iD8DBQFInG31fIRcEFL/JewRAm62AJ4yJzjMoxl+/uqOWubf90TNLBMeuQCfSktY
GYy2KGFGDXzxT36dHuBE0ks=
=bAf+
-----END PGP SIGNATURE-----


  reply	other threads:[~2008-08-08 16:02 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-05 12:05 Brighten Godfrey
2008-08-05 12:16 ` [Caml-list] " Richard Jones
2008-08-05 12:26   ` Brighten Godfrey
2008-08-05 12:25 ` blue storm
2008-08-05 21:47   ` Brighten Godfrey
2008-08-08 15:46     ` Ludovic Coquelle
2008-08-08 16:01       ` Peng Zang [this message]
2008-08-05 13:21 ` Peng Zang
2008-08-05 21:02   ` Chris Kauffman

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=200808081201.57404.peng.zang@gmail.com \
    --to=peng.zang@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=lcoquelle@gmail.com \
    /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).