From: Judicaël Courant <Judicael.Courant@lri.fr>
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 08:47:09 +0200 [thread overview]
Message-ID: <20010628084709.201e7770.Judicael.Courant@lri.fr> (raw)
In-Reply-To: <200106280300.UAA18698@smtp4-cm.mail.eni.net>
Hi,
On Wed, 27 Jun 2001 20:00:27 -0700
Chris Hecker <checker@d6.com> wrote:
>
> 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)
>
let get_float h s = get_meta h s (fun (Float el) -> el)
is legal syntax, but IMHO
let get_float h s = get_meta h s (fun x -> match x with Float el -> el | _
-> Not_found)
would be a bit cleaner
Now, what about this for get_meta:
let get_meta h s f =
let g x = try ignore (f x); true with _ -> false in
let s = List.find g (Hashtbl.find_all h s) in
f s
;;
Or, if you do not want to apply f twice to its argument, you can rewrite
find to better suit your needs.
The original code is
let rec find p = function
| [] -> raise Not_found
| x :: l -> if p x then x else find p l
I suggest:
let rec find_and_apply p = function
| [] -> raise Not_found
| x :: l -> try p x with _ -> find_and_apply p l
Then, you can write
let get_meta h s f = find_and_apply f (Hashtbl.find_all h s)
Hope this helps...
Judicaël.
--
Judicael.Courant@lri.fr, http://www.lri.fr/~jcourant/
(+33) (0)1 69 15 64 85
-------------------
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 6:47 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 [this message]
2001-06-28 14:59 ` Frederick Smith
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=20010628084709.201e7770.Judicael.Courant@lri.fr \
--to=judicael.courant@lri.fr \
--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