scoop:scoop.core:0.1.0
src/iteration.scoop
Selected production source · src/iteration.scoop
1
/**2
* A one-way cursor whose [next] operation returns Some(value) or None at the end.3
*/4
public interface Iterator<T> {5
/**6
* Returns the next value or None when iteration is exhausted.7
*/8
public fun next(): Option<T>9
}11
/**12
* A value that supplies an [Iterator] for the for-loop protocol.13
*/14
public interface Iterable<T> {15
/**16
* Creates the cursor used to traverse this value.17
*/18
public operator fun iterator(): Iterator<T>19
}21
internal class ListIterator<T> internal constructor(22
private val source: List<T>,23
private var index: Long,24
private var exhausted: Boolean25
) : Iterator<T> {26
public override fun next(): Option<T> {27
if (exhausted) { return None }28
if (index >= source.size) {29
exhausted = true30
return None31
}32
val value = source[index]33
index = index + 1L34
return Some(value)35
}36
}