From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.8 required=5.0 tests=AWL,DNS_FROM_RFC_POST, SPF_NEUTRAL autolearn=disabled version=3.1.3 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id D6592BBAF for ; Fri, 8 Aug 2008 18:02:01 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ag4BAFELnEhKfVwZnGdsb2JhbACRAD4BAQEBAQgDCgcRlHeFeg X-IronPort-AV: E=Sophos;i="4.31,327,1215381600"; d="scan'208";a="13834637" Received: from qw-out-2122.google.com ([74.125.92.25]) by mail2-smtp-roc.national.inria.fr with ESMTP; 08 Aug 2008 18:02:01 +0200 Received: by qw-out-2122.google.com with SMTP id 9so460780qwb.15 for ; Fri, 08 Aug 2008 09:02:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:reply-to:to:subject:date :user-agent:cc:references:in-reply-to:content-type :content-transfer-encoding:content-disposition:message-id; bh=UzX7IF47JmUOKLeSQ1NsWxgCkeLrcQncXS4oiK4xQho=; b=C++fLygx0kj/y/a5qnrXDiUyB7nOxn3uqS2hQcfw1wTl8cR19WTH1KP9CiTvIwqJTs ESnwiTa1iUCXW2GAEyIVnzj1/t8kWnQD9u0J9yrrAX7zH1LotdYRR8mexLzUv2AiTOI8 t+5trLKOsJRzeBnXsRfgjSdvWNADNOQEgX3Qo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:reply-to:to:subject:date:user-agent:cc:references:in-reply-to :content-type:content-transfer-encoding:content-disposition :message-id; b=RjC2TLJaKeJwjQLkWHOkOdjoQppBtG9EcqePuXgzm2/NXF7Zyxqne6nHNIL3Mv4f/H /WveZgDHczuhE4iVLE/ahWHWYsmNlBNmhTWaty2dlC/wl0Y1mKQ0S0UyWUyJSGP1POBa uK+ch+E4uMv/tnHRbt+FEOwbiqBpp5evxbHXY= Received: by 10.214.81.21 with SMTP id e21mr3715997qab.61.1218211319921; Fri, 08 Aug 2008 09:01:59 -0700 (PDT) Received: from lawn-143-215-204-204.lawn.gatech.edu ( [143.215.204.204]) by mx.google.com with ESMTPS id 6sm1517934ywn.0.2008.08.08.09.01.58 (version=TLSv1/SSLv3 cipher=RC4-MD5); Fri, 08 Aug 2008 09:01:59 -0700 (PDT) From: Peng Zang Reply-To: peng.zang@gmail.com To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Getting an element of a hashtable: simple ... or is it? Date: Fri, 8 Aug 2008 12:01:55 -0400 User-Agent: KMail/1.9.7 Cc: "Ludovic Coquelle" References: <94C5D65F-7CA4-48FF-B4AA-E88E82F6C8E1@cs.berkeley.edu> In-Reply-To: Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200808081201.57404.peng.zang@gmail.com> X-Spam: no; 0.00; hashtable:01 hash:01 hashtbl:01 iter:01 hashtbl:01 functor:01 iter:01 enum:01 enum:01 val:01 implements:01 hash:01 iterating:01 pseudocode:01 nodes:01 -----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 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-----