title: Kotlin: how to pass an object function as parameter to another?
body: <p>I am trying to learn functional Kotlin and have written this test code:</p>
  
  <pre><code>import java.util.*
  
  data class BorrowerX(val name: String, val maxBooks: Int) {
  
      companion object {
  
          fun getName(br: BorrowerX): String = br.name
  
          fun findBorrowerX(n: String, brs: ArrayList&lt;BorrowerX&gt;): BorrowerX? {
  
              val coll: List&lt;BorrowerX&gt; = brs.filter { BorrowerX.getName(it) == n }
  
              if (coll.isEmpty()) {
                  return null
              } else return coll.first()
  
          }
  
          fun findBorrowerX2(n: String, brs: ArrayList&lt;BorrowerX&gt;, f: (BorrowerX) -&gt; String): BorrowerX? {
  
              val coll: List&lt;BorrowerX&gt; = brs.filter { f(it) == n }
  
              if (coll.isEmpty()) {
                  return null
              } else return coll.first()
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>In the REPL I can successfully call "findBorrowerX":</p>
  
  <pre><code>import BorrowerX
  
  val br1 = BorrowerX(name = "Borrower1", maxBooks = 1)
  val br2 = BorrowerX(name = "Borrower2", maxBooks = 2)
  val br3 = BorrowerX(name = "Borrower3", maxBooks = 3)
  
  val brs1 = arrayListOf(br1, br2, br3)
  
  BorrowerX.findBorrowerX("Borrower1", brs1)
  BorrowerX(name=Borrower1, maxBooks=1)
  
  BorrowerX.findBorrowerX("Borrower-Bad", brs1)
  null
  </code></pre>
  
  <p>But how do I make the call to "findBorrowerX2":</p>
  
  <pre><code>BorrowerX.findBorrowerX2("Borrower1", brs1, BorrowerX.getName(???))
  </code></pre>
  
  <p>And pass the iterated BorrowerX to getName??</p>
  
  <p>This looks related, but I'm not sure:</p>
  
  <p><a href="https://stackoverflow.com/questions/16120697/kotlin-how-to-pass-a-function-as-parameter-to-another">Kotlin: how to pass a function as parameter to another?</a></p>
  
  <p>Thank you in advance for your help with this!</p>
  
  <p>EDIT:</p>
  
  <p>Here is the equivalent Scala code for what I want to do:</p>
  
  <pre><code>def findBorrowerX2(n: String, brs: List[BorrowerX], f: BorrowerX =&gt; String): BorrowerX = {
  
    val coll: List[BorrowerX] = brs.filter(f(_) == n)
  
    if (coll.isEmpty) {
      null
    } else {
      coll.head
    }
  
  }
  
  scala&gt; BorrowerX.findBorrowerX2("Borrower3", brs1, BorrowerX.getName(_))
  res1: BorrowerX = BorrowerX(Borrower3,3)
  
  scala&gt; BorrowerX.findBorrowerX2("Borrower33", brs1, BorrowerX.getName(_))
  res2: BorrowerX = null
  </code></pre>
  
  <p>Perhaps this is not possible in Kotlin?</p>