How to let mkString skip null in scala?

scala> Seq("abc", null).mkString(" ")
res0: String = abc null

but I want to get "abc" only

Is there a scala way to skip nulls?

Answers


scala> val seq = Seq("abc", null, "def")
seq: Seq[String] = List(abc, null, def)

scala> seq.flatMap(Option[String]).mkString(" ")
res0: String = abc def

There's always Seq("abc", null).filter(_ != null).mkString(" ")


Combination of Rex's answer and Eric's first comment:

Seq("abc", null).map(Option(_)).collect{case Some(x) => x}.mkString(" ")

The first map wraps the values resulting in Seq[Option[String]]. collect then essentially does a filter and map, discarding the None values and leaving only the unwrapped Some values.


Need Your Help

changing mingw' startup directory or creating mingw symlinks

windows mingw

whats the easiest way to set mingw's console startup directory? i only use mingw for compiling. but due to the lack of editors or even symlinks i am confused how to tell mingw console to come up in a

uikeyboard leaves a black portion on top on navigation

uikeyboard ios5.1 uinavigation

I have a form on the ipad which has many textfield and a button at the end. There are some fields which come under the keyboard when it is active. In order to pull the hidden texfield behind the ke...