Is it possible to print values during collect without modifying return type?
scala, scala-2.9, scala-collections
Solution
`flatMap`
List("A", "B", "C") flatMap {
case "A" => List(1)
case "B" => List(2)
case x => println(x); Nil
}
`collect`/`flatten`
List("A", "B", "C").collect {
case "A" => Some(1)
case "B" => Some(2)
case x => println(x); None
}.flatten
Problem
I have a code segment something like this: ``` def test() : Seq[Int] = List("A", "B", "C") collect { case "A" => 1 case "B" => 2 //case _ => println(_) } ``` Now I would like to print specific values (just for debugging) on the output without adding any elements to the resulting collection. If I uncomment the commented line, Scala infers the value of the expression to `Seq[Any]`, which is completely understandable. Anybody got any hints how to do this? Thanks in advance!