Scala - build Option val by function
A simaple example of return Option value:
def contact(input: List[String]): Option[String] = {
if (input.isEmpty) {
None
} else {
Some(input.reduce(_ + "|" + _))
}
}
scala> def contact(input: List[String]): Option[String] = {
| if (input.isEmpty) {
| None
| } else {
| Some(input.reduce(_ + "|" + _))
| }
| }
contact: (input: List[String])Option[String]
scala> val ss:List[String] = List("1","2","sss","44")
ss: List[String] = List(1, 2, sss, 44)
scala> contact(ss)
res6: Option[String] = Some(1|2|sss|44)
scala> val ss:List[String] = List()
ss: List[String] = List()
scala> contact(ss)
res8: Option[String] = None