delay and force in Python

Will Stuyvesant hwlgw at hotmail.com
Wed Jan 19 12:04:08 EST 2005


Yes you are right, if you just want to carry an expression
around then lambda does it; but delay was not intended as a
top-level function.  Perhaps you think that my silly stream
implementation in the original post builds the whole list,
but it does not:

>>> o = stream_enumerate_interval(11,121)
>>> print o
[11, <function <lambda> at 0x00BA8670>]
>>> f = o[1]
>>> r = f()
>>> print r
[12, <function <lambda> at 0x00BA86B0>]

instead it builds a list of lambda's, and that is so
desirable <wink>


Others suggested generators indeed, and I can imagine now a
usage pattern like

>>> s = stream_generator(initValue, next, stop, other_params)
>>> stream_hd(stream_tl(stream_filter(isEven,s)))

where the idea is to pass a generator to a function all the
time.  What I don't like though is that I then, as I
understand it, have to code some for-loop inside that
function, I try to avoid for-loops.  But a functional form
defined with a for-loop does not seem all that bad.  Anyway
I have some reading-up to do, about things some posters use
and I forgot to keep up with since I have been coding Python
1.5.2 style for ages now: properties, itertools, ...printed
some stuff already, thanks!




More information about the Python-list mailing list