caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Chris Hecker <checker@d6.com>
To: Frederick Smith <fms@cs.cornell.edu>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] there has to be a better way to write this code
Date: Fri, 29 Jun 2001 00:53:27 -0700	[thread overview]
Message-ID: <4.3.2.7.2.20010629004619.0277ebe0@shell16.ba.best.com> (raw)
In-Reply-To: <3B3B4636.C50C42FB@cs.cornell.edu>


>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


      reply	other threads:[~2001-06-29  7:54 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
2001-06-29  7:53   ` Chris Hecker [this message]

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=4.3.2.7.2.20010629004619.0277ebe0@shell16.ba.best.com \
    --to=checker@d6.com \
    --cc=caml-list@inria.fr \
    --cc=fms@cs.cornell.edu \
    /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;
as well as URLs for NNTP newsgroup(s).