Ben Butterworth
11/23/2020, 5:17 PMmyMapObject.asSequence().map {
yield Pair(it.key, processValue(it.value))
}
but the iterator method override fun next(): Pair<String, OutputType> {}
doesn't return sequences, but the actual value. My understanding of yield is that it needs to be inside a coroutine, so yield isn't really what im looking for? (im taking cues from the python yield keyword, which is very different)Vampire
11/23/2020, 5:33 PMyield
is not necessary here, it would allow you to produce values for an arbitrary sequence.
It is a suspending function, so it has to be called from a suspending function: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/-sequence-scope/yield.html
If you for example use the sequence
function, the lambda you give it is a suspending function, so you can call yield
in it: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/sequence.html
But as you just want to map the elements of an existing sequence, yield
is not what you are after, just omit it and you have what you want.Ben Butterworth
11/23/2020, 5:36 PMoverride fun next(): Pair<String, ObjectType> {}
it cant return a sequence, which is why it wont work. I want it to be an iterator so clients can call each item independently. Unless this is also possible with sequences?Vampire
11/23/2020, 5:39 PMBen Butterworth
11/23/2020, 5:42 PMBen Butterworth
11/23/2020, 5:45 PMVampire
11/23/2020, 5:49 PMmyMapObject.asSequence().map { Pair(it.key, processValue(it.value)) }.iterator()
Vampire
11/23/2020, 5:50 PMBen Butterworth
11/23/2020, 5:51 PM