scala 如何从范围创建列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1408583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 01:38:44  来源:igfitidea点击:

How to create List from Range

algorithmscala

提问by Ula Krukar

I am new to Scala, just started learning, so this is basic beginner question.

我是 Scala 的新手,刚开始学习,所以这是基本的初学者问题。

I try to implement Sieve of Eratosthenesalgorithm. Here is what I got so far:

我尝试实现Eratosthenes算法的筛分。这是我到目前为止所得到的:

def sieve_core(cross: Int, lst: Seq[Int]): List[Int] = {
    val crossed = lst.filter(_ % cross != 0)
    crossed match {
            case a :: rest => cross :: sieve_core(a, crossed)
            case _ => cross :: Nil
    }
}

def sieve(max: Int): List[Int] = {
    sieve_core(2, (2 to max))
}

println(sieve(100))

The result is:

结果是:

List(2)

As far as I understand, case _ => cross :: Nilis matched in first iteration of sieve_core, which means that crossedis not an instance of a List.

据我了解,case _ => cross :: Nil在 的第一次迭代中匹配sieve_core,这意味着它crossed不是 List 的实例。

I changed lstparameters type to List[Int]and now the code won't compile with an error:

我将lst参数类型更改为List[Int],现在代码不会编译并出现错误:

(fragment of Problem3.scala):24: error: type mismatch;
 found   : Range.Inclusive
 required: List[Int]
    sieve_core(2, (2 to max))
                      ^

Apparently Rangeis not a List.

显然Range不是List.

Question: how can I turn Range into a List? Or is it some bigger problem with my code, I have made some bad assumption somewhere along the way?

问题:如何将 Range 变成 List?还是我的代码有更大的问题,我在此过程中的某个地方做了一些错误的假设?

Any help appreciated.

任何帮助表示赞赏。

回答by DigitalRoss

There's an applymethod on the Listcompanion object which takes a range and returns a List:

伴随对象apply上有一个方法,List它接受一个范围并返回一个List

scala> List.range(2, 11)
res0: List[Int] = List(2, 3, 4, 5, 6, 7, 8, 9, 10)

There are lots of useful Listfactory methods in the Listcollection documentation.

有很多有用的List工厂方法List采集文件

回答by Jonathan Graehl

To turn any sequence sinto a list, use s.toList

要将任何序列s转换为列表,请使用s.toList

I'm sure digitalross' is more efficient in this case, though.

不过,我确信在这种情况下,digitalross 的效率更高。

回答by Sal Borrelli

(2 to max)is not a scala.collection.immutable.Listindeed but a scala.collection.immutable.Range, more precisely an instance of scala.collection.immutable.Range.Inclusive, as mentioned in your error message. Just in passing note that Inclusiveand Exclusiveare themselves members of Range, with a fairly auto-explanatory meaning.

(2 to max)确实不是scala.collection.immutable.List而是scala.collection.immutable.Range,更准确地说是 的实例scala.collection.immutable.Range.Inclusive,如您的错误消息中所述。顺便提一下,InclusiveExclusive本身就是 的成员Range,具有相当不言自明的含义。

Luckily the Range class offers the handy method toList, which you can leverage for converting the range into a list and resolve the problem, like in the following code snippet:

幸运的是 Range 类提供了方便的方法toList,您可以利用该方法将范围转换为列表并解决问题,如下面的代码片段所示:

scala> val max = 10
max: Int = 10

scala> val r = (2 to max)
r: scala.collection.immutable.Range.Inclusive = Range 2 to 10

scala> val l = r.toList
l: List[Int] = List(2, 3, 4, 5, 6, 7, 8, 9, 10)