caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Erick Tryzelaar <erickt@dslextreme.com>
To: Christoph Bauer <christoph.bauer@lms-gmbh.de>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] generic Hashtbl.to_array
Date: Tue, 25 Jul 2006 02:14:26 -0700	[thread overview]
Message-ID: <44C5E0F2.5000904@dslextreme.com> (raw)
In-Reply-To: <26EB47FDD566A7469FC862DAF373792F01711274@kaiserslautern1.lmsintl.com>

Christoph Bauer wrote:
> Hi,
>
> what is the best way to write Hashtbl.to_array?
>
> Hashtbl.to_array : ('a, 'b) Hashtbl.t -> ('a * 'b) array
>
> The simples idea has the problem, that you don't have
> a initial value to make the result array:

The easiest is to use a temporary list:

# let x = Hashtbl.create 2;;
val x : ('_a, '_b) Hashtbl.t = <abstr>
# Hashtbl.add x 5 3;;
- : unit = ()
# Hashtbl.add x 7 2;;
- : unit = ()
# Array.of_list (Hashtbl.fold (fun a b c -> (a, b) :: c) x []);;
- : (int * int) array = [|(7, 2); (5, 3)|]


You could also try inverting the Hashtbl fold into an iterator+closure 
and pass the closure into the Array.init function, but I'm not sure how 
complicated/efficient that would be.

I suppose it just depends on how efficient you need it to be. If it's 
just some simple stuff, I'd just use the intermediary list.

-e



  reply	other threads:[~2006-07-25  9:32 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-25  8:29 Christoph Bauer
2006-07-25  9:14 ` Erick Tryzelaar [this message]
2006-07-25 11:45 ` [Caml-list] " Damien Doligez
2006-07-25 12:00 Christoph Bauer
2006-07-25 16:19 ` skaller
2006-07-25 12:44 AW: " Christoph Bauer
2006-07-25 15:20 ` Tom
2006-08-15  8:08   ` Stéphane Glondu
2006-07-25 15:53 AW: AW: " Christoph Bauer
2006-07-25 16:35 ` Tom
2006-08-15  8:26   ` Stéphane Glondu
2006-07-26  2:16 oleg
2006-07-26  9:48 ` [Caml-list] " Damien Doligez
2006-07-26 14:41 AW: " Christoph Bauer
2006-07-26 14:53 ` Tom

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=44C5E0F2.5000904@dslextreme.com \
    --to=erickt@dslextreme.com \
    --cc=caml-list@inria.fr \
    --cc=christoph.bauer@lms-gmbh.de \
    /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).