Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding an item that matches predicate in Scala

I'm trying to search a scala collection for an item in a list that matches some predicate. I don't necessarily need the return value, just testing if the list contains it.

In Java, I might do something like:

for ( Object item : collection ) {     if ( condition1(item) && condition2(item) ) {        return true;     } } return false; 

In Groovy, I can do something like:

return collection.find { condition1(it) && condition2(it) } != null 

What's the idiomatic way to do this in Scala? I could of course convert the Java loop style to Scala, but I feel like there's a more functional way to do this.

like image 285
Jeff Storey Avatar asked Mar 04 '12 16:03

Jeff Storey


People also ask

What does find return Scala?

Scala Map find() method with example The find() method is utilized to find the first element of the map that satisfies the given predicate. Return Type: It returns the first element of the map which satisfies the given predicate.

What does :+ mean in Scala?

On Scala Collections there is usually :+ and +: . Both add an element to the collection. :+ appends +: prepends. A good reminder is, : is where the Collection goes. There is as well colA ++: colB to concat collections, where the : side collection determines the resulting type.

How do you check if a value is in a list Scala?

contains() function in Scala is used to check if a list contains the specific element sent as a parameter. list. contains() returns true if the list contains that element. Otherwise, it returns false .

What does exists do in Scala?

Scala List exists() method with example. The exists() method is utilized to check if the given predicate satisfy the elements of the list or not. Return Type: It returns true if the stated predicate holds true for some elements of the list else it returns false.


2 Answers

Use filter:

scala> val collection = List(1,2,3,4,5) collection: List[Int] = List(1, 2, 3, 4, 5)  // take only that values that both are even and greater than 3  scala> collection.filter(x => (x % 2 == 0) && (x > 3)) res1: List[Int] = List(4)  // you can return this in order to check that there such values scala> res1.isEmpty res2: Boolean = false  // now query for elements that definitely not in collection scala> collection.filter(x => (x % 2 == 0) && (x > 5)) res3: List[Int] = List()  scala> res3.isEmpty res4: Boolean = true 

But if all you need is to check use exists:

scala> collection.exists( x => x % 2 == 0 ) res6: Boolean = true 
like image 156
om-nom-nom Avatar answered Oct 02 '22 00:10

om-nom-nom


Testing if value matching predicate exists

If you're just interested in testing if a value exists, you can do it with.... exists

scala> val l=(1 to 4) toList l: List[Int] = List(1, 2, 3, 4)  scala> l exists (_>5) res1: Boolean = false  scala> l exists (_<2) res2: Boolean = true  scala> l exists (a => a<2 || a>5) res3: Boolean = true 

Other methods (some based on comments):

Counting matching elements

Count elements that satisfy predicate (and check if count > 0)

scala> (l count (_ < 3)) > 0 res4: Boolean = true 

Returning first matching element

Find the first element that satisfies predicate (as suggested by Tomer Gabel and Luigi Plinge this should be more efficient because it returns as soon as it finds one element that satisfies the predicate, rather than traversing the whole List anyway)

scala> l find (_ < 3) res5: Option[Int] = Some(1)   // also see if we found some element by // checking if the returned Option has a value in it scala> l.find(_ < 3) isDefined res6: Boolean = true 

Testing if exact value exists

For the simple case where we're actually only checking if one specific element is in the list

scala> l contains 2 res7: Boolean = true 
like image 42
Paolo Falabella Avatar answered Oct 02 '22 01:10

Paolo Falabella