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=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 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 78F00BC6B for ; Sat, 30 Jun 2007 10:19:48 +0200 (CEST) Received: from ns.graphicgalerie.com (ns.graphicgalerie.com [88.191.26.11]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l5U8JluR003804 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Sat, 30 Jun 2007 10:19:48 +0200 Received: from polo.concept-micro.com (ABordeaux-103-1-3-239.w80-15.abo.wanadoo.fr [80.15.128.239]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ns.graphicgalerie.com (Postfix) with ESMTP id A8E01BB4378 for ; Sat, 30 Jun 2007 10:19:40 +0200 (CEST) Date: Sat, 30 Jun 2007 10:19:37 +0200 From: Pierre =?ISO-8859-15?B?RXRjaGVtYe906Q==?= To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] The Implicit Accumulator: a design pattern using optional arguments Message-Id: <20070630101937.a95e7e6c.petchema@concept-micro.com> In-Reply-To: <200706271453.06045.jon@ffconsultancy.com> References: <200706271314.35134.jon@ffconsultancy.com> <1A1D6F56-B3DB-4552-969C-9859482175AC@lrde.epita.fr> <200706271453.06045.jon@ffconsultancy.com> Organization: Concept Micro 33 SARL X-Mailer: Sylpheed version 2.3.0beta5 (GTK+ 2.8.20; i486-pc-linux-gnu) X-TRP: honey@concept-micro.com X-Face: "1 a écrit : > > # let symbol = > let m = Hashtbl.create 1 in > fun string -> > try Hashtbl.find m string with Not_found -> > Hashtbl.add m string string; > string;; > val symbol : '_a -> '_a = The usual way to do this in OCaml is using a weak hashtable. Benefit is that the strings can still be collected when not longer used. Weak hashtables also have a "merge" function that do exactly what we want: module H = Weak.Make(struct type t = string let hash = Hashtbl.hash let equal (x:t) y = x = y end) let symbol = let symbols = H.create 13 in H.merge symbols