# \#generator

**URL:** https://discourse.julialang.org/tag/generator/407.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [Generator sometimes works, sometimes not](https://discourse.julialang.org/t/generator-sometimes-works-sometimes-not/127701)

<div class="topic-metadata">

**Author:** [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Replies:** 3\
**Last updated:** [April 4, 2025, 3:08pm UTC](https://discourse.julialang.org/t/generator-sometimes-works-sometimes-not/127701 "2025-04-04T15:08:10Z")

</div>

It’s about Generator Expressions. I used to only know about sum(i for i in 1:I). But today I happened to discover that we can also write minimum(i for i in 1:I). I’m thus intrigued: Do all function who takes an Vect…

---

## [Understanding \`Base.findall\` and customizing it](https://discourse.julialang.org/t/understanding-base-findall-and-customizing-it/125220)

<div class="topic-metadata">

**Author:** [@Leo\_I](https://discourse.julialang.org/u/Leo_I)\
**Replies:** 2\
**Last updated:** [January 26, 2025, 1:24pm UTC](https://discourse.julialang.org/t/understanding-base-findall-and-customizing-it/125220 "2025-01-26T13:24:58Z")

</div>

Given a vector ww, I wish to obtain all its nonzero values and the corresponding positions (with specified type). I solve this with: using BenchmarkTools, Test function \_find0(::tp, ww::AbstractVector{tw}) ::Tuple{Vecto…

---

## [Sieve of Eratosthenes iterator](https://discourse.julialang.org/t/sieve-of-eratosthenes-iterator/121512)

<div class="topic-metadata">

**Author:** [@Brinkhuis](https://discourse.julialang.org/u/Brinkhuis)\
**Replies:** 1\
**Last updated:** [October 20, 2024, 7:56pm UTC](https://discourse.julialang.org/t/sieve-of-eratosthenes-iterator/121512 "2024-10-20T19:56:40Z")

</div>

I would like to create an Iterator for prime numbers (I know there is a package Primes). Based on Pythons code for the Sieve of Eratosthenes I coded a function sieve(). sieve(10) returns a vector of the first 10 primes. …

---

## [Generator expressions](https://discourse.julialang.org/t/generator-expressions/110337)

<div class="topic-metadata">

**Author:** [@vsoler](https://discourse.julialang.org/u/vsoler)\
**Replies:** 4\
**Last updated:** [February 20, 2024, 11:26am UTC](https://discourse.julialang.org/t/generator-expressions/110337 "2024-02-20T11:26:08Z")

</div>

I’m reading the Julia Manual on Generator Expressions. According to the Manual … the following expression sums a series without allocating memory: sum(1/n^2 for n in 1:1000) So I tried to compare the previous expres…

---

## [Explain generators](https://discourse.julialang.org/t/explain-generators/109573)

<div class="topic-metadata">

**Author:** [@Anders\_Holtsberg](https://discourse.julialang.org/u/Anders_Holtsberg)\
**Replies:** 11\
**Last updated:** [February 2, 2024, 5:03am UTC](https://discourse.julialang.org/t/explain-generators/109573 "2024-02-02T05:03:45Z")

</div>

I am trying to understand how to implement a simple generator so that I can traverse my graphs in different ways. It boils down to the example below. In Lua - which I come from - it works without any ado, but in Julia it…

---

## [How do I retrieve values using an array generator?](https://discourse.julialang.org/t/how-do-i-retrieve-values-using-an-array-generator/105421)

<div class="topic-metadata">

**Author:** [@crustaccione](https://discourse.julialang.org/u/crustaccione)\
**Replies:** 11\
**Last updated:** [October 26, 2023, 3:45pm UTC](https://discourse.julialang.org/t/how-do-i-retrieve-values-using-an-array-generator/105421 "2023-10-26T15:45:02Z")

</div>

I know how to create an array using array = \[x + y for x in 2:8, y in -1:3\] But I am trying to figure out how to retrieve elements of an array that the array generator creates e.g. array\_gen = (x + y for x in 2:8, y i…

---

## [Is it always safe to “re-use” an iterator/generator?](https://discourse.julialang.org/t/is-it-always-safe-to-re-use-an-iterator-generator/95697)

<div class="topic-metadata">

**Author:** [@lmtzx9h4qqnt](https://discourse.julialang.org/u/lmtzx9h4qqnt)\
**Replies:** 8\
**Last updated:** [March 15, 2023, 2:21am UTC](https://discourse.julialang.org/t/is-it-always-safe-to-re-use-an-iterator-generator/95697 "2023-03-15T02:21:05Z")

</div>

The way iterators work in Python, there is the following (potentially surprising) behavior: \>\>\> l = \[1, 2, 3\] \>\>\> g = (x for x in l) \>\>\> list(g) \[1, 2, 3\] \>\>\> list(g) \[\] That is, iterators are “used up” during the iter…

---

## [Generator slow when passed as argument](https://discourse.julialang.org/t/generator-slow-when-passed-as-argument/91606)

<div class="topic-metadata">

**Author:** [@uniment](https://discourse.julialang.org/u/uniment)\
**Replies:** 6\
**Last updated:** [December 20, 2022, 3:02am UTC](https://discourse.julialang.org/t/generator-slow-when-passed-as-argument/91606 "2022-12-20T03:02:47Z")

</div>

(Julia 1.9.0-alpha1) Why is this? julia\> @btime let x=(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8) ns=propertynames(x) NamedTuple{ns}(map(n-\>getproperty(x,n), ns)) end 1.000 ns (0 allocations: 0 byt…

---

## [Inconsistency in \`sum\` of an empty generator](https://discourse.julialang.org/t/inconsistency-in-sum-of-an-empty-generator/89638)

<div class="topic-metadata">

**Author:** [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Replies:** 5\
**Last updated:** [November 2, 2022, 3:46pm UTC](https://discourse.julialang.org/t/inconsistency-in-sum-of-an-empty-generator/89638 "2022-11-02T15:46:57Z")

</div>

On Julia 1.3, the following two empty generators both throw an error when you apply sum to them: julia\> g1 = (1 for i in Int\[\]); julia\> g2 = (x for x in \[-1\] if x \> 0); julia\> eltype(g1) Any julia\> eltype(g2) Any ju…

---

## [How do I get readdlm to return a Matrix of strings?](https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186)

<div class="topic-metadata">

**Author:** [@eyeHawk](https://discourse.julialang.org/u/eyeHawk)\
**Replies:** 12\
**Last updated:** [August 2, 2022, 10:43pm UTC](https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186 "2022-08-02T22:43:12Z")

</div>

I am reading in a list of english words into my program. I then want to make sure the words are lowercase so I am processing the list with lowercase() one word at a time and pushing them into another list. It seems that …

---

## [Creating Generators](https://discourse.julialang.org/t/creating-generators/3962)

<div class="topic-metadata">

**Author:** [@rokkuran](https://discourse.julialang.org/u/rokkuran)\
**Replies:** 32\
**Last updated:** [July 6, 2022, 8:34pm UTC](https://discourse.julialang.org/t/creating-generators/3962 "2022-07-06T20:34:58Z")

</div>

I’ve been playing around with Julia lately and have been enjoying it. I have come from python and as a learning exercise was looking at re-writing some code I had written to do least significant bit embedding. I’m wonde…

---

## [Understanding Allocations and Views](https://discourse.julialang.org/t/understanding-allocations-and-views/74967)

<div class="topic-metadata">

**Author:** [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Replies:** 4\
**Last updated:** [January 21, 2022, 4:31am UTC](https://discourse.julialang.org/t/understanding-allocations-and-views/74967 "2022-01-21T04:31:50Z")

</div>

Hello, I am trying to improve some code I have, specifically to reduce the allocations it makes in a hot inner loop. I’ve been using Profile and julia --track-allocation=user to inspect on what lines the bulk of the all…

---

## [Filling an array with a generator that returns tuples](https://discourse.julialang.org/t/filling-an-array-with-a-generator-that-returns-tuples/62737)

<div class="topic-metadata">

**Author:** [@fedario](https://discourse.julialang.org/u/fedario)\
**Replies:** 4\
**Last updated:** [January 2, 2022, 8:03am UTC](https://discourse.julialang.org/t/filling-an-array-with-a-generator-that-returns-tuples/62737 "2022-01-02T08:03:01Z")

</div>

I’m trying to create a function that takes a number of iterables and fills an array columnwise with each combination of those. The following function has the correct output, but the number of allocations is unexpected. u…

---

## [Generator questions](https://discourse.julialang.org/t/generator-questions/73665)

<div class="topic-metadata">

**Author:** [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Replies:** 1\
**Last updated:** [December 27, 2021, 2:15am UTC](https://discourse.julialang.org/t/generator-questions/73665 "2021-12-27T02:15:38Z")

</div>

While poking around with the generator example below to try understanding the concept, some questions popped up: Why is the last equality false given that the generator was regenerated? With the command g.f(g.iter\[n\]),…

---

## [What is # = none: 1 = # in expression?](https://discourse.julialang.org/t/what-is-none-1-in-expression/71355)

<div class="topic-metadata">

**Author:** [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Replies:** 2\
**Last updated:** [November 11, 2021, 11:51pm UTC](https://discourse.julialang.org/t/what-is-none-1-in-expression/71355 "2021-11-11T23:51:41Z")

</div>

Hellow! what is # = none: 4 = #, is it possible to create an expression without this? or is bad idea to generate src-files based on expressions? julia\> ex = quote x = 1 y = 2 x + y …

---

## [Question about allocations and generators](https://discourse.julialang.org/t/question-about-allocations-and-generators/62693)

<div class="topic-metadata">

**Author:** [@RomainPct](https://discourse.julialang.org/u/RomainPct)\
**Replies:** 2\
**Last updated:** [June 11, 2021, 7:51pm UTC](https://discourse.julialang.org/t/question-about-allocations-and-generators/62693 "2021-06-11T19:51:08Z")

</div>

Hello, this this question is out of curiosity, hoping to understand more about Julia. Those three functions allocate differently: g1(v::Vector{Int64}) = SVector{length(v), Int64}(v...) g2(v::Vector{Int64}, n) = SVector…

---

## [Base.Generator being slow because type inference fails with isnothing](https://discourse.julialang.org/t/base-generator-being-slow-because-type-inference-fails-with-isnothing/57297)

<div class="topic-metadata">

**Author:** [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Replies:** 6\
**Last updated:** [March 16, 2021, 1:01pm UTC](https://discourse.julialang.org/t/base-generator-being-slow-because-type-inference-fails-with-isnothing/57297 "2021-03-16T13:01:11Z")

</div>

Hey, consider following two functions, to generate a generator: function f(size; scaling=1.0) if isnothing(scaling) scaling = 1.0 end return (sum(scaling .\* Tuple(index)) for index in CartesianIndic…

---

## [\[ANN\] Quicktype -- generate JSON3 structs from json samples (WIP)](https://discourse.julialang.org/t/ann-quicktype-generate-json3-structs-from-json-samples-wip/52948)

<div class="topic-metadata">

**Author:** [@kskyten](https://discourse.julialang.org/u/kskyten)\
**Replies:** 0\
**Last updated:** [January 6, 2021, 4:26pm UTC](https://discourse.julialang.org/t/ann-quicktype-generate-json3-structs-from-json-samples-wip/52948 "2021-01-06T16:26:18Z")

</div>

Quicktype is a neat program that infers schemas from json samples and generates code in various different languages to read the json into native objects. I hacked together basic support for Julia in Quicktype. It should…

---

## [Varargs of vectors and scalars under modulo](https://discourse.julialang.org/t/varargs-of-vectors-and-scalars-under-modulo/52548)

<div class="topic-metadata">

**Author:** [@jakewilliami](https://discourse.julialang.org/u/jakewilliami)\
**Replies:** 5\
**Last updated:** [December 29, 2020, 4:45pm UTC](https://discourse.julialang.org/t/varargs-of-vectors-and-scalars-under-modulo/52548 "2020-12-29T16:45:57Z")

</div>

How would I go about expanding the following code for any number of vectors (possibly using Iterators or @nloops)? My code below shows it working for the first three vectors. function list\_span(u̲::Vector{T}, v̲::Vecto…

---

## [Help subdividing Generators](https://discourse.julialang.org/t/help-subdividing-generators/51957)

<div class="topic-metadata">

**Author:** [@danilo-bc](https://discourse.julialang.org/u/danilo-bc)\
**Replies:** 9\
**Last updated:** [December 18, 2020, 6:05am UTC](https://discourse.julialang.org/t/help-subdividing-generators/51957 "2020-12-18T06:05:44Z")

</div>

I got inspired by this post on how to generate words from a pool of characters (Generate all words of a given size from a fixed alphabet - #4 by bennedich) and wanted to try it for myself for fun and to learn Generators,…

---

## [\[ANN\] View common generators as arrays](https://discourse.julialang.org/t/ann-view-common-generators-as-arrays/39707)

<div class="topic-metadata">

**Author:** [@klacru](https://discourse.julialang.org/u/klacru)\
**Replies:** 8\
**Last updated:** [May 21, 2020, 7:12am UTC](https://discourse.julialang.org/t/ann-view-common-generators-as-arrays/39707 "2020-05-21T07:12:32Z")

</div>

The new package GeneratorArrays effectively converts a generator into a lazy form of a read-only abstract array. The object array(f(i) for i in a) can be used like \[f(i) for i in a\] without using an extra array for stor…

---

## [What is a Base.Generator?](https://discourse.julialang.org/t/what-is-a-base-generator/32316)

<div class="topic-metadata">

**Author:** [@lwhitefox](https://discourse.julialang.org/u/lwhitefox)\
**Replies:** 20\
**Last updated:** [December 16, 2019, 2:21pm UTC](https://discourse.julialang.org/t/what-is-a-base-generator/32316 "2019-12-16T14:21:32Z")

</div>

I’ve been looking at some sample code from th eFlux model zoo, and I came across something like: f(x) = x^2 d = (f(i) for i in 1:10) (this is much simplified to help pose the question). The result indicates that d is B…

---

## [Are generator expressions documented? Also other questions](https://discourse.julialang.org/t/are-generator-expressions-documented-also-other-questions/13786)

<div class="topic-metadata">

**Author:** [@taliesinb](https://discourse.julialang.org/u/taliesinb)\
**Replies:** 8\
**Last updated:** [August 24, 2018, 1:51pm UTC](https://discourse.julialang.org/t/are-generator-expressions-documented-also-other-questions/13786 "2018-08-24T13:51:25Z")

</div>

Sorry, this might be a dumb question. This search reveals nothing obviously relevant, at least in the top results: Search · The Julia Language And yet I’ve noticed from some examples I saw online that Python-like gener…
