General question for someone with greater wisdom. ...
# announcements
a
General question for someone with greater wisdom. This code (run with kotlin 1.5)
Copy code
1:    val aut = setOf(2, 3, 1, 4, 0)
2:    print("$aut")
3:    val aux2a: List<Int> = aut.dropWhile{ 2 < it }
4:    a2.containsAll(aux2a) shouldBe true
5:    (aux2a.size < a2.size) shouldBe true
fails (i.e.
(aux2a.size < a2.size)
is
false
) at line 5. the documentation for
dropWhile
says :
Copy code
Returns a list containing all elements except first elements that satisfy the given predicate.
I would have expected that at least item
3
(from line 1) xor item
4
would have been dropped. Perhaps both under certain circumstances (ordering is not a property of
Set
). What is the expected behavior of
dropWhile
for a Set?
r
already the first item
2
does not meed your predicate (
2 < 2
), so it stops dropping.
👍 2
a
So, since ordering is not a property of
Set
, dropWhile may drop none or any arbitrary number of items matching a predicate. Is that impression correct?
p
yes, it's non-deterministic
on a given JVM with a given collection size it'll probably give you the same results, so tests and such could inadvertently pass
a
It follows that
dropWhile
on a
Set
is a good operation to avoid, then.
1
p
Indeed.
dropWhile
is an extension on Iterable, so that it works on Set is more of a consequence of type limitations than any deliberate decisions I'd say
a
I hear you. I suspected as much, but it's nice to have a second opinion. Thanks.
e
Kotlin guarantees that `setOf()`/`mutableSetOf()`/`mapOf()`/`mutableMapOf()` will be iterated in insertion order
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/set-of.html "Elements of the set are iterated in the order they were specified." https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/mutable-set-of.html "The returned set preserves the element iteration order."
you don't know the iteration order of an arbitrary
Set
, but you may know it for particular instances
a
expediency is the mother of bugs 😬. The above clarification is gratefully appreciated.
👍 2