> >>Lambda will be more difficult.  Eric Raymond adapted an anti-gun control
> >>slogan and said "you can pry lambda out of my cold dead hands."  A bunch
> >>of folks will sorely miss the ability to create anonymous functions on
> >>the fly.  When lambda is used for deferred argument evaluation (a la PEP
> >>312), the def syntax is a crummy substitute.

> > Yeah, I'm with you here.  As warty as lambda is, it just is so damn
> > convenient some times.  I've recently been using it as a companion to
> > property(), providing concise definitions of read-only attributes.

I'm with you on lambda:  I've found a few places where losing it would be a
major inconvenience.

One example is in a library I wrote to implement the Snobol4 algorithm for
recursive-descent pattern matching.  Using that algorithm relies heavily on
the ability to specify subexpressions for "deferred evaluation" -- their
values must not be computed until they are actually needed.  For example,
here is a mini-specification for arithmetic expressions in Snobol4:

        id = any(letters) span(letters digits)
        primary = id | '(' *expr ')'
        factor = primary arbno(any("*/") primary)
        expr = factor arbno(any("+-") factor)

This should be fairly straightforward once you realize that "arbno" is like
a regular-expression *, blank is used for concatenation, and | (meaning
"or") binds less tightly than blank.

The whole definition depends on *expr, which is a request for deferred
evaluation of expr.  In other words, it refers to the value of expr when
encountered during matching, rather than the (null) value it has when the
assignment to primary is evaluated.

Through appropriate use of overloading, I have been able to translate this
code into the following Python:

        id = any(letters) + span(letters + digits)
        primary = id | '(' + defer(lambda: expr) + ')'
        factor = primary + arbno(any("*/") + primary)
        expr = factor + arbno(any("+-") + factor)

I do not want to have to rewrite this code as:

        def defer_expr:
                return expr
        id = any(letters) + span(letters + digits)
        primary = id | '(' + defer(defer_expr) + ')'
        factor = primary + arbno(any("*/") + primary)
        expr = factor + arbno(any("+-") + factor)

because I do not want to have to make up a new name, and because in
practice, I've seen patterns in which there are many deferred evaluations,
not just one as in this example.  In other words, I want deferred evaluation
to remain a conceptually inexpensive operation, and lambda is the only way
of doing this.



_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to