Scala exists example

The “exists” method checks to see if a value is in a list of values:

Range(1, 10).
  exists(
    x => x.equals(2)
  )

If you make the list into a view, “exists” will only load items from the view until it finds a value, as seen here:

Range(1, 10).
  view.
  map(x => 
   if (x < 5) x else ???
  ).
  exists(
    x => x.equals(2)
  )

You can also simplify this and check for values like “None”:

List(1, 2, None).exists(_.equals(None))

res155: Boolean = true

Leave a Reply

Your email address will not be published. Required fields are marked *