Scoop API

scoop:scoop.std:0.1.0

src/collections/find.scoop

Selected production source · src/collections/find.scoop

1package scoop.collections
3internal fun <T> requireCollectionElement(value: T?): T = when (value) {
4 case Some(val element) -> element
5 case None -> throw NoSuchElementException("no matching element")
6}
8/**
9 * Returns the first eligible element in iteration order, applying the predicate when supplied, or
10 * None.
11 */
12public 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 */
17public 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, or
21 * None.
22 */
23public fun <T> Iterable<T>.firstOrNone(predicate: (T) -> Boolean): T? {
24 for (element in this) { if (predicate(element)) return Some(element) }
25 return None
28/**
29 * Returns the first eligible element in iteration order, applying the predicate when supplied;
30 * absence throws NoSuchElementException.
31 */
32public 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 */
38public 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, or
42 * None.
43 */
44public fun <T> Iterable<T>.lastOrNone(predicate: (T) -> Boolean): T? {
45 var result: T? = None
46 for (element in this) { if (predicate(element)) result = Some(element) }
47 return result
50/**
51 * Returns the last eligible element after traversal, applying the predicate when supplied, or
52 * None.
53 */
54public fun <T> Iterable<T>.lastOrNone(): T? = lastOrNone<T> { _ -> true }
55/**
56 * Returns the last eligible element after traversal, applying the predicate when supplied; absence
57 * throws NoSuchElementException.
58 */
59public fun <T> Iterable<T>.last(): T = requireCollectionElement<T>(lastOrNone<T>())
60/**
61 * Returns the last eligible element after traversal, applying the predicate when supplied; absence
62 * throws NoSuchElementException.
63 */
64public fun <T> Iterable<T>.last(predicate: (T) -> Boolean): T =
65 requireCollectionElement<T>(lastOrNone<T>(predicate))
67private fun <T> Iterable<T>.singleResult(predicate: (T) -> Boolean, rejectMany: Boolean): T? {
68 var result: T? = None
69 var found = false
70 for (element in this) {
71 if (predicate(element)) {
72 if (found) {
73 if (rejectMany) throw IllegalArgumentException(Some("multiple matching elements"))
74 return None
75 }
76 found = true
77 result = Some(element)
78 }
79 }
80 return result
83/**
84 * Returns the sole eligible element, or None for zero or multiple matches.
85 */
86public 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 */
90public fun <T> Iterable<T>.singleOrNone(): T? = singleOrNone<T> { _ -> true }
91/**
92 * Requires exactly one eligible element. No match throws NoSuchElementException; multiple matches
93 * throw IllegalArgumentException.
94 */
95public 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 matches
99 * throw IllegalArgumentException.
100 */
101public 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-range
105 * index.
106 */
107public fun <T> Iterable<T>.elementAtOrNone(index: Long): T? {
108 if (index < 0L) return None
109 var remaining = index
110 for (element in this) {
111 if (remaining == 0L) return Some(element)
112 remaining -= 1L
113 }
114 return None
117/**
118 * Returns the element at a zero-based iteration index; an out-of-range index throws
119 * IndexOutOfBoundsException.
120 */
121public fun <T> Iterable<T>.elementAt(index: Long): T = when (elementAtOrNone<T>(index)) {
122 case Some(val element) -> element
123 case None -> throw IndexOutOfBoundsException()
126/**
127 * Returns the first equal element's zero-based iteration index, or -1 if absent.
128 */
129public fun <T : Equals<T>> Iterable<T>.indexOf(element: T): Long {
130 var index = 0L
131 for (value in this) {
132 if (value == element) return index
133 index = nextCollectionCount(index)
134 }
135 return -1L
138/**
139 * Stops at the first equal element and reports whether one exists.
140 */
141public operator fun <T : Equals<T>> Iterable<T>.contains(element: T): Boolean {
142 for (value in this) { if (value == element) return true }
143 return false