Scala语言中的scheme cond

发布于 2024-10-01 01:55:34 字数 33 浏览 1 评论 0原文

scala 有相当于scheme 的 cond 吗?

Does scala have an equivalent to scheme's cond?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撩人痒 2024-10-08 01:55:34

我猜您正在寻找 match (或者只是简单地 if/else if/else)。

I guess you're looking for match (or just simply if/else if/else).

捎一片雪花 2024-10-08 01:55:34
case class Paired(x: Int, y: Int)

def foo(x: Any) = x match {
  case string : String => println("Got a string")
  case num : Int if num < 100 => println("Number less than 100")
  case Paired(x,y) => println("Got x and y: " + x + ", " + y)
  case unknown => println("??: " + unknown)
}

前两个 case 语句显示基于类型的模式匹配。第三个展示了如何使用提取器将数据分解为组成部分并将这些部分分配给变量。第三个显示了一个可变模式匹配,它将匹配任何内容。未显示的是 _ 情况:

case _ => println("what")

它与变量模式匹配一​​样,匹配任何内容,但不将匹配的对象绑定到变​​量。

顶部的案例类是用于创建提取器以及类本身的 Scala 简写。

case class Paired(x: Int, y: Int)

def foo(x: Any) = x match {
  case string : String => println("Got a string")
  case num : Int if num < 100 => println("Number less than 100")
  case Paired(x,y) => println("Got x and y: " + x + ", " + y)
  case unknown => println("??: " + unknown)
}

The first two case statements show type based pattern matching. The third shows the use of an Extractor to break data down into constituent parts and to assign those parts to variables. The third shows a variable pattern match which will match anything. Not shown is the _ case:

case _ => println("what")

Which like the variable pattern match, matches anything, but does not bind the matched object to a variable.

The case class at the top is Scala shorthand for creating an extractor as well as the class itself.

待天淡蓝洁白时 2024-10-08 01:55:34

当然,matchif 的作用都与 cond 完全相同。一种可能性是这样做:

object Cond {
  def apply(clauses: Iterable[(()=>Boolean, ()=>Any)]): Any = {
    clauses find (_._1()) map (_._2()) getOrElse ()
  }
}

这个对象接受一些 Iterable 的东西,其中每个项目都是一对返回 Boolean 的函数和一个返回 Any 的函数。它尝试查找第一个函数返回 true 的项目,如果找到则停止查找,对找到的项目调用第二个函数并返回该函数的结果(如果没有找到,则返回 ())。

示例:

val clauses = Seq(
  ({()=>false}, {()=>println("foo")}),
  ({()=>true}, {()=>println("bar")})
)
Cond(clauses)

def checkYear(year: Int) = {
  Cond(Seq(
    ({()=>year % 400 == 0}, {()=>42}),
    ({()=>true}, {()=>{c:Char => (c.toString * 3)}})
  ))
}

ETA:是的,我知道它很难看,但它有效。

Of course, neither match nor if does exactly the same thing as cond. One possibility is to do like this:

object Cond {
  def apply(clauses: Iterable[(()=>Boolean, ()=>Any)]): Any = {
    clauses find (_._1()) map (_._2()) getOrElse ()
  }
}

This object accepts something Iterable where each item is a pair of a function returning Boolean and a function returning Any. It tries to find an item whose first function returns true, stops looking if it finds one, calls the second function on a found item and returns the result of that function (or () if none was found).

Examples:

val clauses = Seq(
  ({()=>false}, {()=>println("foo")}),
  ({()=>true}, {()=>println("bar")})
)
Cond(clauses)

def checkYear(year: Int) = {
  Cond(Seq(
    ({()=>year % 400 == 0}, {()=>42}),
    ({()=>true}, {()=>{c:Char => (c.toString * 3)}})
  ))
}

ETA: Yes, I know it is ugly, but it works.

夜夜流光相皎洁 2024-10-08 01:55:34

最直接的翻译是使用模式保护,尽管它需要一些样板。模式保护仅在 case 模式中工作,而 case 仅在 match 中工作(除非我们正在编写 PartialFunction代码>)。

我们可以通过将单位值与简单的case进行匹配来满足这些条件:

;; Scheme
(cond
  (foo bar)
  (baz quux)
  (t   mydefault))

// Scala
() match {
  case _ if foo => bar
  case _ if baz => quux
  case _        => mydefault
}

The most straightforward translation is to use pattern guards, although it requires some boilerplate. Pattern guards only work in a case pattern, and case only works in a match (unless we're writing a PartialFunction).

We can satisfy these conditions by matching a unit value against trivial cases:

;; Scheme
(cond
  (foo bar)
  (baz quux)
  (t   mydefault))

// Scala
() match {
  case _ if foo => bar
  case _ if baz => quux
  case _        => mydefault
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文