This compiles
//legal
def s1 = List("aaa","bbb").collect { case x => x.split("\\w") }
The following don’t.
// all illegal
// missing parameter type for expanded function ((x$2) => x$2.split{<null>}("\\w"{<null>} {<null>}){<null>}
def s2 = List("aaa","bbb").collect ( _.split("\\w") )
// missing parameter type
def s3 = List("aaa","bbb").collect ( x => x.split("\\w") )
// type mismatch; found : String => Array[java.lang.String] required: PartialFunction[java.lang.String,?]
def s4 = List("aaa","bbb").collect ( (x:String) => x.split("\\w") )
And whilst the scala compiler is doing his best to communicate with me where my errors lie, it’s going right over my head.
The fact that this also compiles
def s2 = List("aaa","bbb").find ( _.split("\\w").length > 2 )
makes it all the more confusing when to use what
The reason why second part does not compile is that
List#collecttakesPartialFunction[A, B]as a param so it is possible to specify function to apply and filter elements that you what this function to be applied.For example:
will be applied only to integer elements and return
List(2, 3)which isList[Int]In your case you can use
mapandfilterfunctions to do the workdef s1 = List("aaa","bbb").map(_.split("\\w")).filter(_.length > 2)