What exactly is happening with the `.count` lambda...
# getting-started
s
What exactly is happening with the
.count
lambda inside this function; it looks to me like a lambda is being passed to an
Int
? What?
Copy code
fun bingo(ticket: Array<Pair<String, Int>>, win: Int) =
    if (ticket.count { it.second.toChar() in it.first } >= win) "Winner!" else "Loser!"
I’m on https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/count.html and don’t see anything like a closure happening for
Array.count
a
Given the definition
Copy code
inline fun <T> Array<out T>.count(
    predicate: (T) -> Boolean
): Int
Note that
predicate: (T) -> Boolean
is a lambda. The example leaves out the parenthesis and could be rewritten like this:
Copy code
if (
    ticket.count ( 
        { it.second.toChar() in it.first } 
    ) >= win
) "Winner!"
else "Loser!"
In other words, count the number of results produced by the predicate
it.second.toChar() in it.first
.
👍 1
s
Thanks @Al Warren!