Scoop API

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 */
4public 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 */
14public interface Iterable<T> {
15 /**
16 * Creates the cursor used to traverse this value.
17 */
18 public operator fun iterator(): Iterator<T>
21internal class ListIterator<T> internal constructor(
22 private val source: List<T>,
23 private var index: Long,
24 private var exhausted: Boolean
25) : Iterator<T> {
26 public override fun next(): Option<T> {
27 if (exhausted) { return None }
28 if (index >= source.size) {
29 exhausted = true
30 return None
31 }
32 val value = source[index]
33 index = index + 1L
34 return Some(value)
35 }