在 Scala 中替换 if-without-else
Replace if-without-else in Scala
在Scala中,你通常是如何用函数式的方式替换if-without-else的?
例如,像命令式风格中的这种典型模式:
var list = List("a", "b", "c")
if (flag) { // flag is boolean variable
// do something inside if flag is true
list = "x" :: list
}
// if flag is false, nothing happened
我想这样让它发挥作用:
val tempList = List("a", "b", "c")
val list = if (flag) "x" :: tempList else tempList
有没有不使用中间变量的更好方法?
所以任何人都可以分享你如何在 scala 中消除 if-without-else?
通常最好避免使用临时变量使命名空间混乱。所以像这样会更好:
val list = {
val temp = List("a", "b", "c")
if (flag) "x" :: temp else temp
}
或
val list = List("a", "b", "c") match {
case x if flag => "x" :: x
case x => x
}
如果您发现自己经常这样做很多并且性能不是一个大问题,那么定义一个扩展方法可能会很方便。我的个人图书馆中有一个如下所示:
implicit class AnythingCanPickFn[A](private val underlying: A) extends AnyVal {
/** Transforms self according to `f` for those values where `p` is true. */
@inline def pickFn(p: A => Boolean)(f: A => A) =
if (p(underlying)) f(underlying) else underlying
}
你会像这样使用这个:
List("a", "b", "c").pickFn(_ => flag){ "x" :: _ }
可以使用 scalaz 的三元运算符来完成
import scalaz._
import Scalaz._
val temp = List("a", "b", "c")
val list = (flag) ? ("x" :: temp) | temp
libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.0.6"
您可以通过将可选部分设为可选来为猫剥皮:
scala> val flag = true
flag: Boolean = true
scala> Option("x").filter(_ => flag).toList ::: "a" :: "b" :: "c" :: Nil
res0: List[String] = List(x, a, b, c)
scala> val flag = false
flag: Boolean = false
scala> Option("x").filter(_ => flag).toList ::: "a" :: "b" :: "c" :: Nil
res1: List[String] = List(a, b, c)
或相反
scala> sys.props.get("flag")
res3: Option[String] = None
scala> sys.props.get("flag").map(_ => "x").toList ::: List("a","b","c")
res4: List[String] = List(a, b, c)
scala> sys.props("flag") = "true"
scala> sys.props.get("flag").map(_ => "x").toList ::: List("a","b","c")
res6: List[String] = List(x, a, b, c)
在Scala中,你通常是如何用函数式的方式替换if-without-else的?
例如,像命令式风格中的这种典型模式:
var list = List("a", "b", "c")
if (flag) { // flag is boolean variable
// do something inside if flag is true
list = "x" :: list
}
// if flag is false, nothing happened
我想这样让它发挥作用:
val tempList = List("a", "b", "c")
val list = if (flag) "x" :: tempList else tempList
有没有不使用中间变量的更好方法?
所以任何人都可以分享你如何在 scala 中消除 if-without-else?
通常最好避免使用临时变量使命名空间混乱。所以像这样会更好:
val list = {
val temp = List("a", "b", "c")
if (flag) "x" :: temp else temp
}
或
val list = List("a", "b", "c") match {
case x if flag => "x" :: x
case x => x
}
如果您发现自己经常这样做很多并且性能不是一个大问题,那么定义一个扩展方法可能会很方便。我的个人图书馆中有一个如下所示:
implicit class AnythingCanPickFn[A](private val underlying: A) extends AnyVal {
/** Transforms self according to `f` for those values where `p` is true. */
@inline def pickFn(p: A => Boolean)(f: A => A) =
if (p(underlying)) f(underlying) else underlying
}
你会像这样使用这个:
List("a", "b", "c").pickFn(_ => flag){ "x" :: _ }
可以使用 scalaz 的三元运算符来完成
import scalaz._
import Scalaz._
val temp = List("a", "b", "c")
val list = (flag) ? ("x" :: temp) | temp
libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.0.6"
您可以通过将可选部分设为可选来为猫剥皮:
scala> val flag = true
flag: Boolean = true
scala> Option("x").filter(_ => flag).toList ::: "a" :: "b" :: "c" :: Nil
res0: List[String] = List(x, a, b, c)
scala> val flag = false
flag: Boolean = false
scala> Option("x").filter(_ => flag).toList ::: "a" :: "b" :: "c" :: Nil
res1: List[String] = List(a, b, c)
或相反
scala> sys.props.get("flag")
res3: Option[String] = None
scala> sys.props.get("flag").map(_ => "x").toList ::: List("a","b","c")
res4: List[String] = List(a, b, c)
scala> sys.props("flag") = "true"
scala> sys.props.get("flag").map(_ => "x").toList ::: List("a","b","c")
res6: List[String] = List(x, a, b, c)