A generator is a generalization of a subroutine, primarily used to simplify the writing of iterators. The yield statement in a generator does not specify a co-routine to jump to, but rather passes a value back to a parent routine.

learn more… | top users | synonyms

-2
votes
0answers
12 views

Python Generate and Execute Statements [on hold]

I have a series of statements that needs to evaluated with every iteration Currently I'm able to print the statements but I'm unable to execute them ...
1
vote
0answers
39 views

Scan over parameter iterator for model simulation

I'm writing a small Python application that performs model simulation for many different parameters. The iterator of parameters isn't completely known ahead of time. Rather, the iterator needs to ...
3
votes
1answer
194 views

Generators in Javascript; Project Euler #2

I'm trying to learn javascript using project euler and I decided to try to force myself to learn about generators in javascript using problem number 2 which asks us to: ...
1
vote
0answers
25 views

Scrabble word finder

I have been working on a Scrabble Word Finder for a couple of days now and have it working pretty much but would love to improve the efficiency. The code is made up of 2 classes: one for creating and ...
9
votes
2answers
1k views

4chan Tripcode Explorer in Python

Background information for those of you who don't know what 4chan tripcodes are: Via Wikipedia: A tripcode is the hashed result of a password that allows one's identity to be recognized without ...
2
votes
2answers
48 views

Split up an iterable into batches

I was writing a program that had a list of e-mail messages. It needed to group them into batches, with the condition that each batch must have at max 20 messages, and be maximally 50k in size (no ...
1
vote
1answer
46 views

Aggregating search results from multiple databases, in smaller work units

I have a large function that performs a search over a data set given a set of parameters. This function goes through multiple databases which all hold data that would comprise one final entry from the ...
3
votes
1answer
110 views

Range iterator in ES6 similar to Python and Ruby for

Background and Purpose For those unaccustomed to Ruby, Ruby supports range literals i.e. 1..4 meaning 1, 2, 3, 4 and ...
3
votes
5answers
122 views

Determining whether a loop iterated at least one element in generator function

I have a case in which I need to determine whether a for loop yielded at least one element: ...
5
votes
1answer
55 views

Using generators to build up a maze

In prepping to teach a workshop on recursion I wrote this code that uses a maze-building algorithm. In doing so I found it really natural to use generators a lot. I feel pretty happy with how the code ...
6
votes
2answers
117 views

Generate all combinations of certain digits

Related to another answer of mine on Project Euler 35, I found the need to calculate all combinations of certain digits, i.e. 1, 3, 7 and 9, below a given n. I ...
8
votes
2answers
263 views

Generating (presumably) unique `nonce` fields for Twitter OAuth

This is code that's part of a library (closed source) that I have that generates a unique nonce value for Twitter OAuth. Essentially, it generates a random number, ...
2
votes
1answer
93 views

Poker deck class /w generator function and list comprehensions

There are multiple aspects in the code I do not really like. [card for card in ...] looks really lame, also ...
4
votes
1answer
106 views

Python exception-raising generator function

I want my generator function to raise as soon as it can. If I make it yield the elements directly, then the KeyError won't happen until iteration begins. Is the ...
12
votes
5answers
401 views

“Heat spot” image generation performance

I wrote a function that generates an image like this. The function is as follows: ...
18
votes
1answer
325 views

Ultimate FizzBuzz

Swift's SequenceType is a useful means of generating a sequence of values, and it makes it particularly useful iterate over these values. I don't really have much ...
2
votes
1answer
79 views

Avoid branching code for degenerate cases in Fibonacci generator

I was writing a simple function that would return the Fibonacci sequence up to the nth term and for whatever reason started wasting a lot of time on it. Here's what I came up with: ...
4
votes
2answers
132 views

Memoized Prime Generator

Here is my code: ...
2
votes
1answer
84 views

Project Euler #2: sum of even Fibonacci numbers, using generators

I am currently working on Project Euler problem #2 which requires you to print the sum of all even fibonacci numbers below 4 million. I just recently learned about generators and from my ...
1
vote
0answers
27 views

99 bottles of beer on the wall + EcmaScript 6

Today, a solution written in Python was posted to this challenge, and since I was inspired by it, I made a version in EcmaScript 6. I tried to explore some new language constructs: arrow functions ...
0
votes
1answer
100 views

Portable bitfields code generator

I am working on a project that involves quite a lot of instantiated objects, with tons of properties, most of which have only a few possible values, so the prospect of saving on memory is quite ...
18
votes
2answers
766 views

A header-only linear-time C++11 PEG parser generator supporting left-recursion and grammar ambiguity

I've rewritten my original parser generator to a header-only library which uses templates and functionals for better type safety and clarity. The generated parser creates an abstract syntax tree which ...
4
votes
1answer
124 views

Sieve prime generator C++

I had some issues with the behavior of the iterator in my prime generator: ...
7
votes
4answers
152 views

Calculating Fibonnaci sequence lazily

Implementation: ...
0
votes
1answer
317 views

Speeding up Mersenne prime generator

I am writing a program to calculate Mersenne primes. This code works, but it is so slow: ...
1
vote
0answers
99 views

A class with a function pointer instead of a generator

I am building TiKZ files, one pdf for each image I have. The purpose is to add text to each separately. The images are legion, so I created a script to generate the text files instead of generating ...
7
votes
2answers
308 views

Wrapping my head around generators

I am going on with refactoring my code base while migrating from Python 2 to Python 3. I am using generators to make reusable components this time as I am more comfortable with them this time then ...
5
votes
3answers
797 views

Optimization for SQLite result set parsing

I am retrieving information from an SQLite database that gives me back around 20 million rows that I need to process. This information is then transformed into a dict of lists which I need to use. I ...
3
votes
2answers
814 views

Searching a 2D matrix for a consecutive sequence

I've been solving problems on checkio (and trying to digest other's code) in order to improve my Python. My main goals are to learn to write more idiomatic Python and to explore the language more ...
2
votes
1answer
309 views

