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 JAA29557; Fri, 29 Jun 2001 09:54:16 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 JAA29614 for ; Fri, 29 Jun 2001 09:54:16 +0200 (MET DST) Received: from snfc21.pbi.net (mta5.snfc21.pbi.net [206.13.28.241]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f5T7sFj00268 for ; Fri, 29 Jun 2001 09:54:15 +0200 (MET DST) Received: from checkerlap.d6.com ([64.163.212.190]) by mta5.snfc21.pbi.net (iPlanet Messaging Server 5.1 (built May 7 2001)) with ESMTP id <0GFO004B0LYBR8@mta5.snfc21.pbi.net> for caml-list@inria.fr; Fri, 29 Jun 2001 00:54:12 -0700 (PDT) Date: Fri, 29 Jun 2001 00:53:27 -0700 From: Chris Hecker Subject: Re: [Caml-list] there has to be a better way to write this code In-reply-to: <3B3B4636.C50C42FB@cs.cornell.edu> X-Sender: def6@shell16.ba.best.com To: Frederick Smith Cc: caml-list@inria.fr Message-id: <4.3.2.7.2.20010629004619.0277ebe0@shell16.ba.best.com> MIME-version: 1.0 X-Mailer: QUALCOMM Windows Eudora Version 4.3.2 Content-type: text/plain; charset=us-ascii Content-transfer-encoding: 7BIT References: <200106280300.UAA18698@smtp4-cm.mail.eni.net> Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk >Below is my solution. Its not that different from Judicael Courant's but uses somewhat fewer exceptions. Ah, for some reason it didn't cross my mind to define my own find function! I ended up with a hybrid of your two solutions. I can't decide if the non-exhaustive pattern warning is worse than having to type the 'a option (or your constructive_bool) cases every time: type data_type = String of string | Float of float | Bool of bool | Vector of float * float let hash_get_meta f h s = let rec find f l = match l with [] -> raise Not_found | hd::tl -> try f hd with Match_failure _ -> find f tl in find f (Hashtbl.find_all h s) let hash_get_string = hash_get_meta (function String el -> el) let hash_get_bool = hash_get_meta (function Bool el -> el) let hash_get_float = hash_get_meta (function Float el -> el) let hash_get_vector = hash_get_meta (function Vector (x,y) -> x,y) That's pretty close to what I wanted (except for the pattern warnings, dare I ask for a pragma to turn it off locally rather than globally via command line?). To get rid of them I'd do what you said, but with 'a option instead of defining a new type: let hash_get_string = hash_get_meta (function String el -> Some el | _ -> None) let hash_get_float = hash_get_meta (function Float el -> Some el | _ -> None) let hash_get_bool = hash_get_meta (function Bool el -> Some el | _ -> None) let hash_get_vector = hash_get_meta (function Vector (x,y) -> Some (x,y) | _ -> None) Definitely wordier and more redundant, but maybe worth it. Thanks to all who answered! Chris ------------------- 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