Scala: how to create an "eager evaluated" list with many elements?

455 views Asked by At

If I create list like

List(10 to 1000)

actually,the List only contains a range object, and the list members are generated dynamically when visiting specific elements.

But my requirement is to construct a real List(10,11,12...1000) without having a range object. I found

"for ... yield"

is also lazy evaluation, thus doesn't match my need. If I don't want to use a for loop to append elements to an empty List, is there a convenient way to do so?

Thanks a lot.

2

There are 2 answers

0
The Archetypal Paul On

Even more directly:

List.range(10, 1001)
0
Akos Krivachy On

Calling .toList on a Range forces evaluation:

(10 to 1000).toList