Is it feasible to rewrite this code using list comprehension or a generator function?

I wrote the following code to parse some weak ciphers out of an nmap.xml report. I am wondering if there is a more elegant way to write this code in order to avoid the nested for loops with the double ...
0
votes
1answer
931 views

Filtering a long list of files through a set of ignore patterns using iterators

I have a backup job that walks across a huge directory with potentially millions of files. Python's os.walk() works just fine. Now, I have implemented a feature to ignore files based in a black list ...
4
votes
1answer
711 views

Subscriptable/Indexable generator

I'm not a Python developper, but I enjoy programming with it, and for a project I wanted to have generators that I can easily index. Using python's slice model is obviously the way to go, and here's ...
1
vote
1answer
430 views

DFS algorithm with generators

Background: I was working on a project were I needed to write some rules for text-processing. After working on this project for a couple of days and implementing some rules, I realized I needed to ...
2
votes
3answers
23k views

Random String generator in C

I created this small function just to practice C code. It's a simple random string generator. ...
0
votes
2answers
1k views

Generator to Tuple to List?

I have a method that takes a generator, converts it into a tuple to sort it, then into a list. It works perfectly, but being as I am fairly new to the Python world, I keep asking myself if it is worth ...
2
votes
1answer
497 views

Efficiency of Recursive Checkers Legal Move Generator

I'm implementing a checkers engine for a scientific experiment. I found out through profiling that this is one of the functions that takes up a lot of time. I'm not looking for an in-depth analysis, ...
2
votes
1answer
379 views

Reading from a file and connect all data in one big data than to use generators

Is there better way to read from a file and connect all data in one big data than to use generators? At the moment, I do the following: use generators to read data from files. use NumPy to pack all ...
2
votes
1answer
635 views

Discrete random variable generator

There is my SSCCE to generate a value of discrete random variable. values is set of value the RV can take and procents is ...
4
votes
1answer
878 views

Iterator and Generator versions of Python's range()

I have created iterator and generator versions of Python's range(): Generator version ...
1
vote
1answer
494 views

Objective-C CF Random Name Generator

Based on a python name generator grammar I found here I've decided to write my own in objective-C. The idea is I can load the grammar from a plist file and generate random names from it. I'm looking ...
3
votes
3answers
391 views

Simple randomization program

I'm a few months into learning C++ programming and I want to know if I'm moving generally in the right direction or not with the following code. This is the most advanced thing I've created so far, ...
3
votes
2answers
397 views

Text parser implemented as a generator

I often need to parse tab-separated text (usually from a huge file) into records. I wrote a generator to do that for me; is there anything that could be improved in it, in terms of performance, ...
2
votes
2answers
190 views

Python prime number generator

I'm trying to get a better grasp on generators in python because I don't use them enough (I think generators are cool). To do so I wrote a generator for prime numbers: ...
5
votes
1answer
436 views

Random Topic Generator

I've written a python module that randomly generates a list of ideas that can be used as the premises for furthur research. For instance, it would be useful in situations where a new thesis topic is ...
2
votes
2answers
507 views

Python generator to produce lattice points on an n-simplex.

Looking for a code review, and hopefully to learn something if someone has a nicer solution. Here's what I wrote: ...
9
votes
4answers
1k views

Generator expressions or yield generators?

This is a revisit to a question already asked here about a year and a half ago. While solving Project Euler's Problem 40 I wrote this: ...
4
votes
2answers
4k views

Python generator function that yields combinations of elements in a sequence sorted by subset order

In Python, itertools.combinations yields combinations of elements in a sequence sorted by lexicographical order. In the course of solving certain math problems, I ...