映射 getOrElse 的 scala 更好的语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12504650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:32:54  来源:igfitidea点击:

scala better syntax for map getOrElse

scala

提问by pbaris

is there a better way to write the code below?

有没有更好的方法来编写下面的代码?

val t = map.get('type).getOrElse(""); 
if (t != "") "prefix" + t;

be interested in inline code something like

对内联代码感兴趣

val t = map.get('type).getOrElse("").????

回答by Travis Brown

Maphas its own getOrElsemethod, so you can just write the following:

Map有它自己的getOrElse方法,所以你可以只写以下内容:

val t = map.getOrElse('type, "")

Which accomplishes the same thing as the definition of tin your first example.

这完成了与t您的第一个示例中的定义相同的事情。



To address your comment: If you know your map will never contain the empty string as a value, you can use the following to add the "prefix":

解决您的评论:如果您知道您的地图永远不会包含空字符串作为值,您可以使用以下内容添加"prefix"

map.get('type).map("prefix" + _).getOrElse("")

Or, if you're using Scala 2.10:

或者,如果您使用的是 Scala 2.10:

map.get('type).fold("")("prefix" + _)

If your map canhave ""values, this version will behave a little differently than yours, since it will add the prefix to those values. If you want exactly the same behavior as your version in a one-liner, you can write the following:

如果您的地图可以""值,则此版本的行为将与您的略有不同,因为它将为这些值添加前缀。如果您希望在单行中与您的版本完全相同,则可以编写以下内容:

map.get('type).filter(_.nonEmpty).map("prefix" + _).getOrElse("")

This probably isn't necessary, though—it sounds like you don't expect to have empty strings in your map.

不过,这可能不是必需的 - 听起来您不希望地图中有空字符串。

回答by Dave Griffith

It's also worth noting that, in certain cases, you can replace multiple common .getOrElseusages with one .withDefaultValuecall.

还值得注意的是,在某些情况下,您可以.getOrElse用一次.withDefaultValue调用替换多个常见用法。

val map = complexMapCalculation().withDefaultValue("")

val t = map('type)

I wouldn't say this is something that should be done every time, but it can be handy.

我不会说这是每次都应该做的事情,但它可以很方便。

回答by Taylor Leese

You could also use the Scalaz Zerotypeclass so your code would look like below. The unary operator is defined on OptionW.

您还可以使用 Scalaz Zero类型类,以便您的代码如下所示。一元运算符在OptionW上定义。

val t = ~map.get('type)                 // no prefix
val t = ~map.get('type).map("prefix"+_) // prefix

Here's an example session:

这是一个示例会话:

scala> import scalaz._; import Scalaz._
import scalaz._
import Scalaz._

scala> val map = Map('type -> "foo")
map: scala.collection.immutable.Map[Symbol,java.lang.String] = Map('type -> foo)

scala> ~map.get('type)
res3: java.lang.String = foo

scala> ~map.get('notype)
res4: java.lang.String = ""

scala> ~map.get('type).map("prefix"+_)
res5: java.lang.String = prefixfoo

scala> ~map.get('notype).map("prefix"+_)
res6: java.lang.String = ""

回答by shyan1

While reading the book Play for Scala, I picked up this code snippet that was defined in a controller, which might be a better syntax for getOrElse.

在阅读这本书时Play for Scala,我拿起了在控制器中定义的这段代码片段,这可能是getOrElse更好的语法。

def show(ean: Long) = Action { implicit request =>

  Product.findByEan(ean).map { product =>
    Ok(views.html.products.details(product))
  }.getOrElse(NotFound)

}

In which, the Product.findByEan(ean: Long)was defined like

其中,Product.findByEan(ean: Long)被定义为

def findByEan(ean: Long) = products.find(_ean == ean)