Browse thread
[OSR] Standard syntax extensions ?
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2008-04-25 (08:22) |
From: | Richard Jones <rich@a...> |
Subject: | Re: [Caml-list] [OSR] Standard syntax extensions ? |
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