title: Return/break out of infinite foreach in kotlin
body: <p>For class I have to make a program that calculates the birthday problem
  Now I'm having trying to learn kotlin at the same time and I'm having trouble with a little snippet of code:</p>
  
  <pre><code>        val checkSet = mutableSetOf&lt;Int&gt;()
          generateSequence{ Random.nextInt(n)}.forEach {
              if(!checkSet.add(it)) {
                  return@outForeach
              }
          }
          outForeach@
          sum += checkSet.size
  </code></pre>
  
  <p>As you can see I'm trying to do this with an infinite sequence. Kotlin doesn't accept this as outForeach is an unresolved reference. But this doesn't work either:</p>
  
  <pre><code>        val checkSet = mutableSetOf&lt;Int&gt;()
          generateSequence{ Random.nextInt(n)}.forEach {
              if(!checkSet.add(it)) {
                  return@forEach
              }
          }
          sum += checkSet.size
  </code></pre>
  
  <p>This will just start the forEach loop again. Is there a way to implement something as a forEachUntil or so?</p>
  
  <p>p.s. I'm aware that this looks a lot like this question: <a href="https://stackoverflow.com/questions/48056994/return-doesnt-jump-out-of-foreach-in-kotlin">&#39;return&#39; doesn&#39;t jump out of forEach in Kotlin</a> It's just that I don't really get the answers and I don't know if its applicable here. Also a way to implement forEachUntil seems for me to be far more elegant</p>