From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA19043; Thu, 28 Jun 2001 16:58:46 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 QAA19116 for ; Thu, 28 Jun 2001 16:58:45 +0200 (MET DST) Received: from sundown.cs.cornell.edu (sundown.cs.cornell.edu [128.84.96.20]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f5SEwif15896 for ; Thu, 28 Jun 2001 16:58:44 +0200 (MET DST) Received: from cs.cornell.edu (dhcp99-221.cs.cornell.edu [128.84.99.221]) by sundown.cs.cornell.edu (8.11.3/8.11.3/R-3.5) with ESMTP id f5SEweC07536; Thu, 28 Jun 2001 10:58:40 -0400 (EDT) Message-ID: <3B3B4636.C50C42FB@cs.cornell.edu> Date: Thu, 28 Jun 2001 10:59:02 -0400 From: Frederick Smith Organization: Cornell University X-Mailer: Mozilla 4.73 [en] (Win98; U) X-Accept-Language: en MIME-Version: 1.0 To: Chris Hecker CC: caml-list@inria.fr Subject: Re: [Caml-list] there has to be a better way to write this code References: <200106280300.UAA18698@smtp4-cm.mail.eni.net> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hi Chris, Below is my solution. Its not that different from Judicael Courant's but uses somewhat fewer exceptions. Hope this helps. -Fred type 'a constructive_bool = True of 'a | False let rec find f l = (match l with | [] -> raise Not_found | hd::tl -> (match f hd with True a -> a | _ -> find f tl)) ;; let gmeta f h s = find f (Hashtbl.find_all h s);; let gfloat = gmeta (function Float el -> True el | _ -> False);; let gstring = gmeta (function String el -> True el | _ -> False);; let gvec = gmeta (function Vec el -> True el | _ -> False);; let gbool = gmeta (function Bool el -> True el | _ -> False);; Chris Hecker wrote: > I'm trying to write a little property system, which means I have a > dictionary/hash with strings for keys and a variant of different types > for the data. Then, I define get_float and get_string and the like on > this hash, and the functions either return the type if it exists, or > throw an exception if it doesn't. The attached code below works fine. > > My problem is that the attached code is really pretty verbose and > redundant. Each of the get_* functions has a lot of duplicate code in > it, and I think there must be a better way. I'd like to write > something like this (not really caml syntax, but you get the idea): > > let get_float h s = get_meta h s (Float el -> el) > > but I can't see any way to get that to work. The closest I can come > is this: > > let get_meta h s f = > try > List.iter f (Hashtbl.find_all h s); > raise Not_found > with Match_failure _ -> raise Not_found > > exception Found_float of float > > let get_float h s = > try > get_meta h s (function Float el -> raise (Found_float el)) > with Found_float el -> el > > which isn't much of a savings over the code below, and probably isn't > worth it. (Have I mentioned how annoying that exception at global > scope is? :) > > If I didn't want to have multiple types per key, I could do this: > > let get_float h s = > match Hashtbl.find h s with > Float el -> el > | _ -> raise Not_found > > which is slightly better, but having the multiple types is nice > (especially since Hashtbl supports it). > > Thoughts? > > Chris > > ---------------------------------------- > type data_type = > String of string > | Float of float > | Vec of (float * float) > | Bool of bool > > let h = Hashtbl.create 7 > > let _ = > Hashtbl.add h "foo" (String "foo_string"); > Hashtbl.add h "foof" (Float 3.141); > Hashtbl.add h "bar" (String "bar_string"); > Hashtbl.add h "bar" (Float 2.718); > Hashtbl.add h "vec" (Vec (1.0,2.0)); > Hashtbl.add h "yes" (Bool true); > Hashtbl.add h "no" (Bool false) > > exception Found_float of float > exception Found_string of string > exception Found_vec of (float * float) > exception Found_bool of bool > > let get_float h s = > try > List.iter (function Float el -> raise (Found_float el) | _ -> ()) (Hashtbl.find_all h s); > raise Not_found > with Found_float el -> el > > let get_string h s = > try > List.iter (function String el -> raise (Found_string el) | _ -> ()) (Hashtbl.find_all h s); > raise Not_found > with Found_string el -> el > > let get_vec h s = > try > List.iter (function Vec el -> raise (Found_vec el) | _ -> ()) (Hashtbl.find_all h s); > raise Not_found > with Found_vec el -> el > > let get_bool h s = > try > List.iter (function Bool el -> raise (Found_bool el) | _ -> ()) (Hashtbl.find_all h s); > raise Not_found > with Found_bool el -> el > > ------------------- > Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ > To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr