What is the difference between `isEmpty()` and `no...
# getting-started
a
What is the difference between
isEmpty()
and
none()
?
s
have you read the docs? they’re very different functions
assuming you’re talking about the methods defined on
Iterable
a
yes
Not the methods with the predicates
But
none()
(not
none { }
)
a
Looks like they're similar, but the code seems slightly nuanced in that
isEmpty()
is on a collection but
none()
is on an
Iterable
specifically. I'm guessing it's possible for something to be iterable and not a collection and that's where you can use it?
a
🤔 Interesting...
a
I'm guessing based on what I saw, I'm not claiming expertise here ha
I clicked into the source on
none()
a
And..?
It just return isEmpty() 😕
a
only if it's of type collection
Copy code
if (this is Collection) return isEmpty()
return !iterator().hasNext()
k
I don't really get why they added that method, they should have called it
isEmpty()
too.
Same for
find
and
firstOrNull
, just pick one.
☝️ 1
e
You could use
sequence.none()
but not
sequence.isEmpty()
. What is
sequence
? Check out https://medium.com/@elye.project/kotlin-slow-list-and-lazy-sequence-61691fc974c5
a
@karelpeeters I'd guess that they named it different because it behaves different.
Collection.isEmpty()
checks if there is anything inside the collection,
Iterator.none()
checks if any items are left to iterate over
k
No it's
Iterable.none
, you don't start with an existing iterator.
a
oh