如何使用高阶函数展平选项列表?
使用 Scala 2.7.7:
如果我有一个选项列表,我可以使用 for 理解来展平它们:
val listOfOptions = List(None, Some("hi"), None)
listOfOptions: List[Option[java.lang.String]] = List(None, Some(hi), None)
scala> for (opt <- listOfOptions; string <- opt) yield string
res0: List[java.lang.String] = List(hi)
我不喜欢这种风格,宁愿使用 HOF。这种尝试过于冗长,无法接受:
scala> listOfOptions.flatMap(opt => if (opt.isDefined) Some(opt.get) else None)
res1: List[java.lang.String] = List(hi)
直觉上我希望以下内容能够工作,但事实并非如此:
scala> List.flatten(listOfOptions)
<console>:6: error: type mismatch;
found : List[Option[java.lang.String]]
required: List[List[?]]
List.flatten(listOfOptions)
即使以下内容似乎应该工作,但事实并非如此:
scala> listOfOptions.flatMap(_: Option[String])
<console>:6: error: type mismatch;
found : Option[String]
required: (Option[java.lang.String]) => Iterable[?]
listOfOptions.flatMap(_: Option[String])
^
我能想到的最好的办法是:
scala> listOfOptions.flatMap(_.toList)
res2: List[java.lang.String] = List(hi)
..但我宁愿不必将选项转换为列表。这看起来很笨重。
有什么建议吗?
Using Scala 2.7.7:
If I have a list of Options, I can flatten them using a for-comprehension:
val listOfOptions = List(None, Some("hi"), None)
listOfOptions: List[Option[java.lang.String]] = List(None, Some(hi), None)
scala> for (opt <- listOfOptions; string <- opt) yield string
res0: List[java.lang.String] = List(hi)
I don't like this style, and would rather use a HOF. This attempt is too verbose to be acceptable:
scala> listOfOptions.flatMap(opt => if (opt.isDefined) Some(opt.get) else None)
res1: List[java.lang.String] = List(hi)
Intuitively I would have expected the following to work, but it doesn't:
scala> List.flatten(listOfOptions)
<console>:6: error: type mismatch;
found : List[Option[java.lang.String]]
required: List[List[?]]
List.flatten(listOfOptions)
Even the following seems like it should work, but doesn't:
scala> listOfOptions.flatMap(_: Option[String])
<console>:6: error: type mismatch;
found : Option[String]
required: (Option[java.lang.String]) => Iterable[?]
listOfOptions.flatMap(_: Option[String])
^
The best I can come up with is:
scala> listOfOptions.flatMap(_.toList)
res2: List[java.lang.String] = List(hi)
... but I would much rather not have to convert the option to a list. That seems clunky.
Any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Scala 2.8 中,扁平化将起作用:
但是,这在 2.7.7 中不起作用:
集合库已重新设计,并且在 2.8 中改进了很多,因此也许您可能想尝试使用最新的 Scala 2.8 RC 和看看这是否会让您更容易使用。
如果你真的不想使用 toList 方法,我想你也可以这样写:
也许也不是一件好事,但至少这在 2.7.7 中有效。
In Scala 2.8, flatten will work:
This doesn't work in 2.7.7, however:
The collections library has been redesigned, and has improved a lot in 2.8, so perhaps you might want to try to use the latest Scala 2.8 RC and see if that makes it more easy to use for you.
If you really don't want to use the toList method, I guess you can also write it like this:
Also not a thing of beauty perhaps, but at least this works in 2.7.7.
为了补充 Arjan 的答案,在 Scala 2.7.7 中,您可以使用
List#flatten
,但您需要帮助类型推断器:To complement Arjan's answer, in Scala 2.7.7 you can use
List#flatten
, but you need to help out the type inferencer: