Scala:从符合条件的列表中返回一个元素

Scala: Returning an element from a list that matches a condition

我有一个列表,我正在尝试编写一个函数 returnMatchedElement(x:Int,y:Int,f:(Int,Int)=>Boolean),如果某个条件与列表的某个元素匹配,它将 return 该元素。到目前为止,这是我得到的:

def returnMatchedElement(x:Int,l:List[Int],f:(Int,Int)=>Boolean):Int={
 for (y<-l if f(x,y)) yield y 
0}
def matchElements(a:Int,b:Int):Boolean= {if a==b true else false} 
val l1=List(1,2,3,4,5)

returnMatchedElement(3,l1,matchElements)
res13: Int = 0 

我猜我在理解 yield 关键字时遇到了问题。我在这里弄错了什么?

编辑

下面的答案有效(感谢),但前提是 f returns 布尔值。我尝试了另一个这样的例子

def matchElements(a:Int,b:Int):Int= {if (a==b) 1 else 0} 
def returnMatchedElement(x:Int,l:List[Int],f:(Int,Int)=>Int):Option[Int]={l.find(y => f(x, y))}

现在编译器说

<console>:8: error: type mismatch;
 found   : Int
 required: Boolean
       def returnMatchedElement(x:Int,l:List[Int],f:(Int,Int)=>Int):Option[Int]={l.find(y => f(x, y))}

简单地使用find,它找到满足谓词的序列的第一个元素,如果有:

def returnMatchedElement(x: Int, l: List[Int], f: (Int,Int) => Boolean): Option[Int] = {
  l.find(y => f(x, y))
}