Scoop API

scoop:scoop.std:0.1.0

src/collections/traversal.scoop

Selected production source · src/collections/traversal.scoop

1package scoop.collections
3internal fun nextCollectionCount(value: Long): Long {
4 if (value == 9223372036854775807L) throw ArithmeticException()
5 return value + 1L
6}
8/**
9 * Runs the action for each element in iteration order.
10 */
11public fun <T> Iterable<T>.forEach(action: (T) -> Unit) {
12 for (element in this) action(element)
15/**
16 * Runs the action with each element and its zero-based Long index.
17 */
18public fun <T> Iterable<T>.forEachIndexed(action: (Long, T) -> Unit) {
19 var index = 0L
20 for (element in this) {
21 action(index, element)
22 index = nextCollectionCount(index)
23 }
26/**
27 * Runs the action eagerly for all elements and returns the original iterable.
28 */
29public fun <T> Iterable<T>.onEach(action: (T) -> Unit): Iterable<T> {
30 forEach<T>(action)
31 return this
34/**
35 * Accumulates left to right from the supplied initial value, which is returned for empty input.
36 */
37public fun <T, R> Iterable<T>.fold(initial: R, operation: (R, T) -> R): R {
38 var result = initial
39 for (element in this) result = operation(result, element)
40 return result
43/**
44 * Accumulates left to right from the first element, or returns None for empty input.
45 */
46public fun <T> Iterable<T>.reduceOrNone(operation: (T, T) -> T): T? {
47 var result: T? = None
48 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 result
57/**
58 * Accumulates left to right from the first element; empty input throws NoSuchElementException.
59 */
60public 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 */
66public fun <T> Iterable<T>.count(predicate: (T) -> Boolean): Long {
67 var result = 0L
68 for (element in this) { if (predicate(element)) result = nextCollectionCount(result) }
69 return result
72/**
73 * Counts eligible elements, applying the predicate when supplied.
74 */
75public fun <T> Iterable<T>.count(): Long = count<T> { _ -> true }
77/**
78 * Returns whether any eligible element exists, applying the predicate when supplied and stopping
79 * at the first match.
80 */
81public fun <T> Iterable<T>.any(predicate: (T) -> Boolean): Boolean {
82 for (element in this) { if (predicate(element)) return true }
83 return false
86/**
87 * Returns whether any eligible element exists, applying the predicate when supplied and stopping
88 * at the first match.
89 */
90public fun <T> Iterable<T>.any(): Boolean = when (iterator().next()) {
91 case None -> false
92 case Some(_) -> true
95/**
96 * Returns whether every element satisfies the predicate; true for empty input.
97 */
98public fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean {
99 for (element in this) { if (!predicate(element)) return false }
100 return true
103/**
104 * Returns whether no eligible element exists, applying the predicate when supplied and stopping at
105 * the first match.
106 */
107public fun <T> Iterable<T>.none(): Boolean = !any<T>()
108/**
109 * Returns whether no eligible element exists, applying the predicate when supplied and stopping at
110 * the first match.
111 */
112public fun <T> Iterable<T>.none(predicate: (T) -> Boolean): Boolean = !any<T>(predicate)