Anthony Styzhin
11/24/2019, 5:33 PMfun findShort(s: String): Int = s.split(" ").minBy { it.length }!!.count()
vs
fun findShort(s: String): Int = s.split(" ").minBy { it.length }!!.length
Both options work, pass unit tests and both are presented among solutions at codewars. Just trying to satisfy my curiosity ^^
Thanks in advance!karelpeeters
11/24/2019, 6:16 PMcount
and length
are on String
here, so everything else is not relevant. Then count
is defined here: https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/common/src/generated/_Strings.kt#L946-L952
So they're exactly the same.Burkhard
11/24/2019, 6:18 PMAnthony Styzhin
11/24/2019, 6:20 PMkarelpeeters
11/24/2019, 6:20 PMBurkhard
11/24/2019, 6:23 PMcount
function is just there to be more similar to Collection
(might actually be Iterable
). It also has count
and length
, but in this case I think length
calls count
and not the other way round.karelpeeters
11/24/2019, 6:25 PMcount
returns size
which is part of the Collection
interface. Iterable
also has count
but that is O(n)
.Burkhard
11/24/2019, 6:32 PMlength
and count
are the same for all classes/interfaces in the standard library (unless someone can point me to an exception).John
11/25/2019, 11:37 AM@kotlin.internal.InlineOnly
public inline fun CharSequence.count(): Int {
return length
}
karelpeeters
11/25/2019, 11:37 AM