【Scala】泛函数

来源:互联网 发布:软件外包的上市公司 编辑:程序博客网 时间:2024/06/03 12:12
def findFirstA[A](arr: Array[A],target:A)(equ: (A, A) => Boolean): Int = {
        def loop(idx: Int): Int = idx match {
          case l if (l >= arr.length) => -1    //indicate not found
          case i if (equ(arr(i),target)) => idx
          case _ => loop(idx + 1)
        }
        loop(0)

}                                         


findFirstA[Int](Array(2,4,3,9,0),3)((x,y) => x == y)              //> res57: Int = 2
findFirstA[String](Array("Hello","My","World"),"My")((x,y) => x == y)        //> res58: Int = 1