scala Range for Long

后端 未结 3 1011
长情又很酷
长情又很酷 2021-01-12 18:21

I\'m new to the Scala language.

I need Range for Long type.

I need a List of [1, 2, 3 ... 10000000] with step 1. If I use until/to I get an error because of

3条回答
  •  醉梦人生
    2021-01-12 19:15

    You can create such a range by using the following syntax:

    val range = 1L to 10000000L
    

    The 'L' is mandatory to inform the compiler that the litterals are longs and not ints.

    You can then use almost all List methods on the instance range. It should not fill you memory because the intermediate values are generated when needed. The range can be passed to any method expecting a Traversable[Long], a Seq[Long], an Iterable[Long], etc.

    However, if you really need a List just call range.toList (and increase the heap size to accommodate all the list elements)...

提交回复
热议问题