scala - 模式匹配或 Map Monads

标签 scala

我想知道你们认为什么在性能方面更惯用和更好。
在 Option 或 Try 类型的 monad 上,如果您想控制副作用,请使用模式匹配或 map 和 getOrElse。
那么你认为什么更好:

maybeConnectTimeout
  .map(connectTimeout => session.connect(connectTimeout))
  .getOrElse(session.connect())
或这个
maybeConnectTimeout match {
  case Some(connectTimeout) => session.connect(connectTimeout)
  case None => session.connect()
}

最佳答案

what ... is ... better in terms of performance.


根据 odersky模式匹配

I am surprised that the pattern match gets so little love here. Not only is it by far the fastest (probably at least 10 times as fast as the alternatives), it is also the clearest.

关于scala - 模式匹配或 Map Monads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69238744/

相关文章:

scala - 运算符作为函数字面量

scala - 如何在 Scala/SBT 中安装 Guice?

scala - Scala 2.12 中的隐式 ExecutionContext 优先级

scala - 奇怪的scala编译不会失败,但是给出错误的答案

scala - 所有这些“要么”的问题是怎么回事?

scala - 我可以在Scala中使括号重载吗?

javascript - 在scala js中导入电子浏览器窗口

scala - 弗林克 : PageRank type mismatch error

scala - 如何使在 Scala 中实现的树对高阶集合函数有用?

Java/斯卡拉 : understanding Play framework custom tag or helper