Hey Guys, I was trying to solve a bug in a application here and I noticed something that seems weird to me.
Basically I had this code in my application:
fun DownloadManager.isAudiobookCompleted(isbn: String): Boolean {
return currentDownloads
.filter { it.audiobookIsbn == isbn }
.all { it.state == Download.STATE_COMPLETED }
}
Very intuitive where I want to return true, but was buggy. So I went to see why and just realized how the .all works:
/**
* Returns `true` if all elements match the given [predicate].
*
* @sample samples.collections.Collections.Aggregates.all
*/
public inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean {
if (this is Collection && isEmpty()) return true
for (element in this) if (!predicate(element)) return false
return true
}
Basically when the list isEmpty, it returns true. Is't it wrong?
I mean, for me it should return true only match the predicate, and if the list is empty for sure don't match the predicate...