Questions tagged [generator]

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.

1
vote
1answer
39 views

Random String Generation module in Python

The module is for facilitating random string generation. The example I use to explain it is the following: Instead of writing ...
1
vote
0answers
66 views

An implementation of Project Wonderful's Infinite Auction

Project Wonderful is no more. ☹ As a little project, I've been trying to (slowly) re-implement the service. Here's my implementation of the Infinite Auction. The main function is ...
4
votes
1answer
116 views

Generating combinations of specific values of a nested dict

I am developing a framework that allows to specify a machine learning model via a yaml file with different parameters nested so the configuration files are easy to ...
5
votes
3answers
499 views

Python generator for “barely obtuse” triangles (Project Euler 224)

I am trying to solve Project Euler Problem 224: Let us call an integer sided triangle with sides a ≤ b ≤ c barely obtuse if the sides satisfy a2 + b2 = c2 - 1. How many barely obtuse triangles ...
4
votes
3answers
69 views

Take a limited number of items from a potentially infinite generator

Hypothetically, I have a bunch of generators that never end, like: function* progression() { let n = 0; while (true) { yield n++; } } I want to take ...
2
votes
0answers
116 views

One-handed Solitaire Game Choice Generator

I want to test some different endgame scenarios for one-handed Solitaire where the removal of the cards if the suit or value match is optional. I've written a simple generator which yields at the ...
1
vote
0answers
87 views

Merge K sorted Arrays (JavaScript using generators)

Question: Merge k sorted Arrays My main concerns about my code are: Performance: is it optimized and is my description of the time and complexity correct. How should I annotate the space and time ...
1
vote
0answers
66 views

Recursive Generator Exceeds Call Stack

I have this recursive generator that fetches 100 messages from an API and gets the next 100 until there are no more messages to get: ...
-1
votes
1answer
123 views

When to create a generator or return simply a list in Python? [closed]

I have this kind of instance method: ...
0
votes
1answer
4k views

Generating unique alphanumeric ID's of a fixed length

I am needing to generate unique id's for users. The 'unique' part is the problem for me. The 13 character id's which have a format of the following: 0 + random int with range of 1-9 + random letter ...
3
votes
1answer
295 views

Flatten nested iterables

I wrote a program sometime ago that reads csv files, sorts, reorders and groups their columns/rows' content (with itertools) and later labels the groups using a ...
0
votes
1answer
161 views

Python custom generator of object not efficient

Let's consider the following class Signal which defines a multiphasic signal: ...
2
votes
0answers
99 views

Generator coroutine class

Submitted for review ... A co-routine class called generator that co_yields values of a parameterized type. I am interested of course in whether it is bug-free. I ...
3
votes
1answer
145 views

Using generator for buffered read of large file in Python

I have a large file that I need to parse - and since it will be regenerated from external queries every time script runs so there is no way to parse it once and cache the results. I want to save ...
3
votes
0answers
2k views

C# recursive backtracker labyrinth generation & game

So for over a weekend I decided to write something rather simple. For maze generation I've followed this algorithm and aside from that I've made a little game, to go from A to B. Everything works fine ...
1
vote
2answers
167 views

Generator function to enumerate files

I want to implement a function that enumerates all files in directory. I think the function should Return a Promise, because underlying readdir call is prefered ...
1
vote
1answer
78 views

Transforming lists of products to boolean vectors

Task Inputs Lists of products stored in a CSV file, where each line represents the shopping list of a single client: Outputs A CSV file: Header: products names sorted by number of appearances in ...
0
votes
1answer
1k views

Generate a set of combinations

For one of my personal projects, I needed an algorithm to generate a set of combinations. I've finally successfully written one. The problem is that this algorithm uses two recursive calls to ...
1
vote
1answer
60 views

Modelling deadlock in cyclic pipelines in Haskell with coroutine libraries

