Scala 中可以匹配范围吗?

发布于 2024-08-03 00:23:33 字数 255 浏览 2 评论 0原文

Scala 中是否可以匹配一系列值?

例如:

val t = 5
val m = t match {
    0 until 10 => true
    _ => false
}

如果 t 介于 0 和 10 之间,则 m 将为 true,否则为 false。当然,这一点是行不通的,但是有什么办法可以实现这样的目标吗?

Is it possible to match a range of values in Scala?

For example:

val t = 5
val m = t match {
    0 until 10 => true
    _ => false
}

m would be true if t was between 0 and 10, but false otherwise. This little bit doesn't work of course, but is there any way to achieve something like it?

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

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

发布评论

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

评论(5

浸婚纱 2024-08-10 00:23:34

使用 Range 进行防护:

val m = t match {
  case x if 0 until 10 contains x => true
  case _ => false
}

Guard using Range:

val m = t match {
  case x if 0 until 10 contains x => true
  case _ => false
}
忆伤 2024-08-10 00:23:34

您可以使用守卫:

val m = t match {
    case x if (0 <= x && x < 10) => true
    case _ => false
}

You can use guards:

val m = t match {
    case x if (0 <= x && x < 10) => true
    case _ => false
}
梦罢 2024-08-10 00:23:34

通过这些定义:

  trait Inspector[-C, -T] {
    def contains(collection: C, value: T): Boolean
  }

  implicit def seqInspector[T, C <: SeqLike[Any, _]] = new Inspector[C, T]{
    override def contains(collection: C, value: T): Boolean = collection.contains(value)
  }

  implicit def setInspector[T, C <: Set[T]] = new Inspector[C, T] {
    override def contains(collection: C, value: T): Boolean = collection.contains(value)
  }

  implicit class MemberOps[T](t: T) {
    def in[C](coll: C)(implicit inspector: Inspector[C, T]) =
      inspector.contains(coll, t)
  }

您可以执行如下检查:

2 in List(1, 2, 4)      // true
2 in List("foo", 2)     // true
2 in Set("foo", 2)      // true
2 in Set(1, 3)          // false
2 in Set("foo", "foo")  // does not compile
2 in List("foo", "foo") // false (contains on a list is not the same as contains on a set)
2 in (0 to 10)          // true

因此您需要的代码是:

val m = x in (0 to 10)

With these definitions:

  trait Inspector[-C, -T] {
    def contains(collection: C, value: T): Boolean
  }

  implicit def seqInspector[T, C <: SeqLike[Any, _]] = new Inspector[C, T]{
    override def contains(collection: C, value: T): Boolean = collection.contains(value)
  }

  implicit def setInspector[T, C <: Set[T]] = new Inspector[C, T] {
    override def contains(collection: C, value: T): Boolean = collection.contains(value)
  }

  implicit class MemberOps[T](t: T) {
    def in[C](coll: C)(implicit inspector: Inspector[C, T]) =
      inspector.contains(coll, t)
  }

You can do checks like these:

2 in List(1, 2, 4)      // true
2 in List("foo", 2)     // true
2 in Set("foo", 2)      // true
2 in Set(1, 3)          // false
2 in Set("foo", "foo")  // does not compile
2 in List("foo", "foo") // false (contains on a list is not the same as contains on a set)
2 in (0 to 10)          // true

So the code you need would be:

val m = x in (0 to 10)
你是暖光i 2024-08-10 00:23:34

这是使用范围进行匹配的另一种方法:

val m = t match {
  case x if ((0 to 10).contains(x)) => true
  case _ => false
}

Here's another way to match using a range:

val m = t match {
  case x if ((0 to 10).contains(x)) => true
  case _ => false
}
梦幻的心爱 2024-08-10 00:23:34

另一种选择是使用隐式将其实际添加到语言中,我为 int 和 Range 添加了两种变体

object ComparisonExt {
  implicit class IntComparisonOps(private val x : Int) extends AnyVal {
    def between(range: Range) = x >= range.head && x < range.last
    def between(from: Int, to: Int) = x >= from && x < to
  }

}

object CallSite {
  import ComparisonExt._

  val t = 5
  if (t between(0 until 10)) println("matched")
  if (!(20 between(0 until 10))) println("not matched")
  if (t between(0, 10)) println("matched")
  if (!(20 between(0, 10))) println("not matched")
}

Another option would be to actually add this to the language using implicits, i added two variations for int and Range

object ComparisonExt {
  implicit class IntComparisonOps(private val x : Int) extends AnyVal {
    def between(range: Range) = x >= range.head && x < range.last
    def between(from: Int, to: Int) = x >= from && x < to
  }

}

object CallSite {
  import ComparisonExt._

  val t = 5
  if (t between(0 until 10)) println("matched")
  if (!(20 between(0 until 10))) println("not matched")
  if (t between(0, 10)) println("matched")
  if (!(20 between(0, 10))) println("not matched")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文