From: Richard Jones <rich@annexia.org>
To: John Whitington <john@coherentgraphics.co.uk>
Cc: David Teller <David.Teller@univ-orleans.fr>,
Caml <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] [OSR] Standard syntax extensions ?
Date: Fri, 25 Apr 2008 09:22:38 +0100 [thread overview]
Message-ID: <20080425082238.GA30805@annexia.org> (raw)
In-Reply-To: <9AA053A7-5131-437C-80DF-86B2497B0859@coherentgraphics.co.uk>
On Thu, Apr 24, 2008 at 04:52:50PM +0100, John Whitington wrote:
> On 24 Apr 2008, at 16:49, David Teller wrote:
> >* what kind of syntactic sugar is absolutely missing from the
> >language ?
>
> I'd like a keyword "matches", so I can write
>
> map (matches (0, _, _)) l
>
> rather than
>
> map (function (0, _, _) -> true | _ -> false) l
>
> Had anyone done that or similar in camlp4?
That's a good idea. Attached is a trivial camlp4 implementation which
works to a first approximation. ('when'-clauses don't seem to work
but no doubt would be very simple to fix/add).
Rich.
-------------------------------------------------- pa_matches.ml
(* matches keyword.
* Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* $Id$
*
* Compile with:
* ocamlfind ocamlc -I +camlp4 camlp4lib.cma \
* -pp camlp4of.opt -c pa_matches.ml -o pa_matches.cmo
* Test with:
* ocamlfind ocamlc -pp "camlp4o pa_matches.cmo" test.ml -o test
*)
open Camlp4.PreCast
open Syntax
open Ast
let output_matches _loc patt =
<:expr< function $patt$ -> true | _ -> false >>
EXTEND Gram
GLOBAL: expr;
expr: LEVEL ";" [
[ "matches"; p = patt ->
output_matches _loc p
]
];
END
-------------------------------------------------- test.ml
open Printf
let () =
let xs =
List.filter (matches 7)
[ 1; 2; 3; 4; 5; 6; 7; 7; 7; 8; 9; 10 ] in
printf "result = %s\n" (String.concat ";" (List.map string_of_int xs))
--------------------------------------------------
--
Richard Jones
Red Hat
next prev parent reply other threads:[~2008-04-25 8:22 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-04-24 15:49 David Teller
2008-04-24 15:52 ` [Caml-list] " John Whitington
2008-04-25 8:22 ` Richard Jones [this message]
2008-05-01 7:45 ` Richard Jones
2008-04-24 16:16 ` [Caml-list] [OSR] Standard syntax extensions ? - voting Stefano Zacchiroli
2008-04-25 15:15 ` David Teller
2008-04-26 0:16 ` Stefano Zacchiroli
2008-04-24 16:41 ` [Caml-list] [OSR] Standard syntax extensions ? Martin Jambon
2008-04-24 17:02 ` Jon Harrop
2008-04-24 20:53 ` Berke Durak
2008-04-25 8:24 ` Richard Jones
2008-04-25 16:59 ` Berke Durak
2008-04-25 17:31 ` Gerd Stolpmann
2008-04-25 17:38 ` Richard Jones
2008-04-25 10:33 ` Andrej Bauer
2008-04-25 15:32 ` David Teller
2008-04-24 17:05 ` Dario Teixeira
2008-04-25 13:57 ` Peng Zang
2008-04-25 15:04 ` David Teller
2008-04-25 16:11 ` Peng Zang
2008-04-25 20:37 ` Arthur Chan
2008-04-26 7:41 ` Richard Jones
2008-04-26 7:53 ` Till Crueger
2008-04-26 21:32 ` Arthur Chan
2008-04-27 6:58 ` Andrej Bauer
2008-04-27 14:14 ` Christopher L Conway
2008-04-27 17:41 ` [Caml-list] " David Teller
2008-04-27 17:44 ` David Teller
2008-04-25 22:25 ` Mike Lin
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=20080425082238.GA30805@annexia.org \
--to=rich@annexia.org \
--cc=David.Teller@univ-orleans.fr \
--cc=caml-list@yquem.inria.fr \
--cc=john@coherentgraphics.co.uk \
/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).