tbh hate forloop of kotlin is there anyway can m...
# announcements
c
tbh hate forloop of kotlin is there anyway can mate with index via forloop instead of choosing while.
2
🤦‍♂️ 1
d
use case?
c
traversing node
d
any details?
can't even imagine where and what are you going to do with the index.
c
to traverse the childnodes recursively
breadth first search
d
why do you need index for BFS?
c
to traverse nodes , if for some odd reason iterator was not implemented on them.
d
still can't see why you need index. Probably if you have sample code will be better.
just give information needed for people to be able to help you
☝️ 2
k
Yeah, we are gonna need a bit more information. Maybe a code sample, cause I don't really see what you mean either.
n
Use the
withIndex()
method
c
traversing nodes https://docs.oracle.com/javase/10/docs/api/org/w3c/dom/Node.html it doesn't have iterator implemented over it.
n
Copy code
val l = listOf("a","b","c")
fun main() {
    for ((i,s) in l.withIndex()) {
		println("${i} -> ${s}")
    }
}
So how are you iterating over it then?
c
via while loop
n
use a for loop and you’ll have the index
k
You need a queue for BFS, so what's wrong with a while loop?
n
Or use a range over the indices. E.g.
(0 until node.size).forEach { index -> … }
(however you get the indices)
How are you getting the child nodes? By index? If so… I’m not sure what your question is…
c
@Kroppeb so used to
for loop
for that [ in java] :/
k
Could you paste how such code would look?
c
sure
n
One way is:
Copy code
for (i in (0 until node.childNodes.length)) {
    val child = node.childNodes.item(i)
    ... do something with child
}
👍 1
Another way is:
Copy code
generateSequence(node.firstChild,{it.nextSibling})
  .forEach { child -> 
     ... do something with child 
   }
c
kotlin has
too many
higher order functions.
👎🏻 13
👍 1
d
well, that what makes Kotlin powerful imo... it might be a bit overwhelming at first, but once you get used to it, it's hard not to live with it... now, i find it hard to code using old java style already... it's part of the learning process i suppose...
n
“kotlin has
too 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.
💯 3
e
Adding on to @natpryce’s solution, you don’t need to write
0 until node.childNodes.length
, you can just use
for (i in node.childNodes.indices)
instead 🙂 it achieves the same thing
👌 1
k
actually, I don't think you can, as
childNodes
is not a list.
e
Ah, I thought it may have worked because
indices
is implemented on
Collection<*>
in the stdlib. You can convert it to a list via
.asList()
, though it may be unnecessary
k
childNodes
is a
NodeList
which only has a
length
and
item(Int)
e
Yeah. I was just pointing out that you can convert it to a list via
asList()
: https://kotlinlang.org/api/latest/jvm/stdlib/org.w3c.dom/as-list.html
k
But that's the JS
NodeList
We are talking about the JVM one, which does not seem to extend the
ArrayLike
interface