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.6 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 60B79BBAF for ; Tue, 5 Aug 2008 15:21:42 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AkoBAB7xl0jRVZK1jmdsb2JhbACQfD4BAQEBCQMKBw8Flm2FbQ X-IronPort-AV: E=Sophos;i="4.31,310,1215381600"; d="scan'208";a="13733485" Received: from wa-out-1112.google.com ([209.85.146.181]) by mail2-smtp-roc.national.inria.fr with ESMTP; 05 Aug 2008 15:21:41 +0200 Received: by wa-out-1112.google.com with SMTP id j4so1618260wah.3 for ; Tue, 05 Aug 2008 06:21:40 -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=nUiJki8GljeBFyyr/Oj5TAIFigIMmiaRQK3WBT9i0Yg=; b=fSR/iJByEiBoFQpikAra4Wj7DFGYNg8GbqZeo7T9tMfHJHpWnko+R0zTB+oQItfTGx 2ke4Opm2t40a2SrWMOAOVW2XHVBJXHDS+y6ZukCf9Yb3SGPEkSf/A+aNFY9nmvxeDZrM FUHAla+V7UjMLCB7I4KB3/QhRLgylQWuNoU2E= 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=mcToimxdPvkpog7B13wjRYy+ScVLYUHyhW36B1Xma6hPEqj9kro/GJc4AbcJRGmiXi qokN6HUJBluD/QCzRFBn8fLpPNToEmUZBQogwnyX1Hl7ouWJTqU4SWgtwP5cMRtYX1eN NkA/GgeKRS2LD126LBZeoVFp7tz8zakBXR7hI= Received: by 10.115.48.12 with SMTP id a12mr15452540wak.38.1217942500437; Tue, 05 Aug 2008 06:21:40 -0700 (PDT) Received: from lawn-143-215-204-204.lawn.gatech.edu ( [143.215.204.204]) by mx.google.com with ESMTPS id 5sm2169148yxt.1.2008.08.05.06.21.39 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 05 Aug 2008 06:21:39 -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: Tue, 5 Aug 2008 09:21:35 -0400 User-Agent: KMail/1.9.7 Cc: Brighten Godfrey References: <94C5D65F-7CA4-48FF-B4AA-E88E82F6C8E1@cs.berkeley.edu> In-Reply-To: <94C5D65F-7CA4-48FF-B4AA-E88E82F6C8E1@cs.berkeley.edu> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200808050921.38057.peng.zang@gmail.com> X-Spam: no; 0.00; hashtable:01 hash:01 hashtbl:01 iter:01 hashtable:01 iterating:01 hashtbl:01 iter:01 beginner's:01 ocaml:01 bug:01 peng:98 peng:98 beginners:01 wrote:01 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 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 Peng On Tuesday 05 August 2008 08:05:46 am Brighten Godfrey wrote: > Hi, > > Suppose you are given a data structure, and you want to retrive one > element -- any one element. Sounds simple... and it is, if you have > a list (List.hd list) or an array (arr.(0)). But how about a > hashtable, if we don't know a priori any of the keys in the hashtable? > > The best way I've thought of so far is to begin iterating through all > the hashtable's elements, but then break out with an exception: > > exception Done > let get_one ht = > let el = ref None in > (try ( > Hashtbl.iter (fun i _ -> > el := Some i; > raise Done) > ht; > ) > with Done -> ()); > match !el with > None -> raise Not_found > > | Some x -> x > > But this seems clumsy. Any better ideas? > > 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) iD8DBQFImFPifIRcEFL/JewRAreVAKCOxjyr8uXNIOknO4zmL+i0La4RCQCcDLV1 OXN2V4ZiS8oxC5hQOf5phYU= =ZXwI -----END PGP SIGNATURE-----