Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: prevost@maya.com
Cc: caml-list@inria.fr
Subject: Re: Labels and operators
Date: Thu, 22 Jun 2000 14:19:57 +0900	[thread overview]
Message-ID: <20000622141957S.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: Your message of "19 Jun 2000 00:14:02 -0400" <87bt0y71f9.fsf@isil.localdomain>

From: John Prevost <prevost@maya.com>

> A friend of mine recently said "if ML had regexp stuff that was as
> convenient as Perl, I'd switch to it for everything", and mentioned =~
> as something he specifically wanted.  So, as I was walking home
> tonight, I thought "hey, I bet I could make some little operators for
> the PCRE library and show him!"
> 
> But, it also occurred to me that you want to use the nice labelled
> optional argument stuff, and I wasn't sure you could do that with
> operators.  Here's what I've discovered.

> The only solution I can think of is something like:
> 
> # let re ?iflags ?flags ?rex ?pos pat = (iflags, flags, rex, Some pat, pos)
> val re :
>   ?iflags:'a -> ?flags:'b -> ?rex:'c -> ?pos:'d -> 'e ->
>     'a option * 'b option * 'c option * 'e option * 'd option =
>   <fun>
> # let (=~) s (iflags,flags,rex,pat,pos) =
>                    pmatch ?iflags ?flags ?rex ?pat ?pos;;
> val ( =~ ) :
>   string ->
>   Pcre.irflag option * Pcre.rflag list option * Pcre.regexp option *
>   string option * int option -> bool = <fun>
> # "foo" =~ re "f";;
> - : bool = true
> # "foo" =~ re "f" ~pos:1;;
> - : bool = false
> 
> Which, well, works, but seems kind of nasty.

In fact you have a more general solution than that.
What you are trying to do here is just reverse application:

# let (>>) x f = f x;;
val ( >> ) : 'a -> ('a -> 'b) -> 'b = <fun>
# let sub ?(pos=0) ?len s =
    let len = match len with None -> String.length s - pos | Some x -> x in
    String.sub s ~pos ~len;;
val sub : ?pos:int -> ?len:int -> string -> string = <fun>
# "hello" >> sub ~pos:3;;
- : string = "lo"
# "hello" >> sub ~len:3;;
- : string = "hel"

And, with Pcre (artificial since I don't have it installed),

# "foo" >>pmatch "f";;
- : bool = true
# "foo" >>pmatch "f" ~pos:1;;
- : bool = false

That's not exactly the Perl operator, but it feels a lot like it.

> Well, okay, I think it might be reasonable to change the syntax to
> allow this syntax:
> 
> <expr1> <op> <labelled args> <expr2>
> 
> since the labelled args could not in any way shape or form be thought
> to go with either expr1 or expr2.  This would lead to things like:
> 
> # "foo" =~ ~pos:1 "f";;
> - : bool = false
> 
> being possible.  Don't know whether it's a great idea, though.

This is technically possible, but this might be confusing:
<expr2> itself may be a complex expression without parenthesis.

# "foo" =~ ~pos:1 String.sub "foo" 0 1 ^ "bar";;

You would need a bit of habit to parse that.

        Jacques



  parent reply	other threads:[~2000-06-22 17:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-06-19  4:14 John Prevost
2000-06-22  1:48 ` Ken Wakita
2000-06-22  5:19 ` Jacques Garrigue [this message]
2000-06-24 14:44 ` Markus Mottl
2000-06-24 18:03   ` John Prevost

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=20000622141957S.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=prevost@maya.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