I'm modelling two processes which have been put into a cyclic pipeline - the output of each feeds into the input of the other - so as to work out when they've deadlocked (i.e., neither can progress ...
4
votes
0answers
123 views

Cached infinite prime generator

For a project I am working on, I need to use a lot of primes, frequently. To do this, I added a cache to Will Ness's prime sieve, that stores already generated primes so getting them is quick. How can ...
6
votes
1answer
2k views

Use generator to do inorder traversal

I am not used to generators and wonder how to make code cleaner/Pythonic in the generator part. The function is about validating a binary search tree. The ...
2
votes
1answer
44 views

Python coroutine structure

I've completed Advent of Code's Day 9 in Python3, trying to use it to teach myself coroutines. I'm very familiar with generators, but I've pretty much never used coroutines before. I've ended up with ...
5
votes
2answers
187 views

Alternative to nonlocal for local variables in a function with nested functions in Python

Introduction for future readers I posted the code below in the hope that it would be a good example to discuss the topic "Alternative to nonlocal for local variables in a function with nested ...
7
votes
1answer
2k views

Tokenizing a file with a generator

I'm parsing (specifically tokenizing) a file, line-by-line. I've a method tokenize, that takes a string (one line of code, it can't take the whole file at once), breaks it into parts, and returns a ...
5
votes
2answers
378 views

Generator iterator with push back function

In a compiler project for an LL(1/2) grammar, I have had a need for a generator iterator with a push back function. I have been surprised not to find any perfectly applicable solution (clean, simple, ...
0
votes
1answer
85 views

Working with cached generator values

I have set of values represented by a class. The class should be initialised with a generator to limit memory use. The class itself is the iterable, having ...
1
vote
0answers
80 views

AsyncLock implementation for JS

I made an AsyncLock implementation due to a comment on Hacker News saying JavaScript was missing such a necessary idea. I honestly am not terribly familiar with Threaded programming and the concept of ...
5
votes
2answers
170 views

Asynchronous JavaScript to fetch remote JSON data with promises and generators

I am trying to figure out how to do async calls using promises and generators (without 3rd party libraries). Most of the examples I found on the Internet are either incomplete or confusing (at least ...
2
votes
2answers
146 views

Random Number Generator Followup: Choosing the Generator Algorithm and the Distribution

This question is a follow-up from my previous code review question. This question regards the ability to choose a predefined random number generator algorithm and also choose a generator distribution. ...
5
votes
2answers
316 views

Simple NBA Draft Order Generator

I wrote a simple program to create a random order for an NBA draft, prompt the user for input of draft picks, print the order of the draft and the results, and write the results to a text file. This ...
2
votes
1answer
61 views

My game of life implementation

I'm looking for comments on both the micro (individual lines of code) and macro (overall design) scale. My input/output format is a single string, with newlines delimiting rows and ...
4
votes
1answer
863 views

Implement a boto3 RDS generator in Python

Boto3 does not seem to implement a generator for RDS instances, instead offering a marker and pagination feature. This code does work for my use case and I have been using it for the entire day ...
0
votes
1answer
255 views

Generating a list of menus with AngularJS

Some days ago, I decided to start learning a JS framework in order to gain skills and become more useful at work. I choose Angular for the purpose, and started learning it by building up my "...
5
votes
2answers
160 views

Read lines from a file in chunks

I created a function to read lines from an file into chunks. My hidden agenda in creation this script was in python the yield function in interaction with chunks. The script works fine, but now i ...
2
votes
1answer
4k views

Iterate through python iterable in chunk of n

I need a function to iterate through a python iterable in chunks. That is, it takes an iterable and a size of n and yields generators iterating through each chunk of size n. After some experimentation,...
5
votes
1answer
480 views

“Acro Words” - Creating Acronyms of a Text that are Words

I wrote a program that given an input string returns a list of tuples in which each element of the tuple is a word given by dropping some number of characters of the original string, preserving order. ...
3
votes
2answers
272 views

Transforming a list of two-dimensional coordinates into a flat list of relative changes

I have a little function that parse a nested list of coordinates into a flat list of compressed coordinates. By compressed coordinates, I mean that only the delta (distance) between each coordinates ...
2
votes
2answers
2k views

Infinite prime generator

This code acts as an infinite generator of prime numbers. As new prime numbers are found, they are added to a set. Then, a number x is found to be prime if none of the numbers in the prime set are ...
4
votes
3answers
5k views

Testing a Random number generator

Firstly, would appreciate some code-review feedback, from a TDD and design perspective. Secondly, what are your thoughts on implementing test case: ...
4
votes
2answers
775 views

Retrieving the nth term of an infinite stream

I recently learned how generators can be infinite in Python. For example, the infinite sequence can be the triangular numbers: $$1, 3, 6, 10,...$$ ...
10
votes
1answer
143 views

Using generators to print the tree-like structure of a project

The goal of this code is to print the entity tree of a VHDL project. There are a readme and very minimal tests on the github repo. I am trying to refactor the code to use generators in order to ...
4
votes
2answers
374 views

Combining fragments of data in a generator pipeline

Edit: it seems flow based (or reactive) programming approach would help here; there are some libraries in python that try that. I tried to follow the generator pipeline style (see David Beazley's ...
4
votes
3answers
274 views

Concatenating two IEnumerables with a limit

I've been playing around with generators, generics and extension methods in C# (5.0) and wanted to create an extension method for IEnumerable, which would append ...
3
votes
1answer
7k views

Extracting specific rows and columns from a CSV file

I have written a function to selectively extract data from a file. I want to be able to extract only from a certain line and only given rows. Would convert this function into a generator improve the ...
6
votes
2answers
517 views

Random name generation in ruby

I created this program as an exercise for me to learn ruby. It is a random name generator with a set of rules defined to generate pretty decent and usually pronouncable names. Ruby isn't exactly my ...
3
votes
0answers
924 views

C++11 generator implementation emulating Python's yield keyword

C++11's range-based loops allow convenient and easy iteration over containers, but what about more complicated iterations such as tree traversal? Usually this involves a lot of boilerplate code and ...
2
votes
2answers
424 views

Fibonacci sequence methods

Decided to get a little JDK 8 practice in today, so I built two methods to print out the golden ratio, one uses the Stream API the other does not. Wanted to request general feedback on how well I ...
2
votes
1answer
412 views

Exponential backoff generator

Exponential backoff in the context of various networking protocols looks something like this: When a collision first occurs, send a “Jamming signal” to prevent further data being sent. ...
11
votes
7answers
3k views

French animal generator

I'm learning Python. I'm making a random animal generator, from 5 txt files. Each file is a list of words for respectively the specie of the animal, an adjective, an origin, and a bodypart with a ...
3
votes
1answer
202 views

Recursive flattening of Swift sequences - an overly complicated approach

I recently read and answered Martin R's Recursive flattening of Swift sequences and continued to play around with the code until I arrived at something that was both pretty cool and possibly an ...