Question

Est-il possible de faire correspondre une plage de valeurs à Scala?

Par exemple:

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

m serait true si t était entre 0 et 10, mais sinon false. Ce peu ne fonctionne pas bien sûr, mais est-il possible d'obtenir quelque chose comme ça?

Était-ce utile?

La solution

Garde à l'aide Range:

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

Autres conseils

Vous pouvez utiliser des gardes:

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

Voici une autre façon de correspondre à l'aide d'une gamme:

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

Avec ces définitions:

  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)
  }

Vous pouvez faire des tests comme ceux-ci:

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

Ainsi, le code dont vous avez besoin serait:

val m = x in (0 to 10)

Une autre option serait d'ajouter en fait cela à la langue à l'aide implicits, j'ai ajouté deux variations pour int et 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")
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top