# \#iterators

**URL:** https://discourse.julialang.org/tag/iterators/579.md

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

---

## [Recommended way to destructuring a type](https://discourse.julialang.org/t/recommended-way-to-destructuring-a-type/137103)

<div class="topic-metadata">

**Author:** [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Replies:** 7\
**Last updated:** [May 13, 2026, 4:22pm UTC](https://discourse.julialang.org/t/recommended-way-to-destructuring-a-type/137103 "2026-05-13T16:22:32Z")

</div>

I would like to provide destructuring assignment as a syntactic sugar for a custom type. I understand that the way to do it is iteration, but I am wondering if there is something more elegant than the MWE below, also co…

---

## [Should reduce throw an error on infinite iterators?](https://discourse.julialang.org/t/should-reduce-throw-an-error-on-infinite-iterators/136389)

<div class="topic-metadata">

**Author:** [@HMegh](https://discourse.julialang.org/u/HMegh)\
**Replies:** 4\
**Last updated:** [March 26, 2026, 5:27pm UTC](https://discourse.julialang.org/t/should-reduce-throw-an-error-on-infinite-iterators/136389 "2026-03-26T17:27:40Z")

</div>

Currently, reduce (and by extension mapreduce, sum and other functions) operate on iterators without checking whether they are finite or not. For example julia\> sum(Iterators.cycle(\[1,2,3\])) would run forever. I wanted…

---

## [New package: Collects.jl - meant to improve upon and generalize the interface of collect](https://discourse.julialang.org/t/new-package-collects-jl-meant-to-improve-upon-and-generalize-the-interface-of-collect/129468)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 9\
**Last updated:** [February 17, 2026, 11:36pm UTC](https://discourse.julialang.org/t/new-package-collects-jl-meant-to-improve-upon-and-generalize-the-interface-of-collect/129468 "2026-02-17T23:36:31Z")

</div>

Just requested the registration of the new package, Collects.jl, meaning it should be registered after three days pass. Basically it’s meant to replace collect, especially when the desired output container type is not A…

---

## [Iterating over integer simplex](https://discourse.julialang.org/t/iterating-over-integer-simplex/135060)

<div class="topic-metadata">

**Author:** [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Replies:** 14\
**Last updated:** [January 16, 2026, 12:29pm UTC](https://discourse.julialang.org/t/iterating-over-integer-simplex/135060 "2026-01-16T12:29:59Z")

</div>

I need to iterate over all K-tuples of nonnegative Ints that sum to N. Eg if K is 3 and N==3, (0,0,3) (0,1,2) (0,2,1) (0,3,0) (1,0,2) (1,1,1) (1,2,0) (2,0,1) (2,1,0) (3,0,0) Ordering does not matter, just full travers…

---

## [Understanding iterate() documentation is tough](https://discourse.julialang.org/t/understanding-iterate-documentation-is-tough/134715)

<div class="topic-metadata">

**Author:** [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Replies:** 3\
**Last updated:** [December 24, 2025, 12:32am UTC](https://discourse.julialang.org/t/understanding-iterate-documentation-is-tough/134715 "2025-12-24T00:32:35Z")

</div>

Understanding iterate() documentation is made tough although everyone use it easily. Is Julia for loop behind the scene a while loop? julia\> for i in 1:5 println(i) end is equivalent to :down\_arrow: …

---

## [Iterate on calculated iterable](https://discourse.julialang.org/t/iterate-on-calculated-iterable/132026)

<div class="topic-metadata">

**Author:** [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Replies:** 6\
**Last updated:** [September 2, 2025, 10:23pm UTC](https://discourse.julialang.org/t/iterate-on-calculated-iterable/132026 "2025-09-02T22:23:08Z")

</div>

I have a type for which I want to implement a kind of iteration, which I can easily do with functions in Iterators. But I want iteration to happen on something I construct. MWE: struct NamedProduct{T} np::T end fun…

---

## [Iterable signature, revisited](https://discourse.julialang.org/t/iterable-signature-revisited/129647)

<div class="topic-metadata">

**Author:** [@Ken\_Williams](https://discourse.julialang.org/u/Ken_Williams)\
**Replies:** 14\
**Last updated:** [June 10, 2025, 8:48pm UTC](https://discourse.julialang.org/t/iterable-signature-revisited/129647 "2025-06-10T20:48:11Z")

</div>

There have been a few threads over the years about how to declare a function argument as “iterable” somehow, for example in Iterable type signature? . For me (at least in my latest situation) the important part isn’t th…

---

## [Iterating over primes](https://discourse.julialang.org/t/iterating-over-primes/128556)

<div class="topic-metadata">

**Author:** [@JADekker](https://discourse.julialang.org/u/JADekker)\
**Replies:** 11\
**Last updated:** [April 30, 2025, 2:47pm UTC](https://discourse.julialang.org/t/iterating-over-primes/128556 "2025-04-30T14:47:38Z")

</div>

In Project Euler, prime numbers come up quite a lot, but sometimes it is not clear how many prime numbers are needed. To avoid duplicate calculations, and as a challenge to get a bit more familiar with iterators in julia…

---

## [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…

---

## [NonLinearRange](https://discourse.julialang.org/t/nonlinearrange/125221)

<div class="topic-metadata">

**Author:** [@RainerHeintzmann](https://discourse.julialang.org/u/RainerHeintzmann)\
**Replies:** 7\
**Last updated:** [January 28, 2025, 8:58am UTC](https://discourse.julialang.org/t/nonlinearrange/125221 "2025-01-28T08:58:54Z")

</div>

I was looking for way to way to allow multi-kernel broadcasting to act also over non-linear ranges. E.g. consider a complex exponential exp.(1im .\* k0 .\* x) with k0=1.35 being a scalar and x=0:255 a linear range. This …

---

## [Simple, understandable iterator](https://discourse.julialang.org/t/simple-understandable-iterator/124937)

<div class="topic-metadata">

**Author:** [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Replies:** 0\
**Last updated:** [January 20, 2025, 1:24am UTC](https://discourse.julialang.org/t/simple-understandable-iterator/124937 "2025-01-20T01:24:44Z")

</div>

It’s been noted that a lot of Julia code has more unnecessary allocation and construction of containers than, say, Rust code. I think this is due in large part to Julia’s dual role as a programming language and an applic…

---

## [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. …

---

## [Iterate over a range in a non standard way](https://discourse.julialang.org/t/iterate-over-a-range-in-a-non-standard-way/121137)

<div class="topic-metadata">

**Author:** [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)\
**Replies:** 4\
**Last updated:** [October 10, 2024, 12:45pm UTC](https://discourse.julialang.org/t/iterate-over-a-range-in-a-non-standard-way/121137 "2024-10-10T12:45:01Z")

</div>

I’m doing some exhaustive search loops and would like to cover say a range 10:100 but suspect (with quite some confidence) that the solution I look for is around 90. So I would like to loop over the range by starting at …

---

## [Does length(itr) need to be invariant? (stateful iterator)](https://discourse.julialang.org/t/does-length-itr-need-to-be-invariant-stateful-iterator/118170)

<div class="topic-metadata">

**Author:** [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Replies:** 4\
**Last updated:** [September 24, 2024, 8:49pm UTC](https://discourse.julialang.org/t/does-length-itr-need-to-be-invariant-stateful-iterator/118170 "2024-09-24T20:49:54Z")

</div>

I have a calculation that tracks a large state for a cohort of economic agents that have a finite lifetime. A certain summary statistic is implemented with the iteration protocol. If you imagine c below to be something …

---

## [Coding different iterations over a dataset](https://discourse.julialang.org/t/coding-different-iterations-over-a-dataset/119675)

<div class="topic-metadata">

**Author:** [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Replies:** 3\
**Last updated:** [September 22, 2024, 6:26am UTC](https://discourse.julialang.org/t/coding-different-iterations-over-a-dataset/119675 "2024-09-22T06:26:32Z")

</div>

I have some code currently implemented via multiple dispatch that looks vaguely like: function process\_data(data,n) for i in 1:n # apply operation to entire data set, data end end function process\_data(data\_se…

---

## [Spending a lot of time in \`materialize\` called by \`findall\`](https://discourse.julialang.org/t/spending-a-lot-of-time-in-materialize-called-by-findall/117859)

<div class="topic-metadata">

**Author:** [@Oblomov](https://discourse.julialang.org/u/Oblomov)\
**Replies:** 14\
**Last updated:** [August 7, 2024, 6:01am UTC](https://discourse.julialang.org/t/spending-a-lot-of-time-in-materialize-called-by-findall/117859 "2024-08-07T06:01:08Z")

</div>

Hello all, I’m new to Julia and as an exercise I’ve ported a medium-complex Python code to Julia. Even without particular optimizations, the Julia code is 4 to 5 times faster than Python (nice!), but I’ve decided to try…

---

## [Help writing a \`collect\` for iterators](https://discourse.julialang.org/t/help-writing-a-collect-for-iterators/117494)

<div class="topic-metadata">

**Author:** [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Replies:** 11\
**Last updated:** [July 26, 2024, 8:38pm UTC](https://discourse.julialang.org/t/help-writing-a-collect-for-iterators/117494 "2024-07-26T20:38:58Z")

</div>

Here is a task that I am having a hard time to accomplish with Julia Base: Given an iterator iter and a set of linear indices inds, write a function collectat(iter, inds) that collects the iterator at the indices witho…

---

## [Overflow with Iterators.product](https://discourse.julialang.org/t/overflow-with-iterators-product/116532)

<div class="topic-metadata">

**Author:** [@minjaep](https://discourse.julialang.org/u/minjaep)\
**Replies:** 7\
**Last updated:** [July 2, 2024, 6:01pm UTC](https://discourse.julialang.org/t/overflow-with-iterators-product/116532 "2024-07-02T18:01:38Z")

</div>

I need to deal with values assigned to a d-dimensional cube, where the dimension d varies. Say L is the side length of a cube. To access a d-dimensional array with L^d entries I used the following. idx = Iterators.produ…

---

## [Why \`map\` requires \`collect\` to use for general iterable things?](https://discourse.julialang.org/t/why-map-requires-collect-to-use-for-general-iterable-things/114748)

<div class="topic-metadata">

**Author:** [@paalon](https://discourse.julialang.org/u/paalon)\
**Replies:** 18\
**Last updated:** [May 28, 2024, 10:05am UTC](https://discourse.julialang.org/t/why-map-requires-collect-to-use-for-general-iterable-things/114748 "2024-05-28T10:05:54Z")

</div>

Why map requires to collect for general iterable things? julia\> foreach(println, "あいうえお") あ い う え お julia\> map(println, "あいうえお"); あ ERROR: ArgumentError: map(f, s::AbstractString) requires f to return AbstractChar; try…

---

## [How to nest any number of for loops](https://discourse.julialang.org/t/how-to-nest-any-number-of-for-loops/114067)

<div class="topic-metadata">

**Author:** [@Marco-Congedo](https://discourse.julialang.org/u/Marco-Congedo)\
**Replies:** 8\
**Last updated:** [May 9, 2024, 10:14pm UTC](https://discourse.julialang.org/t/how-to-nest-any-number-of-for-loops/114067 "2024-05-09T22:14:32Z")

</div>

I need to iterate over all combinations of k permutations of integers (1:n). Say, k=2 and n=2. The permutations of (1:2) are \[1, 2\] \[2, 1\] and what i need is \[1, 2\] , \[1, 2\] \[1, 2\] , \[2, 1\] \[2, 1\] , \[1, 2\] \[2, 1…

---

## [Type-stable alternatives to Iterators.product?](https://discourse.julialang.org/t/type-stable-alternatives-to-iterators-product/113790)

<div class="topic-metadata">

**Author:** [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Replies:** 2\
**Last updated:** [May 3, 2024, 12:50pm UTC](https://discourse.julialang.org/t/type-stable-alternatives-to-iterators-product/113790 "2024-05-03T12:50:19Z")

</div>

I’m dealing with a combinatorial problem. Given N vectors of integers of varied lengths, I want to find all possible ways to select one integer from each vector. Here’s my code, which is unfortunately not type-stable: j…

---

## [I don't get Base.IteratorElType](https://discourse.julialang.org/t/i-dont-get-base-iteratoreltype/113604)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 6\
**Last updated:** [April 30, 2024, 7:50am UTC](https://discourse.julialang.org/t/i-dont-get-base-iteratoreltype/113604 "2024-04-30T07:50:15Z")

</div>

Motivation I’m implementing a collect-like function for a certain subtype of AbstractArray, FixedSizeArray (@giordano). The function is supposed to take an arbitrary iterator, consume it, and return a FixedSizeArray valu…

---

## [Can I use eachrow, eachcol, etc. to change array's values?](https://discourse.julialang.org/t/can-i-use-eachrow-eachcol-etc-to-change-arrays-values/111834)

<div class="topic-metadata">

**Author:** [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Replies:** 9\
**Last updated:** [March 20, 2024, 3:01pm UTC](https://discourse.julialang.org/t/can-i-use-eachrow-eachcol-etc-to-change-arrays-values/111834 "2024-03-20T15:01:00Z")

</div>

Can I use eachrow, eachcol, etc. to change array’s values? For example, x = rand(5, 5) for c in eachcol(x) for el in c if el \< maximum(x)/2 el = 0.0 end end end display(x) does noth…

---

## [Collect (flat) iterator as an Array](https://discourse.julialang.org/t/collect-flat-iterator-as-an-array/108617)

<div class="topic-metadata">

**Author:** [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Replies:** 3\
**Last updated:** [January 10, 2024, 12:52pm UTC](https://discourse.julialang.org/t/collect-flat-iterator-as-an-array/108617 "2024-01-10T12:52:54Z")

</div>

Is there a default generic way I can collect an iterator itr that HasLength into an Array of given (compatible) dims? Eg collect itr = (i for i in 1:6) into a 2x3 Matrix. Just to clarify, it is easy to write a wrappe…

---

## [Iterative construction of lazy Iterators leads to excessive compilation time](https://discourse.julialang.org/t/iterative-construction-of-lazy-iterators-leads-to-excessive-compilation-time/106766)

<div class="topic-metadata">

**Author:** [@\_cao](https://discourse.julialang.org/u/_cao)\
**Replies:** 21\
**Last updated:** [December 10, 2023, 8:19pm UTC](https://discourse.julialang.org/t/iterative-construction-of-lazy-iterators-leads-to-excessive-compilation-time/106766 "2023-12-10T20:19:00Z")

</div>

I wanted to write an iterator that lazily traverses a graph in breadth first search order, where the graph is defined implicitly by a function giving for each vertex an iterable over the adjacent vertices. This is what I…

---

## [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…

---

## [New Iterator from Nested Iterators](https://discourse.julialang.org/t/new-iterator-from-nested-iterators/103377)

<div class="topic-metadata">

**Author:** [@bmit](https://discourse.julialang.org/u/bmit)\
**Replies:** 19\
**Last updated:** [September 14, 2023, 12:51am UTC](https://discourse.julialang.org/t/new-iterator-from-nested-iterators/103377 "2023-09-14T00:51:41Z")

</div>

Is there any shortcut to create a new iterator from existing nested iterators? I know I can manually define a new type, length, iterate, and eltype, but I’m wondering if there’s a more direct way. Below is a MWE of a ge…

---

## [Custom iterator failed](https://discourse.julialang.org/t/custom-iterator-failed/103301)

<div class="topic-metadata">

**Author:** [@Denis\_Ivanov](https://discourse.julialang.org/u/Denis_Ivanov)\
**Replies:** 12\
**Last updated:** [August 28, 2023, 6:41pm UTC](https://discourse.julialang.org/t/custom-iterator-failed/103301 "2023-08-28T18:41:41Z")

</div>

Here is very simple idea: struct Test a::Int n::Int end step(x::Int) = x \> 1 ? x - 1 : 1 Base.iterate(t::Test, state=1) = state \> t.n ? nothing : (step(t.a), state + 1) Base.length(t::Test) = t.n For some …

---

## [Interacting with current and next iterator](https://discourse.julialang.org/t/interacting-with-current-and-next-iterator/102487)

<div class="topic-metadata">

**Author:** [@astro-kevin](https://discourse.julialang.org/u/astro-kevin)\
**Replies:** 2\
**Last updated:** [August 4, 2023, 2:10pm UTC](https://discourse.julialang.org/t/interacting-with-current-and-next-iterator/102487 "2023-08-04T14:10:38Z")

</div>

Say a is an iterator (not a vector or anything that can be indexed). I have function f(x,y) = x+y, and I want to have the function act on successive values of a. Say for our example a is an iterator for all odd numbers …

---

## [Array/vector comprehension and AbstractTrees Leaves iterator](https://discourse.julialang.org/t/array-vector-comprehension-and-abstracttrees-leaves-iterator/102337)

<div class="topic-metadata">

**Author:** [@pitsianis](https://discourse.julialang.org/u/pitsianis)\
**Replies:** 0\
**Last updated:** [August 1, 2023, 1:18am UTC](https://discourse.julialang.org/t/array-vector-comprehension-and-abstracttrees-leaves-iterator/102337 "2023-08-01T01:18:45Z")

</div>

Why the array comprehension behaves differently with the AbstractTrees.Leaves iterator? In the following replacing Leaves(t) with say, range(1,3,3), I do get the 2D array. julia\> using AbstractTrees julia\> t = \[\[1, 2\]…

[Next page](https://discourse.julialang.org/tag/iterators/579.md?match_all_tags=true&page=1&tags%5B%5D=iterators)
