Bengt Richter wrote:
You can bail out of a generator expression with a raise-StopIteration
expression spelled iter([]).next() ;-)
>>> list(show(x) for x in xrange(20) if x<8 or iter([]).next() or True)
0 1 2 3 4 5 6 7
[0, 1, 2, 3, 4, 5, 6, 7]
This is both neat and incredibly arcane at the same
On Tue, 25 Jan 2005 23:53:26 +1000, Nick Coghlan <[EMAIL PROTECTED]> wrote:
>Peter Otten wrote:
>> Nick Coghlan wrote:
>>
>>
Py> print islice((x for x in xrange(1, 996) if x % 2 == 0), 1, 2).next()
4
>>>
>>>Wouldn't it be nice if this could be spelt:
>>>
>>>print (x for x in xrange(1, 99
Nick Coghlan <[EMAIL PROTECTED]> writes:
[...]
> (xrange can't handle Python longs, unfortunately, so we *are*
> constrained by sys.maxint. However, since my machine only has half a
> gig of RAM, the above is still a damn sight quicker than the
> equivalent list comprehension would be!)
[...]
Othe
Peter Otten wrote:
Nick Coghlan wrote:
Py> print islice((x for x in xrange(1, 996) if x % 2 == 0), 1, 2).next()
4
Wouldn't it be nice if this could be spelt:
print (x for x in xrange(1, 996) if x % 2 == 0)[2]
Well, I just put a patch on SF to enable exactly that:
http://www.python.org/sf/1108272
Nick Coghlan wrote:
>> Py> print islice((x for x in xrange(1, 996) if x % 2 == 0), 1, 2).next()
>> 4
>
> Wouldn't it be nice if this could be spelt:
>
> print (x for x in xrange(1, 996) if x % 2 == 0)[2]
>
> Well, I just put a patch on SF to enable exactly that:
> http://www.python.org/sf/11082
Nick Coghlan wrote:
>> How's this:
>>
>> Py> from itertools import islice
>> Py> print islice((x for x in xrange(1, 996) if x % 2 == 0), 1, 2).next()
>> 4
>
> Wouldn't it be nice if this could be spelt:
>
> print (x for x in xrange(1, 996) if x % 2 == 0)[2]
as I've always said, the sooner we can
Nick Coghlan wrote:
Will Stuyvesant wrote:
The program below creates a stream with the numbers 1..995
and then filters the stream, keeping only the even numbers,
and then prints the second number in the stream (implemented
as the first number of the tail, just like in the 3.5
Section in the Wizard
Just for the record, an implementation without using generators,
somewhat like in Sect. 3.5 of the Wizard book, and without recursion
limit problems.
.
. def stream_hd(s): # the head of the stream
. return s[0]
.
. def stream_tl(s): # the tail of the stream
. return s[1]()
.
. ##
. # The lo
Of course I meant to put a break out of the loop after the print
statement. Duh on me.
--
http://mail.python.org/mailman/listinfo/python-list
On Wed, 19 Jan 2005 23:53:28 +1000, Nick Coghlan <[EMAIL PROTECTED]> wrote:
[...]
>> Something else: this crashes with a "maximum recursion reached"
>> . print stream_enumerate_interval(1,998)
>>
>> while this does not crash
>> . print stream_enumerate_interval(1,900)
>> this means Python has a ma
It took me a while to figure out what the "translated" code was trying
to do. Here's a quick example that I think accomplishes the same
thing:
>>> for i, n in enumerate(x for x in xrange(998) if x % 2 == 0):
... if i == 1:
... print n
...
2
--
http://mail.python.org
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)
Will Stuyvesant wrote:
. def delay(exp): return lambda: exp
If you look at the definition of "delay" in SICP, you'll notice that
it's defined as "syntax sugar", in other words, a macro. Since Python
does not have macros, you'll have to just use "lambda", because by
defining "delay" as a function
Will Stuyvesant wrote:
The program below creates a stream with the numbers 1..995
and then filters the stream, keeping only the even numbers,
and then prints the second number in the stream (implemented
as the first number of the tail, just like in the 3.5
Section in the Wizard book).
How's this:
P
Will Stuyvesant wrote:
Streams are interesting because they are to lists like
xrange is to range. Could save a lot on memory and
computations.
I think you're looking for generators.
Below is my straight translation from Scheme code in the
Wizard book to Python.
Something else: this crashes with a
Here is a question for people who are more comfortable than
I am with new Python stuff like generators.
I am having fun implementing things from the Wizard book
(Abelson, Sussman, "Structure and Interpretation of Computer
Programs") in Python. In chapter 3.5 it is about streams as
delayed lists.
16 matches
Mail list logo