Yield

M

Mathias Panzenboeck

Fredrik said:
the main advantage is that it lets you decouple the generation of data
from the use of data; instead of inlining calculations, or using a pre-
defined operation on their result (e.g printing them or adding them to a
container), you can simply yield them, and let the user use whatever
mechanism she wants to process them. i.e. instead of

for item in range:
calculate result
print result

you'll write

def generate():
for item in range:
yield result

and can then use

for item in generate():
print item

or

list(process(s) for s in generate())

or

sys.stdout.writelines(generate())

or

sum(generate())

etc, without having to change the generator.

you can also do various tricks with "endless" generators, such as the
following pi digit generator, based on an algorithm by Python's grand-
father Lambert Meertens:

def pi():
k, a, b, a1, b1 = 2, 4, 1, 12, 4
while 1:
# Next approximation
p, q, k = k*k, 2*k+1, k+1
a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
# Yield common digits
d, d1 = a/b, a1/b1
while d == d1:
yield str(d)
a, a1 = 10*(a%b), 10*(a1%b1)
d, d1 = a/b, a1/b1

import sys
sys.stdout.writelines(pi())

</F>

or fibonacci:

def fib():
a, b = 0, 1
while True:
yield a
a, b = b, a+b

now get the first 10 ones:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]


or primes:

def primes():
yield 1
yield 2

p = 3
psofar = []

smallerPrimes = lambda: takewhile(lambda x: x+x <= p,psofar)

while True:
if all(p % n != 0 for n in smallerPrimes()):
psofar.append(p)
yield p

p += 2

etc.
 
M

Mikael Olofsson

Mathias said:
def primes():
yield 1
yield 2
[snip rest of code]

Hmm... 1 is not a prime. See for instance

http://en.wikipedia.org/wiki/Prime_number

The definition given there is "In mathematics </wiki/Mathematics>, a
*prime number* (or a *prime*) is a natural number </wiki/Natural_number>
that has exactly two (distinct) natural number divisors
</wiki/Divisor>." The important part of the statement is "exactly
two...divisors", which rules out the number 1.

/MiO
 
M

Mikael Olofsson

I said:
The definition given there is "In mathematics </wiki/Mathematics>, a
*prime number* (or a *prime*) is a natural number
</wiki/Natural_number> that has exactly two (distinct) natural number
divisors </wiki/Divisor>." The important part of the statement is
"exactly two...divisors", which rules out the number 1.

Or should I say: Thunderbird made me write:... Those freakin
</wiki/Mathematics> and </wiki/Natural_number> was not visible before I
posted the thing. &%#&%#

/MiO
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top