From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id BACE3BBB7 for ; Tue, 25 Jul 2006 11:32:06 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.6/8.13.6) with ESMTP id k6P9W6r8012247 for ; Tue, 25 Jul 2006 11:32:06 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA11244 for ; Tue, 25 Jul 2006 11:32:05 +0200 (MET DST) Received: from mail5.dslextreme.com (mail5.dslextreme.com [66.51.199.81]) by nez-perce.inria.fr (8.13.6/8.13.6) with SMTP id k6P9W4nN012220 for ; Tue, 25 Jul 2006 11:32:04 +0200 Received: (qmail 23670 invoked from network); 25 Jul 2006 09:14:32 -0000 Received: from unknown (HELO [192.168.1.100]) (erickt@68.183.198.108) by mail5.dslextreme.com with (RC4-MD5 encrypted) SMTP; Tue, 25 Jul 2006 02:14:32 -0700 Message-ID: <44C5E0F2.5000904@dslextreme.com> Date: Tue, 25 Jul 2006 02:14:26 -0700 From: Erick Tryzelaar User-Agent: Thunderbird 1.5.0.4 (Macintosh/20060530) MIME-Version: 1.0 To: Christoph Bauer Cc: caml-list Subject: Re: [Caml-list] generic Hashtbl.to_array References: <26EB47FDD566A7469FC862DAF373792F01711274@kaiserslautern1.lmsintl.com> In-Reply-To: <26EB47FDD566A7469FC862DAF373792F01711274@kaiserslautern1.lmsintl.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 44C5E516.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 44C5E514.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; hashtbl:01 hashtbl:01 val:01 abstr:01 inverting:01 iterator:01 wrote:01 caml-list:01 int:01 int:01 closure:01 closure:01 unit:03 unit:03 let:03 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 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 = # 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