scoop:scoop.std:0.1.0
src/collections/traversal.scoop
Selected production source · src/collections/traversal.scoop
1
package scoop.collections3
internal fun nextCollectionCount(value: Long): Long {4
if (value == 9223372036854775807L) throw ArithmeticException()5
return value + 1L6
}8
/**9
* Runs the action for each element in iteration order.10
*/11
public fun <T> Iterable<T>.forEach(action: (T) -> Unit) {12
for (element in this) action(element)13
}15
/**16
* Runs the action with each element and its zero-based Long index.17
*/18
public fun <T> Iterable<T>.forEachIndexed(action: (Long, T) -> Unit) {19
var index = 0L20
for (element in this) {21
action(index, element)22
index = nextCollectionCount(index)23
}24
}26
/**27
* Runs the action eagerly for all elements and returns the original iterable.28
*/29
public fun <T> Iterable<T>.onEach(action: (T) -> Unit): Iterable<T> {30
forEach<T>(action)31
return this32
}34
/**35
* Accumulates left to right from the supplied initial value, which is returned for empty input.36
*/37
public fun <T, R> Iterable<T>.fold(initial: R, operation: (R, T) -> R): R {38
var result = initial39
for (element in this) result = operation(result, element)40
return result41
}43
/**44
* Accumulates left to right from the first element, or returns None for empty input.45
*/46
public fun <T> Iterable<T>.reduceOrNone(operation: (T, T) -> T): T? {47
var result: T? = None48
for (element in this) {49
result = when (result) {50
case None -> Some(element)51
case Some(val previous) -> Some(operation(previous, element))52
}53
}54
return result55
}57
/**58
* Accumulates left to right from the first element; empty input throws NoSuchElementException.59
*/60
public fun <T> Iterable<T>.reduce(operation: (T, T) -> T): T =61
requireCollectionElement<T>(reduceOrNone<T>(operation))63
/**64
* Counts eligible elements, applying the predicate when supplied.65
*/66
public fun <T> Iterable<T>.count(predicate: (T) -> Boolean): Long {67
var result = 0L68
for (element in this) { if (predicate(element)) result = nextCollectionCount(result) }69
return result70
}72
/**73
* Counts eligible elements, applying the predicate when supplied.74
*/75
public fun <T> Iterable<T>.count(): Long = count<T> { _ -> true }77
/**78
* Returns whether any eligible element exists, applying the predicate when supplied and stopping79
* at the first match.80
*/81
public fun <T> Iterable<T>.any(predicate: (T) -> Boolean): Boolean {82
for (element in this) { if (predicate(element)) return true }83
return false84
}86
/**87
* Returns whether any eligible element exists, applying the predicate when supplied and stopping88
* at the first match.89
*/90
public fun <T> Iterable<T>.any(): Boolean = when (iterator().next()) {91
case None -> false92
case Some(_) -> true93
}95
/**96
* Returns whether every element satisfies the predicate; true for empty input.97
*/98
public fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean {99
for (element in this) { if (!predicate(element)) return false }100
return true101
}103
/**104
* Returns whether no eligible element exists, applying the predicate when supplied and stopping at105
* the first match.106
*/107
public fun <T> Iterable<T>.none(): Boolean = !any<T>()108
/**109
* Returns whether no eligible element exists, applying the predicate when supplied and stopping at110
* the first match.111
*/112
public fun <T> Iterable<T>.none(predicate: (T) -> Boolean): Boolean = !any<T>(predicate)