list 将 Scala 列表转换为另一种类型的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6389063/
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-09-11 01:40:27  来源:igfitidea点击:

Convert Scala List to List with another type

listscalapattern-matching

提问by Noel

I want to create a more complex object type List from a simple type List. Eg, List[String] => List[MyType].

我想从一个简单的类型列表创建一个更复杂的对象类型列表。例如,List[String] => List[MyType]

I've given it three goes using map-based approaches. A simple map with wildcard:

我已经使用基于地图的方法给了它三步。带通配符的简单地图:

> case class telecom (name:String, longitude:Double, latitude:Double)
defined class telecom
> List("foo","bar").map(x:String => telecom(x,0,0)):List[telecom]
:1: error: ';' expected but ')' found.

A pattern-matching method that uses the case class constructor:

使用 case 类构造函数的模式匹配方法:

> def foo(c:List[String]){                                                                              
 | c match {                                                                                             
 | case tc:List[telecom] => tc.map(telecom(_,0,0)):List[telecom]; println("matched telephonecomapny");
 | case _ => println("matched nothing"); throw new ClassCastException(); }}
warning: there were unchecked warnings; re-run with -unchecked for details
foo: (c: List[String])Unit

>  foo(List("foo","bar"))
java.lang.ClassCastException: java.lang.String cannot be cast to usda.rd.broadband.model.DatabaseTables$TelephoneCompany
    at $anonfun$foo.apply(<console>:11)
    at scala.collection.TraversableLike$$anonfun$map.apply(TraversableLike.scala:206)
    at scala.collection.TraversableLike$$anonfun$map.apply(TraversableLike.scala:206)
    at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:61)
    at scala.collection.immutable.List.foreach(List.scala:45)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:206)
    at scala.collection.immutable.List.map(List.scala:45)
    at .foo(<console>:11)
    at .<init>(<console>:11)
    at .<clinit>(<console>)
    at RequestResult$.<init>(<console>:9)
    at RequestResult$.<clinit>(<console>)
    at RequestResult$scala_repl_result(<console...

and a simpler pattern-matching method:

和一个更简单的模式匹配方法:

> def bar(c:List[String]){
 | c match {
 | case tc:List[telecom] => tc 
 | case _ => println("matched nothing")}}
 warning: there were unchecked warnings; re-run with -unchecked for details
 foo: (c: List[String])Unit
> val r = bar(List("foo","bar"))
t: Unit = ()

回答by paradigmatic

The first try is quite OK. You just forgot to use parenthesis around lambda function arguments. Instead of:

第一次尝试还可以。您只是忘记在 lambda 函数参数周围使用括号。代替:

List("foo","bar").map(x:String => telecom(x,0,0)):List[telecom]

you should use:

你应该使用:

List("foo","bar").map( (x:String) => telecom(x,0,0)):List[telecom]

or simpler:

或更简单:

List("foo","bar").map( x => telecom(x,0,0))

回答by Daniel C. Sobral

In the interest of one-upmanship, I must say it can be further reduced to

为了一上来,我必须说它可以进一步简化为

List("foo","bar").map(telecom(_,0,0))

回答by dvigal

Or you can make that:

或者你可以这样做:

List("foo","bar").map(x => telecom(x,0,0))