1

The following code hangs the repl:

(
  for {
    i <- 1 to 1000000
    j <- 2 to 1000000
    if i * i == j
  } yield i -> j
).take(1)

It seems the for expression is eagerly evaluated. Any solutions?

qed
  • 22,298
  • 21
  • 125
  • 196

1 Answers1

1

I'd turn that into a stream:

(
    for {
        i <- Stream.range(1, 1000000)
        j <- Stream.range(2, 1000000)
        if i * i == j
    } yield i -> j
).take(1)
bjfletcher
  • 11,168
  • 4
  • 52
  • 67
  • make that `Stream.range(1,10000000)` and you will avoid creating a gigantic `(1 to 1000000)` `Range` instance. – Chirlo Jun 19 '15 at 11:20