将 Scala 列表转换为其他类型的列表

发布于 2024-11-15 23:55:56 字数 2160 浏览 1 评论 0原文

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

我已经使用基于地图的方法进行了三次尝试。带有通配符的简单映射:

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

使用 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$1.apply(<console>:11)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:206)
    at scala.collection.TraversableLike$$anonfun$map$1.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...

以及更简单的模式匹配方法:

> 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 = ()

I want to create a more complex object type List from a simple type List. Eg, 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:

> 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$1.apply(<console>:11)
    at scala.collection.TraversableLike$anonfun$map$1.apply(TraversableLike.scala:206)
    at scala.collection.TraversableLike$anonfun$map$1.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 = ()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若相惜即相离 2024-11-22 23:55:56

第一次尝试还是很OK的。您只是忘记在 lambda 函数参数周围使用括号。而不是:

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

你应该使用:

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

或更简单:

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

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

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))
昵称有卵用 2024-11-22 23:55:56

为了胜人一筹,我必须说它可以进一步简化为

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

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

List("foo","bar").map(telecom(_,0,0))
澉约 2024-11-22 23:55:56

或者你可以这样做:

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

Or you can make that:

List("foo","bar").map(x => telecom(x,0,0))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文