Scoop API

scoop:scoop.std:0.1.0

src/sequences/terminal_reduce.scoop

Selected production source · src/sequences/terminal_reduce.scoop

1package scoop.sequences
3import scoop.collections.*
5/**
6 * Consumes this sequence. Runs the action for each element in iteration order.
7 */
8public fun <T> Sequence<T>.forEach(action: (T) -> Unit): Unit =
9 terminalInput<T>().forEach<T>(action)
11/**
12 * Consumes this sequence. Runs the action with each element and its zero-based Long index.
13 */
14public fun <T> Sequence<T>.forEachIndexed(action: (Long, T) -> Unit): Unit =
15 terminalInput<T>().forEachIndexed<T>(action)
17/**
18 * Consumes this sequence. Accumulates left to right from the supplied initial value, which is
19 * returned for empty input.
20 */
21public fun <T, R> Sequence<T>.fold(initial: R, operation: (R, T) -> R): R =
22 terminalInput<T>().fold<T, R>(initial, operation)
24/**
25 * Consumes this sequence. Accumulates left to right from the first element; empty input throws
26 * NoSuchElementException.
27 */
28public fun <T> Sequence<T>.reduce(operation: (T, T) -> T): T =
29 terminalInput<T>().reduce<T>(operation)
31/**
32 * Consumes this sequence. Accumulates left to right from the first element, or returns None for
33 * empty input.
34 */
35public fun <T> Sequence<T>.reduceOrNone(operation: (T, T) -> T): T? =
36 terminalInput<T>().reduceOrNone<T>(operation)
38/**
39 * Consumes this sequence. Returns whether every element satisfies the predicate; true for empty
40 * input.
41 */
42public fun <T> Sequence<T>.all(predicate: (T) -> Boolean): Boolean =
43 terminalInput<T>().all<T>(predicate)
45/**
46 * Consumes this sequence. Returns two lists: matching elements first and nonmatching elements
47 * second, each retaining source order.
48 */
49public fun <T> Sequence<T>.partition(predicate: (T) -> Boolean): (List<T>, List<T>) =
50 terminalInput<T>().partition<T>(predicate)
52/**
53 * Consumes this sequence. Returns an independent list in reverse iteration order.
54 */
55public fun <T> Sequence<T>.reversed(): List<T> =
56 terminalInput<T>().reversed<T>()
58/**
59 * Consumes this sequence. Counts eligible elements, applying the predicate when supplied.
60 */
61public fun <T> Sequence<T>.count(): Long =
62 terminalInput<T>().count<T>()
64/**
65 * Consumes this sequence. Counts eligible elements, applying the predicate when supplied.
66 */
67public fun <T> Sequence<T>.count(predicate: (T) -> Boolean): Long =
68 terminalInput<T>().count<T>(predicate)
70/**
71 * Consumes this sequence. Returns whether any eligible element exists, applying the predicate when
72 * supplied and stopping at the first match.
73 */
74public fun <T> Sequence<T>.any(): Boolean =
75 terminalInput<T>().any<T>()
77/**
78 * Consumes this sequence. Returns whether any eligible element exists, applying the predicate when
79 * supplied and stopping at the first match.
80 */
81public fun <T> Sequence<T>.any(predicate: (T) -> Boolean): Boolean =
82 terminalInput<T>().any<T>(predicate)
84/**
85 * Consumes this sequence. Returns whether no eligible element exists, applying the predicate when
86 * supplied and stopping at the first match.
87 */
88public fun <T> Sequence<T>.none(): Boolean =
89 terminalInput<T>().none<T>()
91/**
92 * Consumes this sequence. Returns whether no eligible element exists, applying the predicate when
93 * supplied and stopping at the first match.
94 */
95public fun <T> Sequence<T>.none(predicate: (T) -> Boolean): Boolean =
96 terminalInput<T>().none<T>(predicate)