2

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.

vik santata
  • 2,989
  • 8
  • 30
  • 52

2 Answers2

7

Calling .toList on a Range forces evaluation:

(10 to 1000).toList
Akos Krivachy
  • 4,915
  • 21
  • 28
4

Even more directly:

List.range(10, 1001)
The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134