Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Scala List to List with another type

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 = () 
like image 642
Noel Avatar asked Jun 17 '11 16:06

Noel


2 Answers

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)) 
like image 191
paradigmatic Avatar answered Sep 24 '22 10:09

paradigmatic


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

List("foo","bar").map(telecom(_,0,0)) 
like image 26
Daniel C. Sobral Avatar answered Sep 24 '22 10:09

Daniel C. Sobral