title: Kotlin infinite sequences with iterator function
body: <p>I am confused about how to create an infinite sequence in Kotlin to use for lazy evaluation. </p>
  
  <p>In Java:</p>
  
  <pre><code>IntStream.iterate(0, i -&gt; i + 2)
       .limit(100)
       .forEach(System.out::println);
  </code></pre>
  
  <p>but sequences seem much more confusing then Java streams. The sequence constructor is very confusing the doc for it says:</p>
  
  <pre><code>/**
   * Given an [iterator] function constructs a [Sequence] that returns values through the [Iterator]
   * provided by that function.
   * The values are evaluated lazily, and the sequence is potentially infinite.
   */
  </code></pre>
  
  <p>but I don't know what it means by an iterator function or how to make one.</p>
  
  <pre><code>Sequence { iterator(arrayOf&lt;Int&gt;()) }
          .forEach { print(it) }
  </code></pre>
  
  <p>I have this which compiles but obviously doesn't print anything. I don't think my iterator function makes any sense. It wants a function that takes no arguments and returns an iterator, which isn't like the Java <code>.iterate</code> function at all. Iterator happens to have a constructor that takes an array, which would make sense if I had a data set to work with in an array but I don't. I want to be working with an infinite sequence. </p>
  
  <p>There is no <code>.limit</code>, so I previously tried to add a <code>.reduce</code> but the arguments for <code>.reduce</code> were even more confusing. I think there should be a <code>.toList</code> but I knew it wasn't working so I didn't try it.</p>
  
  <p>If someone would show me how to implement the above Java code in Kotlin it would help a lot.</p>