Exists checks if a predicate holds for at least one element in the Traversable collection. For example:
def exists(p: ((A, B)) ⇒ Boolean): Boolean
package com.chapter4.CollectionAPI
object UsingFatArrow {
def fliesPerSecond(callback: () => Unit) {
while (true) { callback(); Thread sleep 1000 }
}
def main(args: Array[String]): Unit= {
fliesPerSecond(() => println("Time and tide wait for none but fly like arrows ..."))
}
}
You will get the following output:
Time and tide wait for none but flies like an arrow...
Time and tide wait for none but flies like an arrow...
Time and tide wait for none but flies like an arrow...
Time and tide wait for none but flies like an arrow...
Time and tide wait for none but flies like an arrow...
Time and tide wait for none but flies like an arrow...
A detailed example can be seen in the following code as follows:
package com.chapter4.CollectionAPI
object ExistsExample {
def main(args: Array[String]) {
// Given a list of cities and now check if "Dublin" is included in
the list
val cityList = List("Dublin", "NY", "Cairo")
val ifExisitsinList = cityList exists (x => x == "Dublin")
println(ifExisitsinList)
// Given a map of countries and their capitals check if Dublin is
included in the Map
val cityMap = Map("Ireland" -> "Dublin", "UK" -> "London")
val ifExistsinMap = cityMap exists (x => x._2 == "Dublin")
println(ifExistsinMap)
}
}
You will get the following output:
true
true
In the earlier example and in a subsequent section, we used the Scala infix notation. Suppose you would like to perform some operation with complex numbers and have a case class with an add method for adding two complex numbers:
case class Complex(i: Double, j: Double) {
def plus(other: Complex): Complex = Complex(i + other.i, j + other.j)
}
Now in order to access the properties of this class, you need to create an object like this:
val obj = Complex(10, 20)
Moreover, suppose you have the following two complex numbers defined:
val a = Complex(6, 9)
val b = Complex(3, -6)
Now to access the plus() method from the case class, you will do something like this:
val z = obj.plus(a)
This should give you output: Complex(16.0,29.0). However, isn't it good if you just call the method like this:
val c = a plus b
And it really works like a charm. Here is the complete example:
package com.chapter4.CollectionAPI
object UsingInfix {
case class Complex(i: Double, j: Double) {
def plus(other: Complex): Complex = Complex(i + other.i, j + other.j)
}
def main(args: Array[String]): Unit = {
val obj = Complex(10, 20)
val a = Complex(6, 9)
val b = Complex(3, -6)
val c = a plus b
val z = obj.plus(a)
println(c)
println(z)
}
}
The precedence of an infix operator: This is determined by the operator's first character. Characters are listed below in increasing order of precedence, with characters on the same line having the same precedence:
(all letters)
|
^
&
= !
< >
:
+ -
* / %
(all other special characters)
General warning: Using the infix notation for calling regular, non-symbolic methods is discouraged and should be used only if it significantly increases readability. One example of a sufficiently motivated use of infix notation is matchers and other parts of the tests definition in ScalaTest.
Another interesting element in the Scala collection package is using forall. It is used to check if a predicate holds for each element in a Traversable collection. In the next subsection, we will see an example of it.