# Why does \`Iterator.copyToArray\` leave the iterator in an undefined state?

**URL:** <https://users.scala-lang.org/t/why-does-iterator-copytoarray-leave-the-iterator-in-an-undefined-state/9671>\
**Category:** Question\
**Created:** [December 23, 2023, 9:46pm UTC](https://users.scala-lang.org/t/why-does-iterator-copytoarray-leave-the-iterator-in-an-undefined-state/9671 "2023-12-23T21:46:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Turin](https://users.scala-lang.org/user_avatar/users.scala-lang.org/turin/32/2092_2.png) [@Turin](https://users.scala-lang.org/u/Turin)\
**Post date:** [December 23, 2023, 9:46pm UTC](https://users.scala-lang.org/t/why-does-iterator-copytoarray-leave-the-iterator-in-an-undefined-state/9671/1 "2023-12-23T21:46:02Z")

</div>

Is there a good reason for it? Does it allow some optimizations in some contexts?  
Because it’s a bit problematic when you’d like to copy the contents of the iterator into several places, for example to an `UnrolledBuffer`. Currently we must call `splitAt` on the iterator for every segment we want to handle separately, and even only if we want to know if we have exhausted the iterator.

---

<div class="post-metadata">

**Author:** ![Bersier](https://users.scala-lang.org/user_avatar/users.scala-lang.org/bersier/32/2200_2.png) [@Bersier](https://users.scala-lang.org/u/Bersier)\
**Post date:** [December 23, 2023, 10:09pm UTC](https://users.scala-lang.org/t/why-does-iterator-copytoarray-leave-the-iterator-in-an-undefined-state/9671/2 "2023-12-23T22:09:58Z")

</div>

Disclaimer: this is only an incomplete answer, as it doesn’t address your specific use-case with `UnrolledBuffer`.

I believe that iterators are not meant to be used more than once. They are not collections, but can represent the state of an iteration.

As such, trying to use an iterator more than once indicates a conceptual coding error. However, elements of an iterator can be collected into a collection, and the latter can be reused.

---

<div class="post-metadata">

**Author:** ![BalmungSan](https://users.scala-lang.org/user_avatar/users.scala-lang.org/balmungsan/32/1700_2.png) [@BalmungSan](https://users.scala-lang.org/u/BalmungSan)\
**Post date:** [December 23, 2023, 10:14pm UTC](https://users.scala-lang.org/t/why-does-iterator-copytoarray-leave-the-iterator-in-an-undefined-state/9671/3 "2023-12-23T22:14:15Z")

</div>

Because consuming the Iterator may have altered state, made network calls, read files, etc.  
They are intended to be consumed once, and only once.

If you want to copy is contents to multiple outputs you have a couple of options:

1. Use `foreach`
2. Load it all in memory on a strict collection like `Vector`
3. Use a `view` which will re-run its logic every time _(be careful about the implications of this)_.

You may also consider more powerful abstractions like **fs2** or **AkkaStreams**.

---

<div class="post-metadata">

**Author:** ![som-snytt](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@som-snytt](https://users.scala-lang.org/u/som-snytt)\
**Post date:** [December 24, 2023, 10:19am UTC](https://users.scala-lang.org/t/why-does-iterator-copytoarray-leave-the-iterator-in-an-undefined-state/9671/4 "2023-12-24T10:19:37Z")

</div>

Something similar just happened in an unrelated API, with Java’s `CompletableFuture`, where `minimalStage` gives you a restricted `CompletionStage` which is a `CompletableFuture` that doesn’t let you test if it has completed.

That is, the API seems to take away functionality in favor of frustration.

Per the other topic, `grouped` actually distributes `ArraySeq`, so if you wanted equally sized slices, that is just using `copyToArray`.

TIL:

```auto
scala> res10.asInstanceOf[collection.immutable.ArraySeq[Int]].unsafeArray
val res12: Array[_] = Array(1, 2, 3)

```

If that is not exactly what you want, you can roll your own. I know if this discussion continues for a bit, Ichoran will chime in to say he needs this on occasion and has one in his library.
