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 concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 0FB9FBBB7 for ; Tue, 25 Jul 2006 10:29:45 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id k6P8Ti22016534 for ; Tue, 25 Jul 2006 10:29:44 +0200 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id KAA11364 for ; Tue, 25 Jul 2006 10:29:43 +0200 (MET DST) Received: from mail-outfwd.lms.be (Kaiserslautern1.lms-gmbh.de [213.68.136.230]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id k6P8TheU016492 for ; Tue, 25 Jul 2006 10:29:43 +0200 Received: from localhost (unknown [127.0.0.1]) by mail-outfwd.lms.be (Postfix) with ESMTP id 2ED0C7F43F for ; Tue, 25 Jul 2006 10:49:24 +0200 (CEST) Received: from mail-kl.lmsintl.com ([127.0.0.1]) by localhost (kl-ftp [127.0.0.1]) (amavisd-new, port 20024) with ESMTP id 26585-01 for ; Tue, 25 Jul 2006 10:49:24 +0200 (CEST) Received: from kaiserslautern1.lmsintl.com (unknown [10.2.0.100]) by mail-kl.lmsintl.com (Postfix) with ESMTP id 1B54EB6EC7 for ; Tue, 25 Jul 2006 10:49:24 +0200 (CEST) Received: by kaiserslautern1.lmsintl.com with Internet Mail Service (5.5.2653.19) id <32PZS502>; Tue, 25 Jul 2006 10:29:28 +0200 Message-ID: <26EB47FDD566A7469FC862DAF373792F01711274@kaiserslautern1.lmsintl.com> From: Christoph Bauer To: caml-list Subject: generic Hashtbl.to_array Date: Tue, 25 Jul 2006 10:29:22 +0200 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2653.19) Content-Type: text/plain X-Virus-Scanned: by IT Services X-Miltered: at concorde with ID 44C5D678.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 44C5D677.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; hashtbl:01 hashtbl:01 raise:03 let:03 let:03 christoph:04 christoph:04 problem:05 fold:06 fold:06 fst:06 generic:08 fun:08 fun:08 bauer:09 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.3 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: let to_array t = let a = Array.init make (Hashtbl.length t) ?init? in ignore (Hashtbl.fold (fun k v i -> a.(i) <- (k, v); i + 1) t 0); a The best solution I found is let to_array t = let dummy = Array.init 0 (fun _ -> raise Not_found) in fst (Hashtbl.fold (fun k v (a, i) -> if i = 0 then let a = Array.make (Hashtbl.length t) (k, v) in (a, 0) else (a.(i) <- (k, v); (a, i + 1))) t (dummy, 0)) Is there a better one? Thanks, Christoph Bauer