Re: Request for comments: use-cases for delayed evaluation

2018-05-17 Thread dieter
Steven D'Aprano  writes:

> Suppose Python had a mechanism to delay the evaluation of expressions 
> until needed. What would you use it for?

Delayed evaluation is some form of "lazy evaluation": evaluate an
expression (only) at the time, it is needed (maybe for the first time).

The avocates of "lazy evaluation" bring forward that it gives you
a convenient new modularization facility: you can separate the description
for the construction of (potentially huge or even infinite) data structures
from the algorithms operating on those structures. The "lazy evaluation"
ensures that the structure is "unfolded" only so far as a concrete
algorithm requires it.

An example could be a game. Lazy evaluation would allow to describe
once and for all how to compute the tree of game states reachable from
a given game state (usually a huge, maybe even an infinite tree).
Various algorithms for the selection of the next game move could
all work on tree (and would be some form of tree search).

With lazy evaluation, Python might become more attractive in
areas where conceptually huge data structures can be involved: games,
articial intelligence, proving systems, ...

-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Request for comments: use-cases for delayed evaluation

2018-05-17 Thread Dan Strohl via Python-list


I could easily see using all of the examples;  I run into this pretty regularly.

What about something like the following (which, honestly is really a 
combination of other examples).

If I have a function that has multiple parameters, each of which might be 
expensive, but it might break out earlier depending on how each one is 
evaluated... for example:

(pretending that "$" is a flag that says "evaluate later", and $(arg) is how 
you say "evaluate now")

def combine_unless_none(*$args, sep=', '):
""" if any arg evaluates to None, return '', otherwise join"""
for arg in args:
tmp_arg = $(arg)
If tmp_arg is None:
return "
return sep.join(args)

-- 
https://mail.python.org/mailman/listinfo/python-list