【发布时间】:2015-07-01 13:54:58
【问题描述】:
我认为这是不可能的,但也许我错过了什么?
这是一个例子
val sentences = "hello.what is your name?I am Loïc"
sentences.split("\\.|\\?").flatMap { sentence =>
if (sentence.startsWith("h")) sentence.split(" ").map(word => word.toUpperCase)
else if (sentence.startsWith("w")) sentence.split(" ").flatMap(word => List(word.toUpperCase, "-").map(word => word.toUpperCase))
else List(sentence)
}
这可以翻译成一个易于理解的表达式吗?
我注意到,如果我需要在服务响应上进行模式匹配,那么当我在期货上使用 map/flatMap 时(例如在 web 服务调用上),我经常会遇到这种模式。所以我正在努力提高可读性。
谢谢:)
【问题讨论】:
-
你所说的“理解表达”是什么意思?您想将 flatMap 转换为 foreach 循环吗??
-
@eliasah 我的意思是 for { book book.author startsWith " bob").map(book => book.title)
标签: scala