generator

Where argument of first next() call goes? [duplicate]

两盒软妹~` 提交于 2019-12-17 14:55:26
问题 This question already has answers here : In ES6, what happens to the arguments in the first call to an iterator's `next` method? (3 answers) Closed 3 years ago . I have a simple generator function function *generate(arg) { console.log(arg) for(let i = 0; i < 3;i++) { console.log(yield i); } } Then I init the generator and trying to print values in console: var gen = generate('arg'); //doesn't print gen.next('a'); // prints 'arg' gen.next('b'); // prints 'b' gen.next('c'); // prints 'c' // ...

Python: How to append generator iteration values to a list

跟風遠走 提交于 2019-12-17 14:54:25
问题 I have a simple generator to give me permutations of a set of coordinates. I wish to save each new permutation to an element in an array using the code below: import random def poss_comb(coord): spin=random.shuffle if spin: spin(coord) yield (coord) ... a=[] for n in xrange(0,10): for item in poss_comb(coord): print item a.append(item) However when printing the results printing item gives me what I want : ['0 1', '', '1 2', '1 3'] ['0 1', '', '1 2', '1 3'] ['1 2', '0 1', '1 3', ''] ['0 1', '1

Coroutine vs Continuation vs Generator

三世轮回 提交于 2019-12-17 10:09:41
问题 What is the difference between a coroutine and a continuation and a generator ? 回答1: I'll start with generators, seeing as they're the simplest case. As @zvolkov mentioned, they're functions/objects that can be repeatedly called without returning, but when called will return (yield) a value and then suspend their execution. When they're called again, they will start up from where they last suspended execution and do their thing again. A generator is essentially a cut down (asymmetric)

Is it possible to reset an ECMAScript 6 generator to its initial state?

孤街醉人 提交于 2019-12-17 09:40:21
问题 Given the provided (very simple) generator, is it possible to return the generator back to its original state to use again? var generator = function*() { yield 1; yield 2; yield 3; }; var iterable = generator(); for (let x of iterable) { console.log(x); } // At this point, iterable is consumed. // Is there a method for moving iterable back // to the start point by only without re-calling generator(), // (or possibly by re-calling generator(), only by using prototype // or constructor methods

New state of the art in unlimited generation of Hamming sequence

北城余情 提交于 2019-12-17 07:34:49
问题 (this is exciting!) I know, the subject matter is well known. The state of the art (in Haskell as well as other languages) for efficient generation of unbounded increasing sequence of Hamming numbers, without duplicates and without omissions, has long been the following (AFAIK - and btw it is equivalent to the original Edsger Dijkstra's code too): hamm :: [Integer] hamm = 1 : map (2*) hamm `union` map (3*) hamm `union` map (5*) hamm where union a@(x:xs) b@(y:ys) = case compare x y of LT -> x

Turn functions with a callback into Python generators?

空扰寡人 提交于 2019-12-17 07:24:01
问题 The Scipy minimization function (just to use as an example), has the option of adding a callback function at each step. So I can do something like, def my_callback(x): print x scipy.optimize.fmin(func, x0, callback=my_callback) Is there a way to use the callback function to create a generator version of fmin, so that I could do, for x in my_fmin(func,x0): print x It seems like it might be possible with some combination of yields and sends, but I can quite think of anything. 回答1: As pointed in

Python: using a recursive algorithm as a generator

让人想犯罪 __ 提交于 2019-12-17 07:01:14
问题 Recently I wrote a function to generate certain sequences with nontrivial constraints. The problem came with a natural recursive solution. Now it happens that, even for relatively small input, the sequences are several thousands, thus I would prefer to use my algorithm as a generator instead of using it to fill a list with all the sequences. Here is an example. Suppose we want to compute all the permutations of a string with a recursive function. The following naive algorithm takes an extra

Why does next raise a 'StopIteration', but 'for' do a normal return?

自闭症网瘾萝莉.ら 提交于 2019-12-17 06:34:27
问题 In this piece of code, why does using for result in no StopIteration or is the for loop trapping all exceptions and then silently exiting? In which case, why do we have the extraneous return ?? Or is the raise StopIteration caused by: return None ? #!/usr/bin/python3.1 def countdown(n): print("counting down") while n >= 9: yield n n -= 1 return for x in countdown(10): print(x) c = countdown(10) next(c) next(c) next(c) Assuming StopIteration is being triggered by: return None . When is

How to len(generator()) [duplicate]

一个人想着一个人 提交于 2019-12-17 06:27:50
问题 This question already has answers here : Length of generator output [duplicate] (9 answers) What's the shortest way to count the number of items in a generator/iterator? (6 answers) Closed 6 years ago . Python generators are very useful. They have advantages over functions that return lists. However, you could len(list_returning_function()) . Is there a way to len(generator_function()) ? UPDATE: Of course len(list(generator_function())) would work..... I'm trying to use a generator I've

Unbounded range()

扶醉桌前 提交于 2019-12-17 06:13:03
问题 Is there an unbounded version of range (or xrange for Python 2), or is it necessary to define it manually? For example squares = (x*x for x in range(n)) can only give me a generator for the squares up to (n-1)**2 , and I can't see any obvious way to call range(infinity) so that it just keeps on truckin'. 回答1: You're describing the basic use of itertools.count: import itertools squares = (x*x for x in itertools.count()) 来源: https://stackoverflow.com/questions/7186336/unbounded-range