scottiedog45
07/14/2019, 1:52 AM.count
lambda inside this function; it looks to me like a lambda is being passed to an Int
? What?
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
Al Warren
07/14/2019, 5:18 AMinline 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:
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
.scottiedog45
07/14/2019, 3:29 PM