Convert Scala List to List with another type

Noel picture Noel · Jun 17, 2011 · Viewed 28.3k times · Source

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

Answer

paradigmatic picture paradigmatic · Jun 17, 2011

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