From: Peng Zang <peng.zang@gmail.com>
To: caml-list@yquem.inria.fr
Cc: Brighten Godfrey <pbg@cs.berkeley.edu>
Subject: Re: [Caml-list] Getting an element of a hashtable: simple ... or is it?
Date: Tue, 5 Aug 2008 09:21:35 -0400 [thread overview]
Message-ID: <200808050921.38057.peng.zang@gmail.com> (raw)
In-Reply-To: <94C5D65F-7CA4-48FF-B4AA-E88E82F6C8E1@cs.berkeley.edu>
-----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-----
next prev parent reply other threads:[~2008-08-05 13:21 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
2008-08-05 13:21 ` Peng Zang [this message]
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=200808050921.38057.peng.zang@gmail.com \
--to=peng.zang@gmail.com \
--cc=caml-list@yquem.inria.fr \
--cc=pbg@cs.berkeley.edu \
/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