将 Scala 列表转换为其他类型的列表
我想从简单类型列表创建更复杂的对象类型列表。例如,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一次尝试还是很OK的。您只是忘记在 lambda 函数参数周围使用括号。而不是:
你应该使用:
或更简单:
The first try is quite OK. You just forgot to use parenthesis around lambda function arguments. Instead of:
you should use:
or simpler:
为了胜人一筹,我必须说它可以进一步简化为
In the interest of one-upmanship, I must say it can be further reduced to
或者你可以这样做:
Or you can make that: