嗯,在Scala中有一个Option[ T ]类型,它旨在消除由于空值引起的各种运行时问题。 所以..。下面是如何使用选项,因此基本上Option[ 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
现在来谈谈你的问题:
// 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 )
}
val list1 = List(Some("aaaa"), Some("bbbb"))
val list2 = List(None, None, Some(""))
// yields Some(aaaa bbbbb)
println(Option(list1.map(_.collect { case x if x.trim.nonEmpty => x }))
.map(_.flatten).filter(_.nonEmpty).map(_.mkString(" ")))
// yields None
println(Option(list2.map(_.collect { case x if x.trim.nonEmpty => x }))
.map(_.flatten).filter(_.nonEmpty).map(_.mkString(" ")))
import scala.util.chaining.scalaUtilChainingOps
object TEST123 {
def main(args: Array[String]): Unit = {
val address1 = ""
val address2 = ""
val result =
Seq(
address1 pipe string_to_Option,
address2 pipe string_to_Option
).flatten.mkString(" ") pipe string_to_Option
println(s"The result is «${result}»")
// prints out: The result is «None»
}
}
8条答案
按热度按时间yfjy0ee71#
这会将单个字符串转换为
Option
,如果它是null
或空修剪字符串,则将其转换为None
:替代版本,使用
collect
:4ioopgfo2#
假设:
使用普通Scala:
或使用scalaz:
ee7vknir3#
嗯,在Scala中有一个
Option[ T ]
类型,它旨在消除由于空值引起的各种运行时问题。所以..。下面是如何使用选项,因此基本上
Option[ T ]
可以有两种类型的值-Some[ T ]
或None
现在来谈谈你的问题:
xqnpmsa84#
您还可以在此处使用
reduce
方法:9udxz4iz5#
以下是一个应该解决原始问题的函数。
9w11ddsr6#
answer from @dk14实际上不正确/不完整,因为如果
list2
具有Some("")
,则不会生成None
,因为filter()
的计算结果为空列表,而不是None
(ScalaFiddle link)但已经很接近了。您只需确保将空字符串转换为
None
,以便we combine it with @juanmirocks answer(ScalaFiddle link):k7fdbhmy7#
我在标准库中搜索了一种类似下面的helper函数,但还没有找到,所以我在此期间定义了:
在上面的帮助下,您可以:
xpszyzbs8#
使用Scala 2.13:
例如: