From: Frederick Smith <fms@cs.cornell.edu>
To: Chris Hecker <checker@d6.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] there has to be a better way to write this code
Date: Thu, 28 Jun 2001 10:59:02 -0400 [thread overview]
Message-ID: <3B3B4636.C50C42FB@cs.cornell.edu> (raw)
In-Reply-To: <200106280300.UAA18698@smtp4-cm.mail.eni.net>
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
next prev parent reply other threads:[~2001-06-28 14:58 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-06-28 3:00 Chris Hecker
2001-06-28 6:47 ` Judicaël Courant
2001-06-28 14:59 ` Frederick Smith [this message]
2001-06-29 7:53 ` Chris Hecker
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3B3B4636.C50C42FB@cs.cornell.edu \
--to=fms@cs.cornell.edu \
--cc=caml-list@inria.fr \
--cc=checker@d6.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox