29
votes

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 = ()
3
Pattern matching matches what is already there. You cannot use the act of matching to transform your data! You're asking for, "Given a list of strings, if they are already a list of telecoms, then (blah blah)!"Rex Kerr

3 Answers

41
votes

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

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

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

Or you can make that:

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