scoop:scoop.std:0.1.0
src/collections/find.scoop
Selected production source · src/collections/find.scoop
1
package scoop.collections3
internal fun <T> requireCollectionElement(value: T?): T = when (value) {4
case Some(val element) -> element5
case None -> throw NoSuchElementException("no matching element")6
}8
/**9
* Returns the first eligible element in iteration order, applying the predicate when supplied, or10
* None.11
*/12
public fun <T> Iterable<T>.firstOrNone(): T? = iterator().next()13
/**14
* Returns the first eligible element in iteration order, applying the predicate when supplied;15
* absence throws NoSuchElementException.16
*/17
public fun <T> Iterable<T>.first(): T = requireCollectionElement<T>(firstOrNone<T>())19
/**20
* Returns the first eligible element in iteration order, applying the predicate when supplied, or21
* None.22
*/23
public fun <T> Iterable<T>.firstOrNone(predicate: (T) -> Boolean): T? {24
for (element in this) { if (predicate(element)) return Some(element) }25
return None26
}28
/**29
* Returns the first eligible element in iteration order, applying the predicate when supplied;30
* absence throws NoSuchElementException.31
*/32
public fun <T> Iterable<T>.first(predicate: (T) -> Boolean): T =33
requireCollectionElement<T>(firstOrNone<T>(predicate))35
/**36
* Returns the first element satisfying the predicate, or None.37
*/38
public fun <T> Iterable<T>.find(predicate: (T) -> Boolean): T? = firstOrNone<T>(predicate)40
/**41
* Returns the last eligible element after traversal, applying the predicate when supplied, or42
* None.43
*/44
public fun <T> Iterable<T>.lastOrNone(predicate: (T) -> Boolean): T? {45
var result: T? = None46
for (element in this) { if (predicate(element)) result = Some(element) }47
return result48
}50
/**51
* Returns the last eligible element after traversal, applying the predicate when supplied, or52
* None.53
*/54
public fun <T> Iterable<T>.lastOrNone(): T? = lastOrNone<T> { _ -> true }55
/**56
* Returns the last eligible element after traversal, applying the predicate when supplied; absence57
* throws NoSuchElementException.58
*/59
public fun <T> Iterable<T>.last(): T = requireCollectionElement<T>(lastOrNone<T>())60
/**61
* Returns the last eligible element after traversal, applying the predicate when supplied; absence62
* throws NoSuchElementException.63
*/64
public fun <T> Iterable<T>.last(predicate: (T) -> Boolean): T =65
requireCollectionElement<T>(lastOrNone<T>(predicate))67
private fun <T> Iterable<T>.singleResult(predicate: (T) -> Boolean, rejectMany: Boolean): T? {68
var result: T? = None69
var found = false70
for (element in this) {71
if (predicate(element)) {72
if (found) {73
if (rejectMany) throw IllegalArgumentException(Some("multiple matching elements"))74
return None75
}76
found = true77
result = Some(element)78
}79
}80
return result81
}83
/**84
* Returns the sole eligible element, or None for zero or multiple matches.85
*/86
public fun <T> Iterable<T>.singleOrNone(predicate: (T) -> Boolean): T? = singleResult<T>(predicate, false)87
/**88
* Returns the sole eligible element, or None for zero or multiple matches.89
*/90
public fun <T> Iterable<T>.singleOrNone(): T? = singleOrNone<T> { _ -> true }91
/**92
* Requires exactly one eligible element. No match throws NoSuchElementException; multiple matches93
* throw IllegalArgumentException.94
*/95
public fun <T> Iterable<T>.single(predicate: (T) -> Boolean): T =96
requireCollectionElement<T>(singleResult<T>(predicate, true))97
/**98
* Requires exactly one eligible element. No match throws NoSuchElementException; multiple matches99
* throw IllegalArgumentException.100
*/101
public fun <T> Iterable<T>.single(): T = single<T> { _ -> true }103
/**104
* Returns the element at a zero-based iteration index, or None for a negative or out-of-range105
* index.106
*/107
public fun <T> Iterable<T>.elementAtOrNone(index: Long): T? {108
if (index < 0L) return None109
var remaining = index110
for (element in this) {111
if (remaining == 0L) return Some(element)112
remaining -= 1L113
}114
return None115
}117
/**118
* Returns the element at a zero-based iteration index; an out-of-range index throws119
* IndexOutOfBoundsException.120
*/121
public fun <T> Iterable<T>.elementAt(index: Long): T = when (elementAtOrNone<T>(index)) {122
case Some(val element) -> element123
case None -> throw IndexOutOfBoundsException()124
}126
/**127
* Returns the first equal element's zero-based iteration index, or -1 if absent.128
*/129
public fun <T : Equals<T>> Iterable<T>.indexOf(element: T): Long {130
var index = 0L131
for (value in this) {132
if (value == element) return index133
index = nextCollectionCount(index)134
}135
return -1L136
}138
/**139
* Stops at the first equal element and reports whether one exists.140
*/141
public operator fun <T : Equals<T>> Iterable<T>.contains(element: T): Boolean {142
for (value in this) { if (value == element) return true }143
return false144
}