https://kotlinlang.org logo
Title
s

Smallville7123

04/15/2019, 1:27 PM
for iterator().hasNext() does the current element count as a true condition
🇳🇴 3
r

ribesg

04/15/2019, 1:28 PM
Read the doc
hasNext()
returning
true
means that
next()
will work
s

Smallville7123

04/15/2019, 1:29 PM
eg if there is only a single element
as
s.forEach { println("it = $it") }
does not print the last element if there is more than 1 element, and does not print anything at all if there is only 1 element
g

gildor

04/15/2019, 1:30 PM
What is s, what is forEach, where is an iterator?
s

Smallville7123

04/15/2019, 1:30 PM
fun main() {
    val word = "ab"
    println("word = " + word)
    val s = stringToStack(word)
    println("stringToStack = $s")
    val i = s.iterator()
    while (i.hasNext()) println("next = " + i.next())
    s.forEach { println("it = $it") }
}
r

ribesg

04/15/2019, 1:31 PM
What’s
stringToStack
g

gildor

04/15/2019, 1:31 PM
This code doesn't mean anything without context. Is it your own iterator implementation I suppose
If so, you just have a bug therr
Iterator is just an interface with convention how it should behave, not a magic, you of course can implement it with incorrect behavior, but all stdlib iterators do not have this problem
s

Smallville7123

04/15/2019, 1:32 PM
word = ab
stringToStack = [a, b]
next = a
it = a
g

gildor

04/15/2019, 1:33 PM
What is that? It's not even valid Kotlin syntax
s

Smallville7123

04/15/2019, 1:33 PM
fun stringToStack(str: String): Stack<String> {
    val deq = Stack<String>()
    var i = 0
    while (i < str.length) deq.addLast(str[i++].toString())
    return deq
}
g

gildor

04/15/2019, 1:35 PM
Do you understand that your comments without context just mean nothing for other people? so there is no chance to help you using it, only if one try to guess what does this code mean
g

gildor

04/15/2019, 1:36 PM
Because you asked recently about implementation details of iterator, I suppose you implement your own iterator and this iterator implementation is incorrect
r

ribesg

04/15/2019, 1:38 PM
What is this
Why don’t you just use
LinkedList
s

Smallville7123

04/15/2019, 1:39 PM
i dont know
so theres
main
,
stringToStack
,
Stack
and
LinkedList
r

ribesg

04/15/2019, 1:40 PM
What the hell are you doing
g

gildor

04/15/2019, 1:41 PM
LinkedList is Java class, as I remember Smallville tried to implement this for Native, but I'm not sure
So, is your question "why my custom iterator has a bug"? Did you even tried to debug it?
s

Smallville7123

04/15/2019, 1:43 PM
the documentation for it literally just states
Returns true if the iteration has more elements.
r

ribesg

04/15/2019, 1:46 PM
Yes. That is what the documentation states. So what’s your point?
s

Smallville7123

04/15/2019, 1:48 PM
how am i supposed to know that "Returns true if the iteration has more elements." actually means "`hasNext()` returning
true
means that
next()
will work"
r

ribesg

04/15/2019, 1:49 PM
s

Smallville7123

04/15/2019, 1:49 PM
which implies the
next
element, not the
current
element
r

ribesg

04/15/2019, 1:49 PM
What do you think
next
means
It’s supposed to return the next element, not the current one. I mean it’s written right there in the name
s

Smallville7123

04/15/2019, 1:50 PM
the next element after the current element
r

ribesg

04/15/2019, 1:50 PM
Yes, that’s what next is supposed to return
Your implementation of next returns the current value.
s

Smallville7123

04/15/2019, 1:53 PM
so it should be this?
/**
             * Returns the next element in the iteration.
             */
            override fun next(): T? {
                node = node?.next
                if (node != null) {
                    return node?.value
                }
                throw Exception("No Such Element")
            }
thus return...
word = ab
stringToStack = [a, b]
next = b
it = b
for
fun main() {
    val word = "ab"
    println("word = " + word)
    val s = stringToStack(word)
    println("stringToStack = $s")
    val i = s.iterator()
    while (i.hasNext()) println("next = " + i.next())
    s.forEach { println("it = $it") }
}
?
g

gildor

04/15/2019, 1:55 PM
which implies the
next
element, not the
current
element
There is no such thing as "current" element in iterator, there is only "next" or nothing
r

ribesg

04/15/2019, 1:56 PM
Yeah I said that your implementation of
next
was wrong I never said it was the only thing wrong
s

Smallville7123

04/15/2019, 1:58 PM
so when i.next() is first called, it will return the first element then the next element on subsequent calls?
instead of the second element then the next on subsequent calls?
g

gildor

04/15/2019, 1:59 PM
Yes, otherwise it would be impossible to get the first element of iterator
s

Smallville7123

04/15/2019, 1:59 PM
ok
so it is this?
override fun hasNext(): Boolean = node != null
            override fun next(): T? {
                val var0 = node?.value
                node = node?.next
                return var0
            }
do kotlin iterators throw an exception if next() is called when hasNext() returns false or does that only happen in JVM
for example,
if (tokenList.peek() == null) {
            try {
                tokenList.pop()
            } catch (e: java.util.NoSuchElementException) {
                return null
            }
        }
with
tokenList
originally being of type
ArrayDeque
r

ribesg

04/15/2019, 2:18 PM
Read the doc
s

Smallville7123

04/15/2019, 2:20 PM
Throws:
    NoSuchElementException - if this deque is empty