如何尽早在Scala中回归 [英] How to early return in Scala

查看:87
本文介绍了如何尽早在Scala中回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习Scala。我喜欢做的一件事是提早回报。我相信,这对于每个人来说都更容易阅读,因为我们之前只是删除了无效的状态。现在,由于Scala是一种函数式语言,并且我已经读过裁减计算是不好的函数式风格,所以我想知道是否有一些技巧或函数式编程等同于早期返回。



这是我要写的代码,要完全清楚,这仅仅是一个愚蠢的例子,我并不是在寻找我特殊情况下的特殊黑客攻击,而是更多地寻求如何处理这些攻击的一般性建议。 / p>

  if(request.headers.get(session_header).isEmpty){
BadRequest(session_not_set)
} else {
Ok(CartHelper.getCart(session,user))
}

现在,我想要做的是:

  if(request.headers.get(session_header).isEmpty){ 
BadRequest(session_not_set)
return;

$ b $ OK(CartHelper.getCart(session,user))

如果您对我有任何提示!

解决方案

在某些情况下,返回关键字无法避免,但目前看起来您没有这个问题。



情景1 :单一情况场景,您当前的场景。在这种情况下,你可以避免使用 return 以非常简单的 if else

  def doSomething:AnyContent = {
if(request.headers.get(session_header).isEmpty){
BadRequest(session_not_set)
} else {
Ok(CartHelper.getCart(session,user))
}
}

如果没有设置会话是一个常见问题,那么您可以简单地用一个局部函数来监视它。


pf:Session => AnyContent
):AnyContent = {
request.headers.get(session_header)
.fold(BadRequest(Session not set))(pf(_))
}



<

  //假设在这里使用Play框架
def getCart:AnyContent = Action {implicit req =>
requireSession(req){session => Ok(CartHelper.getCart(session,user)}
}

情景2 :使用return来断开循环,或者所谓的early返回通常是一个性能提升元素。

一个显然有效的使用 return 在Scala中,这似乎是不可避免的,在这种情况下,您正在迭代某个集合的第一个东西。显然,您可以使用 collection.find 和其他帮助程序方法预先在标准库中构建,但为了论证。

  def ($ lt;  -  1){
//中断循环找到第一个匹配
//为了效率,
if(el == value)返回true;
}
false;
}

即使在像 return 这样的情况下,通过使用不同的结构是可以避免的,并且始终存在recu rsive版本的东西,你可以用它来代替在dan迭代中显然不可能的 return


I am learning Scala at the moment. One thing that I like to do is early returns. I'm convinced that this is far easier for everyone to read as we just remove the invalid states before. Now, as Scala is a functional language and I've read that cutting computation is bad functional style, I'm wondering if there is some trick or functional programming equivalent to early return.

This is code I would write, to be completely clear, this is just a dumb example, I'm not looking for the special hack of my special case, but more for a general advice on how to deal with these.

if (request.headers.get(session_header).isEmpty) {
  BadRequest(session_not_set)
} else {
  Ok(CartHelper.getCart(session, user))
}

Now, what I'm tempted to do is:

if (request.headers.get(session_header).isEmpty) {
  BadRequest(session_not_set)
  return;
}

Ok(CartHelper.getCart(session,user))

If you have any hint for me!

解决方案

In some instances the return keyword cannot be avoided, but it doesn't look like you have that problem currently.

Scenario 1: The single condition scenario, your current one. In this instance you can avoid using return with a very simple if else.

def doSomething: AnyContent = {
  if (request.headers.get(session_header).isEmpty) {
    BadRequest(session_not_set)
  } else {
    Ok(CartHelper.getCart(session,user))
  }
}

If the session not being set is a common problem, you can simply have a guard around it with a partial function.

def requireSession(req: Request)(
   pf: Session => AnyContent
): AnyContent = {
   request.headers.get(session_header)
     .fold(BadRequest("Session not set"))(pf(_))
}

And then:

// Assuming Play framework being used here
def getCart: AnyContent = Action { implicit req =>
  requireSession(req) { session => Ok(CartHelper.getCart(session, user) }
}

Scenario 2: Break loop using return, or the so called early return is usually a performance improvement element.

One apparently valid use of return in Scala which is something that seems unavoidable is a situation where you are iterating a collection for the first of something. Obviously you can have that abstracted away for you using collection.find and other helper methods pre-build in the standard lib, but for the sake of argument.

def inList[T](l: List[T], value: T): Boolean = {
  for (el <- l) {
    // break the loop for the first match found
    // for the sake of efficiency.
    if (el == value) return true;
  }
  false;
}

Even in situations like this return is avoidable, by using a different construct, and there's always a recursive version of something you can use to replace an apparently impossible return inside dan iteration.

这篇关于如何尽早在Scala中回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