scala Range for Long

2020-03-21 05:54发布

问题:

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 using Long instead of Int.

I try to write simple function which expects a start, an end and and an empty List and generates a List of [start .. end].

Here is my function:

def range_l(start : Long, end : Long, list : List[Long]) : List[Long] = {
    if (start == end){
        val add_to_list = start :: list
        return add_to_list
    }
    else {
        val add_to_list = start :: list
        range_l(start + 1, end, add_to_list)
    }
}

If I call it like: range_l(1L, 1000000L, List()) i get OutOfMemory error in the following line: add_to_list = start :: list

What can you advice me? How can I get Range[Long] or how can I optimize the function. How can I avoid OutOfMemory?

Thank you.

回答1:

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)...



回答2:

You can instead use NumericRange[Long] from the standard library.



回答3:

You probably do not need a range. I would take a Stream and iterate over it.

def stream(i: Long = 1): Stream[Long] = i #:: stream(i + 1)

produces an infinit Stream where the difference between the elements is 1. Because Stream is a lazy collection you will not gett any Errors. To iterate over 10000000 elements you simply use the following:

val range = stream take 10000000
for (i <- range) {
  ...
}

take 10000000 will return a Stream with size 10000000. Because Stream is an Iterable you can pass it to a for comprehansion.