Chills
02/29/2020, 1:44 PMdeactivateduser
02/29/2020, 1:45 PMChills
02/29/2020, 1:46 PMdeactivateduser
02/29/2020, 1:46 PMdeactivateduser
02/29/2020, 1:46 PMChills
02/29/2020, 1:47 PMChills
02/29/2020, 1:48 PMdeactivateduser
02/29/2020, 1:48 PMChills
02/29/2020, 1:49 PMdeactivateduser
02/29/2020, 1:50 PMdeactivateduser
02/29/2020, 1:51 PMKroppeb
02/29/2020, 2:35 PMnatpryce
02/29/2020, 2:39 PMwithIndex() methodChills
02/29/2020, 2:41 PMnatpryce
02/29/2020, 2:42 PMval l = listOf("a","b","c")
fun main() {
for ((i,s) in l.withIndex()) {
println("${i} -> ${s}")
}
}natpryce
02/29/2020, 2:43 PMChills
02/29/2020, 2:43 PMnatpryce
02/29/2020, 2:43 PMKroppeb
02/29/2020, 2:44 PMnatpryce
02/29/2020, 2:44 PM(0 until node.size).forEach { index -> … }natpryce
02/29/2020, 2:44 PMnatpryce
02/29/2020, 2:45 PMChills
02/29/2020, 2:45 PMfor loop for that [ in java] :/Kroppeb
02/29/2020, 2:46 PMChills
02/29/2020, 2:47 PMnatpryce
02/29/2020, 2:50 PMfor (i in (0 until node.childNodes.length)) {
val child = node.childNodes.item(i)
... do something with child
}natpryce
02/29/2020, 2:52 PMgenerateSequence(node.firstChild,{it.nextSibling})
.forEach { child ->
... do something with child
}Chills
02/29/2020, 2:54 PMtoo many higher order functions.deactivateduser
02/29/2020, 2:58 PMnatpryce
02/29/2020, 10:01 PMtoo many higher order functions”
I gave two examples above because the w3c DOM API has multiple ways to iterate through the children of a DOM Node. Do you want to index by position in the parent (first example)? Or follow sibling links (second example)? The complexity is from the DOM API, not Kotlin’s stdlib.Evan R.
03/02/2020, 1:30 PM0 until node.childNodes.length, you can just use for (i in node.childNodes.indices) instead 🙂 it achieves the same thingKroppeb
03/02/2020, 1:31 PMchildNodes is not a list.Evan R.
03/02/2020, 1:40 PMindices is implemented on Collection<*> in the stdlib. You can convert it to a list via .asList() , though it may be unnecessaryKroppeb
03/02/2020, 1:41 PMchildNodes is a NodeList which only has a length and item(Int)Evan R.
03/02/2020, 2:39 PMasList() : https://kotlinlang.org/api/latest/jvm/stdlib/org.w3c.dom/as-list.htmlKroppeb
03/02/2020, 2:41 PMNodeList We are talking about the JVM one, which does not seem to extend the ArrayLike interface