#5 Practice Midterm
Originally Posted By: masonw
Generators: Generators are used to generate sequences one element at a time using the yield construct. The next() method runs to the next yield statement. The following function, f(), uses a generator to give the current iteration of the for loop. "print c.next()" runs through another iteration and prints out the number of the iteration.
def f():
....for i in range(10):
........yield i
c = f()
print c.next()
_____________________________________________________________________
Coroutine: A coroutine receives a sequence of values one at a time and computes something. The following code uses a coroutine function to attempt to match the text passed as the function parameter to text that is sent to the coroutine function using the send() function. We create a print_match instance p with the text "hello". "p.next()" runs up to and including the "line = (yield)" statement. "p.send("some words here")" sends the line to the coroutine. If any of this line of text matches with the text passed to the function when it was declared, then the function prints the line. If not, then nothing is printed.
def print_matches(text):
....print "Trying to find", text
....while True:
.........line = (yield)
.........if text in line:
.............print line
p = print_matches("hello")
p.next()
p.send("no") #doesn't print anything
p.send("hello there world") #prints hello there world
_____________________________________________________________________
Lambda: Lambda is used to create anonymous functions which are often used to write "one-off" functions like callback handlers in GUI's and can be used to test features without defining a function. The following function adds 3 to the number passed as a parameter
f = lambda x: x + 3
f(4)
by: Mason Winner, Thomas Nystrand, Kristian Andreasen
'''Originally Posted By: masonw'''
Generators: Generators are used to generate sequences one element at a time using the yield construct. The next() method runs to the next yield statement. The following function, f(), uses a generator to give the current iteration of the for loop. "print c.next()" runs through another iteration and prints out the number of the iteration.<br><br>def f():<br>....for i in range(10):<br>........yield i<br>c = f()<br>print c.next()<br>_____________________________________________________________________<br><br>Coroutine: A coroutine receives a sequence of values one at a time and computes something. The following code uses a coroutine function to attempt to match the text passed as the function parameter to text that is sent to the coroutine function using the send() function. We create a print_match instance p with the text "hello". "p.next()" runs up to and including the "line = (yield)" statement. "p.send("some words here")" sends the line to the coroutine. If any of this line of text matches with the text passed to the function when it was declared, then the function prints the line. If not, then nothing is printed.<br><br>def print_matches(text):<br>....print "Trying to find", text<br>....while True:<br>.........line = (yield)<br>.........if text in line:<br> .............print line<br>p = print_matches("hello")<br>p.next()<br>p.send("no") #doesn't print anything<br>p.send("hello there world") #prints hello there world<br>_____________________________________________________________________<br><br>Lambda: Lambda is used to create anonymous functions which are often used to write "one-off" functions like callback handlers in GUI's and can be used to test features without defining a function. The following function adds 3 to the number passed as a parameter<br><br>f = lambda x: x + 3<br>f(4)<br><br><br>by: Mason Winner, Thomas Nystrand, Kristian Andreasen