[PEAK] Re: Variable binding and pattern matching
Phillip J. Eby
pje at telecommunity.com
Fri Jan 7 14:29:00 EST 2005
At 07:12 PM 1/7/05 +0000, Paul Moore wrote:
>"Phillip J. Eby" <pje at telecommunity.com> writes:
>
> > @something.when("sum_sq < bar*bar", sum_sq="sum(x*x for x in foo)")
> > def do_something((sum_sq,),self,foo,bar):
> > # etc.
>
>This is the variation that looked immediately obvious to me, even
>though it doesn't make the binding explicit in any way. The problem
>with most of the other variations is that the condition looks like a
>little language of its own, which is almost, but not quite like
>Python (OK, I know that's actually what it is, but there's no benefit
>in rubbing the reader's nose in it :-)) The forms using let...in make
>me think of Haskell, which means I end up with two conflicting sets
>of language instincts trying to interpret the condition...
>
> > So, does anybody have any thoughts on what syntax looks better for each of
> > these features? What syntax is more "obvious"?
>
>That was very much a "gut feeling" reaction based on skimming your
>post. I haven't used generic functions myself yet, so I may have
>missed some important points. Treat it as input from someone who is
>*reading* the code, rather than *writing* it.
Okay, so I'll take that vote as +1 on that particular 'let'
alternative. But which syntax seemed more obvious for pattern
matching? i.e. from these:
expr in Call.match( [isinstance], has(inst=object), has(cls=Const) )
expr in Call.match( [isinstance], bind(inst=object), bind(cls=Const) )
expr in Call.match( [isinstance], as(inst=object), as(cls=Const) )
expr in Call.match( [isinstance], get(inst=object), get(cls=Const) )
or these:
expr in Call.match( [isinstance], vars.inst, vars.cls in Const)
expr in Call.match( [isinstance], vars.inst, vars.cls % Const)
expr in Call.match( [isinstance], vars.inst, vars.cls & Const)
expr in Call.match( [isinstance], vars.inst, vars.cls | Const)
expr in Call.match( [isinstance], vars.inst, vars.cls << Const)
expr in Call.match( [isinstance], vars.inst, Const >> vars.cls)
etc.?
None of these are particularly Haskell-like, AFAIK. OTOH, they're not very
Pythonic either. I really want to be able to write something more like:
expr in Call.match( [isinstance], ?inst, ?cls in const)
But this of course can't be parsed with the Python built-in parser.
More information about the PEAK
mailing list