我需要将两个可能为空的地址行连接成一个(两行之间有一个空格),但是None如果两个地址行都是None(此字段将进入一个Option[String]变量),我需要它返回一个。下面的命令让我得到我想要的连接:
Seq(myobj.address1, myobj.address2).flatten.mkString(" ")
Run Code Online (Sandbox Code Playgroud)
但这给了我一个空字符串而不是一个None,以防 address1 和 address2 都是None。
这将单个字符串Option转换为 ,None如果它是null或空修剪的字符串,则将其转换为:
Option(x).collect { case x if x.trim.nonEmpty => x }
Run Code Online (Sandbox Code Playgroud)
假设:
val list1 = List(Some("aaaa"), Some("bbbb"))
val list2 = List(None, None)
Run Code Online (Sandbox Code Playgroud)
使用普通 Scala:
scala> Option(list1).map(_.flatten).filter(_.nonEmpty).map(_.mkString(" "))
res38: Option[String] = Some(aaaa bbbb)
scala> Option(list2).map(_.flatten).filter(_.nonEmpty).map(_.mkString(" "))
res39: Option[String] = None
Run Code Online (Sandbox Code Playgroud)
或者使用 scalaz:
import scalaz._; import Scalaz._
scala> list1.flatten.toNel.map(_.toList.mkString(" "))
res35: Option[String] = Some(aaaa bbbb)
scala> list2.flatten.toNel.map(_.toList.mkString(" "))
res36: Option[String] = None
Run Code Online (Sandbox Code Playgroud)
嗯,在 Scala 中,有一种Option[ T ]类型旨在消除由于空值而导致的各种运行时问题。
所以...这是您使用选项的方式,所以基本上 aOption[ T ]可以具有两种类型的值之一 -Some[ T ]或None
// A nice string
var niceStr = "I am a nice String"
// A nice String option
var noceStrOption: Option[ String ] = Some( niceStr )
// A None option
var noneStrOption: Option[ String ] = None
Run Code Online (Sandbox Code Playgroud)
现在来谈谈你的问题部分:
// lets say both of your myobj.address1 and myobj.address2 were normal Strings... then you would not have needed to flatten them... this would have worked..
var yourString = Seq(myobj.address1, myobj.address2).mkString(" ")
// But since both of them were Option[ String ] you had to flatten the Sequence[ Option[ String ] ] to become a Sequence[ String ]
var yourString = Seq(myobj.address1, myobj.address2).flatten.mkString(" ")
//So... what really happens when you flatten a Sequence[ Option[ String ] ] ?
// Lets say we have Sequence[ Option [ String ] ], like this
var seqOfStringOptions = Seq( Some( "dsf" ), None, Some( "sdf" ) )
print( seqOfStringOptions )
// List( Some(dsf), None, Some(sdf))
//Now... lets flatten it out...
var flatSeqOfStrings = seqOfStringOptions.flatten
print( flatSeqOfStrings )
// List( dsf, sdf )
// So... basically all those Option[ String ] which were None are ignored and only Some[ String ] are converted to Strings.
// So... that means if both address1 and address2 were None... your flattened list would be empty.
// Now what happens when we create a String out of an empty list of Strings...
var emptyStringList: List[ String ] = List()
var stringFromEmptyList = emptyStringList.mkString( " " )
print( stringFromEmptyList )
// ""
// So... you get an empty String
// Which means we are sure that yourString will always be a String... though it can be empty (ie - "").
// Now that we are sure that yourString will alwyas be a String, we can use pattern matching to get out Option[ String ] .
// Getting an appropriate Option for yourString
var yourRequiredOption: Option[ String ] = yourString match {
// In case yourString is "" give None.
case "" => None
// If case your string is not "" give Some[ yourString ]
case someStringVal => Some( someStringVal )
}
Run Code Online (Sandbox Code Playgroud)